Click to See Complete Forum and Search --> : converting web colors to RGB


podulator
September 13th, 2001, 12:10 PM
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

Cakkie
September 13th, 2001, 03:52 PM
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
slisse@planetinternet.be

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

shree
September 13th, 2001, 07:52 PM
A one line solution:

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

podulator
September 13th, 2001, 09:40 PM
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....