B4J Question Return RBG from ColorPicker

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

an option is to make use of a JavaObject (requires JavaObject Library) to convert the selected ColorPicker Paint Value into R-G-B.
Example Sub:
B4X:
'Get the color given as paint as a string in R-G-B
'Example result is 26-51-153
Sub GetColorRGB(ColorValue As Paint) As String
    Dim result As String = ""
    Dim joCV As JavaObject = ColorValue
    Dim R As Double = joCV.RunMethod("getRed", Null)   
    Dim G As Double = joCV.RunMethod("getGreen", Null)   
    Dim B As Double = joCV.RunMethod("getBlue", Null)
    result = Round(R * 255) & "-" & Round(G * 255) & "-" & Round(B * 255)
    Return result
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another option which is similar to how it is done in B4A and B4i:
B4X:
Sub GetARGB(ColorValue As Paint) As Int()
    Dim Color As Int = fx.Colors.To32Bit(ColorValue)
    Dim res(4) As Int
    res(0) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff000000), 24)
    res(1) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff0000), 16)
    res(2) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff00), 8)
    res(3) = Bit.And(Color, 0xff)
    Return res
End Sub
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Another option which is similar to how it is done in B4A and B4i:
B4X:
Sub GetARGB(ColorValue As Paint) As Int()
    Dim Color As Int = fx.Colors.To32Bit(ColorValue)
    Dim res(4) As Int
    res(0) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff000000), 24)
    res(1) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff0000), 16)
    res(2) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff00), 8)
    res(3) = Bit.And(Color, 0xff)
    Return res
End Sub
Hi Erel

In your example, I assume that R = res(0), G = res(1) and B = res(2) and A = (3), is that correct?
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Hi,

an option is to make use of a JavaObject (requires JavaObject Library) to convert the selected ColorPicker Paint Value into R-G-B.
Example Sub:
B4X:
'Get the color given as paint as a string in R-G-B
'Example result is 26-51-153
Sub GetColorRGB(ColorValue As Paint) As String
    Dim result As String = ""
    Dim joCV As JavaObject = ColorValue
    Dim R As Double = joCV.RunMethod("getRed", Null) 
    Dim G As Double = joCV.RunMethod("getGreen", Null) 
    Dim B As Double = joCV.RunMethod("getBlue", Null)
    result = Round(R * 255) & "-" & Round(G * 255) & "-" & Round(B * 255)
    Return result
End Sub
Thanks a lot!
 
Upvote 0
Top