CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2001
    Posts
    19

    converting web colors to RGB

    Can anyone see what's wrong with this, can't figure it...
    sRGB is the web color......


    Dim sRed, sBlue, sGreen as string
    Dim iRed, iGreen, iBlue as Integer

    sRed = "&H" & Left$(sRGB, 2)
    sGreen = "&H" & mid$(sRGB, 3, 2)
    sBlue = "&H" & Right$(sRGB, 2)

    iRed = CInt((CLng(sRed) And CLng("&HFF0000")) / (CLng("&H00FFFF") + 1))
    iGreen = CInt((CLng(sGreen) And CLng("&H00FF00")) / (CLng("&H0000FF") + 1))
    iBlue = CInt((CLng(sBlue) And CLng("&H0000FF")) / (CLng("&H000000") + 1))

    main.txtColour.BackColor = RGB(iRed, iGreen, iBlue)



    thanx for any help

    mat



  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: converting web colors to RGB

    Well, the mistake you made is actually combining two methods. The first one is with bitwise comparision, where you made the mistake to check with the single values using sRed, sGreen and sBlue instead of the entire color sRGB.
    The second version is easier. The strings sReb, sBlue and sGreen already contain the numeric value, that is, in hexadecimal notation.
    Here's all that in code

    ' example 1 (the hard to read way)
    Dim iRed, iGreen, iBlue as Integer

    iRed = CInt((CLng("&h" & sRGB) And CLng("&HFF0000")) / (CLng("&H00FFFF") + 1))
    iGreen = CInt((CLng("&h" & sRGB) And CLng("&H00FF00")) / (CLng("&H0000FF") + 1))
    iBlue = CInt((CLng("&h" & sRGB) And CLng("&H0000FF")) / (CLng("&H000000") + 1))

    txtColour.BackColor = RGB(iRed, iGreen, iBlue)


    ' example 2 (the easy to read way)
    sRed = "&H" & Left$(sRGB, 2)
    sGreen = "&H" & mid$(sRGB, 3, 2)
    sBlue = "&H" & Right$(sRGB, 2)

    iRed = CLng(sRed)
    iGreen = CLng(sGreen)
    iBlue = CLng(sBlue)

    txtColour.BackColor = RGB(iRed, iGreen, iBlue)




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: converting web colors to RGB

    A one line solution:

    Me.BackColor = "&H" & Right$(sRGB, 2) & Mid$(sRGB, 3, 2) & Left$(sRGB, 2)



  4. #4
    Join Date
    Aug 2001
    Posts
    19

    Re: converting web colors to RGB

    cheerz for answering, sussed it out a while ago, a wood for the trees special.
    One liner only worx if its a full 6 place code and not a truncated one, though....


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

Featured