color button to rgb

Post your Gambas programming questions here.
Post Reply
User avatar
sadams54
Posts: 171
Joined: Monday 9th July 2018 3:43am
Contact:

color button to rgb

Post by sadams54 »

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?
User avatar
BruceSteers
Posts: 1884
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: color button to rgb

Post by BruceSteers »

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)

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
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: color button to rgb

Post by Cedron »

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!
User avatar
BruceSteers
Posts: 1884
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: color button to rgb

Post by BruceSteers »

Cedron wrote: Thursday 9th March 2023 2:48am 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

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
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: color button to rgb

Post by Cedron »

BruceSteers wrote: Thursday 9th March 2023 3:01am ...

so with alpha..

     a = Color.GetAlpha(ArgColor)
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!
User avatar
sadams54
Posts: 171
Joined: Monday 9th July 2018 3:43am
Contact:

Re: color button to rgb

Post by sadams54 »

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
Post Reply