I need to convert a long value of a color to the relative R G B values mathematically, since I don't know how, I leave it to this board's capible 'hands'.
Thank you,
Printable View
I need to convert a long value of a color to the relative R G B values mathematically, since I don't know how, I leave it to this board's capible 'hands'.
Thank you,
R = Color mod 256
G = int(Color / 256) mod 256
B = int(Color / &h10000)
- depending on where your colour came from, you may need to swap the Red and Blue values around (endian problems).Code:Private Type ColourRGB
lRed As Integer
lGreen As Integer
lBlue As Integer
End Type
Private Sub Form_Load()
Dim z As ColourRGB
z = ExtractColour(RGB(&HAA, &HBB, &HCC))
Debug.Print Hex(z.lRed)
Debug.Print Hex(z.lGreen)
Debug.Print Hex(z.lBlue)
End Sub
Private Function ExtractColour(lColour As Long) As ColourRGB
Dim tempC As ColourRGB
tempC.lBlue = (lColour \ &H10000) And &HFF
tempC.lGreen = (lColour \ &H100) And &HFF
tempC.lRed = lColour And &HFF
ExtractColour = tempC
End Function
while everyone is on the subject of colors, how is it possible to save the custom colors in a choosecolor dialog box when the user creates them even if they dont select the color as they click ok?
I have not checked the following, but it should be close to correct.I am not sure what VB does with arithmetic using Integer & Long variables, but I think it does sensible integer arithmetic.Code:Dim Red As Integer
Dim Green As Integer
Dim Blue As Integer
Dim Tlong As Long
Blue = RGBvalue Mod 256 'Blue is remainder after divide by 256
Tlong = (RGBvalue - Blue)/256 'Tlong now contains Red & Blue values.
Green = Tlong Mod 256
Red = (Tlong - Green)/256
well markman, depends what choosecolor dialog box you are using?
There's also a way to get the RGB values as you know the color is stored in a long and each value represent a byte
Code:Type LngColor
Color As Long
End Type
Type RGBColor
Red As Byte
Green As Byte
Blue As Byte
Dummy As Byte
End Type
Dim a As LngColor, b As RGBColor
a.Color = 123456
LSet b = a
With b
Debug.Print .Red
Debug.Print .Green
Debug.Print .Blue
End With
Code:lRed = lColor Mod &H100
lGreen = Int(lColor / &H100) Mod &H100
lBlue = Int(lColor / &H10000) Mod &H100
meg, you don't need to use modulus for lblue