How can I convert a color constant (such as vbWindowBackground) to RGB? The method I'm now using (see below) only works with colors retrieved from a CommonDialog color dialog selector.
Code:
RVal = Int(lSourceColor Mod 256)
GVal = Int((lSourceColor \ 256) Mod 256)
BVal = lSourceColor \ 65536
But this doesn't work with when lSourceColor is a system color constant. Do I have to convert the color to another format before using that RGB function? How?
Last edited by Andrex; December 25th, 2006 at 12:02 AM.
Take a look at this. I've included the source so that you don't have to make the form.
Code:
Option Explicit
Private Type HSL
Hue As Long
Saturation As Long
Luminance As Long
End Type
Private Function HSLToRGB(ByVal Hue As Long, ByVal Saturation As Long, ByVal Luminance As Long) As Long
Dim R As Long, G As Long, B As Long
Dim lMax As Long, lMid As Long, lMin As Long
Dim Delta As Single
lMax = (Luminance * 255) / 100
lMin = (100 - Saturation) * lMax / 100
Delta = (lMax - lMin) / 60
Select Case Hue
Case 0 To 60
lMid = (Hue - 0) * Delta + lMin
R = lMax: G = lMid: B = lMin
Case 60 To 120
lMid = -(Hue - 120) * Delta + lMin
R = lMid: G = lMax: B = lMin
Case 120 To 180
lMid = (Hue - 120) * Delta + lMin
R = lMin: G = lMax: B = lMid
Case 180 To 240
lMid = -(Hue - 240) * Delta + lMin
R = lMin: G = lMid: B = lMax
Case 240 To 300
lMid = (Hue - 240) * Delta + lMin
R = lMid: G = lMin: B = lMax
Case 300 To 360
lMid = -(Hue - 360) * Delta + lMin
R = lMax: G = lMin: B = lMid
End Select
HSLToRGB = B * &H10000 + G * &H100& + R
End Function
Private Function RGBToHSL(ByVal RGBValue As Long) As HSL
Dim R As Long, G As Long, B As Long
Dim lMax As Long, lMin As Long
Dim Delta As Single
R = RGBValue And &HFF
G = (RGBValue And &HFF00&) \ &H100&
B = (RGBValue And &HFF0000) \ &H10000
If R > G Then
lMax = R: lMin = G
Else
lMax = G: lMin = R
End If
If B > lMax Then
lMax = B
ElseIf B < lMin Then
lMin = B
End If
RGBToHSL.Luminance = lMax * 100 / 255
If lMax > lMin Then
RGBToHSL.Saturation = (lMax - lMin) * 100 / lMax
Delta = 60 / (lMax - lMin)
Select Case lMax
Case R
If B > G Then
RGBToHSL.Hue = Delta * (G - B) + 360
Else
RGBToHSL.Hue = Delta * (G - B)
End If
Case G
RGBToHSL.Hue = Delta * (B - R) + 120
Case B
RGBToHSL.Hue = Delta * (R - G) + 240
End Select
End If
End Function
Private Sub cmdGo_Click()
Dim Color As HSL
Color = RGBToHSL(Val("&H" & txtRGB.Text))
For Color.Hue = 0 To 49
Picture1(Color.Hue).BackColor = HSLToRGB(Color.Hue, Color.Saturation, Color.Luminance)
Next Color.Hue
End Sub
Sub form_load()
Dim colour As Long, red As Byte, green As Byte, blue As Byte
Dim str As String
red = 11
green = 22
blue = 33
str = "Red : " & red & vbCrLf
str = str & "Green : " & green & vbCrLf
str = str & "Blue : " & blue & vbCrLf
MsgBox str
colour = RGB(red, green, blue)
blue = (colour And &HFF0000) \ &H10000
green = (colour And &HFF00&) \ &H100&
red = colour And &HFF
str = "Red : " & red & vbCrLf
str = str & "Green : " & green & vbCrLf
str = str & "Blue : " & blue & vbCrLf
MsgBox str
txtRGB.Text = RGB(100, 100, 100) ' colour
End Sub
Thanks, but it seems I didn't explain myself correctly. Neither of the functions posted worked.
Let's suppose I have a system color constant such as vbWindowBackground that I want to convert to RGB. All I need is a function to decompose this color to get the values:
Red=255
Green=255
Blue=255
which equals the white background of Windows' objects.
Thanks in advance (and merry christmas, )
Last edited by Andrex; December 25th, 2006 at 03:51 PM.
Colour is a long which you can express by a long or by a vbcolour or RGB(r,g,b) as you whish !
The valid range for a normal RGB color is 0 to 16,777,215 (&HFFFFFF). The high byte of a number in this range equals 0; the lower 3 bytes, from least to most significant byte, determine the amount of red, green, and blue, respectively. The red, green, and blue components are each represented by a number between 0 and 255 (&HFF). If the high byte isn't 0, Visual Basic uses the system colors, as defined in the user's Control Panel settings and by constants listed in the Visual Basic (VB) object library in the Object Browser.
edit : you may also use the TranslateColor function of the olepro32.dll library
Last edited by moa; December 25th, 2006 at 04:06 PM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.