color button to rgb

Post

Posted
Rating:
#1 (In Topic #1002)
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
 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?
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
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)

Code (gambas)

  1. Public Sub GetRGB(Colour As Integer) As Float[]
  2.  
  3.   Dim R, G, B, A As Float
  4.  
  5.   Dim sChar As String = Color.ToHTML(Colour)
  6.  
  7.   If Left(sChar) = "#" Then
  8.     sChar = Right(sChar, -1)
  9.   Else
  10.     sChar = Mid(sChar, 6, sChar.Len - 7)
  11.     Return Split(sChar)
  12.  
  13.   R = Val("&" & sChar[0, 2])
  14.   G = Val("&" & sChar[2, 2])
  15.   B = Val("&" & sChar[4, 2])
  16.   If sChar.Len = 8 Then A = Val("&" & sChar[6, 2])
  17.   Return [R, G, B, A]
  18.  
  19.  
  20.  

or if you just want the hex value use

Code (gambas)

  1.  
  2. Print Color.ToHTML(Color.Red)
  3.  
  4.  

Prints…
#FF0000

or if handling alpha too.

Code (gambas)

  1.  
  2. Print Color.ToHTML(Color.SetAlpha(Color.Yellow, 100))
  3.  
  4.  
prints…
rgba(255,255,0,0.6)
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
Cedron is in the usergroup ‘Regular’
Here's a slightly more concise version of the same routine.

Code (gambas)

  1. Public Sub GetRGB(ArgColor As Integer) As Integer[]
  2.  
  3.     Dim r, g, b As Integer
  4.  
  5.     r = Shr(ArgColor, 16) And &FF&
  6.     g = Shr(ArgColor, 8) And &FF&
  7.     b = ArgColor And &FF&
  8.  
  9.     Return [r, g, b]
  10.  
  11.  

.... and carry a big stick!
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

Cedron said

Here's a slightly more concise version of the same routine.

Code (gambas)

  1. Public Sub GetRGB(ArgColor As Integer) As Integer[]
  2.  
  3.     Dim r, g, b As Integer
  4.  
  5.     r = Shr(ArgColor, 16) And &FF&
  6.     g = Shr(ArgColor, 8) And &FF&
  7.     b = ArgColor And &FF&
  8.  
  9.     Return [r, g, b]
  10.  
  11.  


ooh nice , much better  :)

so with alpha..

Code (gambas)

  1. Public Sub GetRGBA(ArgColor As Integer) As Integer[]
  2.  
  3.     Dim r, g, b, a As Integer
  4.  
  5.     r = Shr(ArgColor, 16) And &FF&
  6.     g = Shr(ArgColor, 8) And &FF&
  7.     b = ArgColor And &FF&
  8.     a = Color.GetAlpha(ArgColor)
  9.  
  10.     Return [r, g, b, a]
  11.  
  12.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
Cedron is in the usergroup ‘Regular’

BruceSteers said



so with alpha..

Code (gambas)

  1.      a = Color.GetAlpha(ArgColor)
  2.  

I didn't see your update.  Yes, I suspect that would be a microtad faster than using:

Code (gambas)

  1.      a = Shr(ArgColor, 24) And &FF&
  2.  

.... and carry a big stick!
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
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

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
Online now: No Back to the top
1 guest and 0 members have just viewed this.