"squirreltown" wrote:
That seems to work, I tried it with the red and blue colors that failed before and they returned correctly.
So thank you, I would not have figured it out.
I looked up Integer Bitshift Operators and a dull haze surrounded everything so I stopped reading it.
As far as the hacky part goes, i won't tell anyone if you don't.
Function RGBtoHexString( a_r As Integer, a_g As Integer, a_b As Integer ) As String
l_rgb = LeftShift( a_r and &hFF, 16 ) or LeftShift( a_g and &hFF, 8 ) or a_b and &hFF
return IntegerToHexString ( l_rgb )
End Function
Function IntegerToHexString( a_number As Integer ) As String
l_hexDigits = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
l_hexString = ""
for l_i = 5 to 0 step -1
l_hexString = l_hexString + l_hexDigits[ RightShift( a_number, l_i * 4 ) and &hF ]
end for
return l_hexString + "FF"
End Function
Function RightShift( a_number As Integer, a_shift As Integer ) As Integer
if a_number > 0 then a_number = Int( a_number / ( 2 ^ a_shift ) )
return a_number
End Function
Function LeftShift( a_number As Integer, a_shift As Integer ) As Integer
if a_number > 0
for l_i = 1 to a_shift
l_number = a_number and &h40000000
a_number = ( a_number and &h3FFFFFFF ) * 2
if l_number then a_number = a_number or &h80000000
end for
end if
return a_number
End Function
Function HexToInteger( a_hex As String ) As Integer
l_bArr = CreateObject( "roByteArray" )
l_bArr.FromHexString( a_hex )
l_int = 0
for each l_byte in l_bArr : l_int = 256 * l_int + l_byte : end for
return l_int
End Function
Function RGBA_to_hex( a_R As Integer, a_G As Integer, a_B As Integer, a_A As Integer ) As String
l_b = createObject( "roByteArray" )
l_b[0] = a_R 'if >255, assignment trims it (as if "and &hFF")
l_b[1] = a_G
l_b[2] = a_B
l_b[3] = a_A
return l_b.toHexString()
End function
Function RGBA_to_int(R, G, B, A) As Integer
return &h1000000 * R + &h10000 * G + &h100 * B + A
End Function
"Rek" wrote:
Just out of curiosity, why do you want to do this? In general, it seems like the hex color codes are easier to understand (just r,g,b,a components). The decimal representation has little to no value to a human...
r = (value >> 24) AND 255
g = (value >> 16) AND 255
b = (value >> 😎 AND 255
a = value AND 255
color = {r: 235, g: 16, b: 16, a: 255}
colorValue = ((color.r * 256 + color.g) * 256 + color.b) * 256) + color.a
"RokuMarkn" wrote:
If you want to convert an integer value to rgb, I think the easiest way is something like this:
--Mark
"RokuMarkn" wrote:r = (value >> 24) AND 255
g = (value >> 16) AND 255
b = (value >> 😎 AND 255
a = value AND 255
function deRGBA(rgba):
ba = createObject("roByteArray")
for _ = 1 to 4:
ba.unshift(rgba and 255)
rgba = (rgba and -256) / 256
end for
return ba
end function
BrightScript Debugger> ? deRGBA(&h0889f7FF).toHexString()
0889F7FF
BrightScript Debugger> ? deRGBA(&h911820FF).toHexString()
911820FF
BrightScript Debugger> ? deRGBA(&h0889f7FF)
8
137
247
255
BrightScript Debugger> ? deRGBA(&h911820FF)
145
24
32
255
BrightScript Debugger> ? &h0889f7ff / 256, &h0889f7and realize it's not int() nor fix() to blame but there was loss of precision because 4-byte-floats division was used. "Oh i know," i thought - "i will force double division". Young grasshopper, you have yet to master the kung-fu of BrightScript:
559608 559607 'first was expected to be 559607.xxxx
BrightScript Debugger> ? &h0889f7ff / 256d, int(&h0889f7ff / 256d), fix(&h0889f7ff / 256d)(Solving the mystery of the second failure left as exercise for the reader 8-) )
559607.99609375 559608 559608 'last two totally should have been 559607
"NewManLiving" wrote:
These are some older functions that were found on other sites or were written by EnTerr ( Whom we have not heard from lately. Hope you are well sir ).
"EnTerr" wrote:
(**) don't try this at home, kids - every time you look yourself up, Dog kills a kitten!