Results 1 to 13 of 13

Thread: RESOLVED - is string a valid color ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    RESOLVED - is string a valid color ?

    im stuck can anyone help me out there has to be a better way to determin if a string is or is not a colour ....

    this is what i've found but im not using it ....

    Code:
    Dim mycc As New ColorConverter
    
      Try
                    Dim lbl As New Label
                    lbl.BackColor = mycc.ConvertFrom($STRING)
                    IsColour = True
                Catch ex As Exception
                    IsColour = False
                    Return True
                End Try
    Last edited by illskills; Feb 15th, 2010 at 08:01 AM.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: is string a valid color ?

    vb.net Code:
    1. Function IsColor(ByVal TextName As String) As Boolean
    2.     If [Enum].GetNames(GetType(KnownColor)).Contains(TextName) Then Return True Else Return False
    3. End Function

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: is string a valid color ?

    wow thanks that's awesome !

    going to test it now

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: is string a valid color ?

    if the string format is in RGB will this still work ?

  5. #5

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: is string a valid color ?

    Quote Originally Posted by cicatrix View Post
    No, this was meant for IsColor("AliceBlue") Then ...
    is there an easy way to check rather than breaking the string down ?

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: is string a valid color ?

    I'm afraid, no.

    vb.net Code:
    1. Function ColorFromRGBString(ByVal RGBString As String) As Color
    2.         ' Input argument RGBString must be in "R, G, B" format,
    3.         ' for example "215, 188, 212"
    4.         Dim rgbelems() As String = RGBString.Split(New Char() {","c})
    5.         If rgbelems.Count <> 3 Then
    6.             Throw New ArgumentException("Wrong argument " + RGBString)
    7.             Return Nothing
    8.         End If
    9.  
    10.         Dim components(2) As Byte, x As Integer
    11.         For x = 0 To 2
    12.             If Not Byte.TryParse(rgbelems(x), components(x)) Then
    13.                 Throw New ArgumentException("Wrong argument " + RGBString)
    14.                 Return Nothing
    15.             End If
    16.         Next
    17.  
    18.         Dim c As Color = Color.FromArgb(255, components(0), components(1), components(2))
    19.         Return c
    20. End Function

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: is string a valid color ?

    thanks cicatrix these are both very useful

    i think i found an easy way ...

    the string i have is either a file path or rgb value

    since ","'s are not allowed in file paths im using the following check ...

    if instr($string,",") > 0 then iscolour = true

    but will put the above functions in my code bank

    thanks
    again
    John

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: is string a valid color ?

    Your methods can lead to errors since you don't check what's in it.
    For example, if I pass "A, B, C, D" to it it will return IsColor = True

    Besides, you can create directories and filenames containing commas.

    Check it yourself, in the command prompt type:
    Code:
    MkDir "C:\A,B,C"
    echo test > "A,B,C.txt"

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: is string a valid color ?

    your right i just checked ... i thought ","'s were not allowed in file names ... but they are

    .... the second function you posted is excellent im currently looking @ it now

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: is string a valid color ?

    Code:
        Function ColorFromRGBString(ByVal RGBString As String) As Object
            ' Input argument RGBString must be in "R, G, B" format,
            ' for example "215, 188, 212"
    
            Dim rgbelems() As String = RGBString.Split(New Char() {","c})
            If rgbelems.GetLength(0) <> 3 Then
                'Throw New ArgumentException("Wrong argument " + RGBString)
                Return Nothing
            End If
            Dim components(2) As Byte, x As Integer
            For x = 0 To 2
                If Not Byte.TryParse(rgbelems(x), components(x)) Then
                    'Throw New ArgumentException("Wrong argument " + RGBString)
                    Return Nothing
                End If
            Next
            Dim c As Color = Color.FromArgb(255, components(0), components(1), components(2))
            Return c
        End Function
    i edited the code above ... the .count function wouldnt work on rgbemlems

    so i replaced it with .GetLength(0) <> 3

    is this correct ?

    took out the argument exceptions and changed the return type to object

    now it returns a color object if the string is a color and nothing if its not.

    Code:
    Dim c
                c = ColorFromRGBString($string)
                If IsNothing(c) Then
                    Exit Function
                End If

  12. #12

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: is string a valid color ?

    Quote Originally Posted by cicatrix View Post
    As long as it works...
    yes it seems to be working now

    thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width