color button to rgb
color button to rgb
I need to convert the value of the color button to a RGB value and I am unable to find how to do this. It means I am overthinking again. I just need to convert the color button value to the proper RGB value. I tried converting to hex but that does not give good results on custom colors. Can somebody help me with this simple thing?
- BruceSteers
- Posts: 1884
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: color button to rgb
There's a few ways i think.
I do not know the best way.
Here's a function that returns [r,g,b,a] values from a color
It uses Color.ToHTML() to get a Hex value then converts it.
(EDIT: updated to handle alpha)
or if you just want the hex value use
Prints...
#FF0000
or if handling alpha too.
prints...
rgba(255,255,0,0.6)
I do not know the best way.
Here's a function that returns [r,g,b,a] values from a color
It uses Color.ToHTML() to get a Hex value then converts it.
(EDIT: updated to handle alpha)
Public Sub GetRGB(Colour As Integer) As Float[]
Dim R, G, B, A As Float
Dim sChar As String = Color.ToHTML(Colour)
If Left(sChar) = "#" Then
sChar = Right(sChar, -1)
Else
sChar = Mid(sChar, 6, sChar.Len - 7)
Return Split(sChar)
Endif
R = Val("&" & sChar[0, 2])
G = Val("&" & sChar[2, 2])
B = Val("&" & sChar[4, 2])
If sChar.Len = 8 Then A = Val("&" & sChar[6, 2])
Return [R, G, B, A]
End
or if you just want the hex value use
Print Color.ToHTML(Color.Red)
Prints...
#FF0000
or if handling alpha too.
Print Color.ToHTML(Color.SetAlpha(Color.Yellow, 100))
prints...
rgba(255,255,0,0.6)
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: color button to rgb
Here's a slightly more concise version of the same routine.
Public Sub GetRGB(ArgColor As Integer) As Integer[]
Dim r, g, b As Integer
r = Shr(ArgColor, 16) And &FF&
g = Shr(ArgColor, 8) And &FF&
b = ArgColor And &FF&
Return [r, g, b]
End
.... and carry a big stick!
- BruceSteers
- Posts: 1884
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: color button to rgb
ooh nice , much better
so with alpha..
Public Sub GetRGBA(ArgColor As Integer) As Integer[]
Dim r, g, b, a As Integer
r = Shr(ArgColor, 16) And &FF&
g = Shr(ArgColor, 8) And &FF&
b = ArgColor And &FF&
a = Color.GetAlpha(ArgColor)
Return [r, g, b, a]
End
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: color button to rgb
I didn't see your update. Yes, I suspect that would be a microtad faster than using:
a = Shr(ArgColor, 24) And &FF&
.... and carry a big stick!
Re: color button to rgb
thank you. You put me on the right track. I modified the code you gave so it returns a string "RRGGBB" padded properly and seems to be giving me dead on color to pass on.
Code: Select all
Public Function GetRGB(ArgColor As Integer) As String
Dim r, g, b As Integer
Dim RGB As String
r = Shr(ArgColor, 16) And &FF&
g = Shr(ArgColor, 8) And &FF&
b = ArgColor And &FF&
RGB = Right("0" & Hex(r), 2) & Right("0" & Hex(g), 2) & Right("0" & Hex(b), 2)
Return RGB
End