Roku: How to convert rgba to hex?

1

I have a color in rgba format coming from the server, but I don't think roku can understand rgba, so was wondering if there was a convenient api to convert rgba to hex in roku?

Example: (255, 255 , 255, 255) - > 0xFFFFFFFF

Thanks

hex
rgba
roku
brightscript
asked on Stack Overflow May 23, 2018 by mco

2 Answers

2

I don't think there is an api in roku so I just wrote a function.

// Example: If you input (255, 255, 255, 255) as the argument it will return "0xFFFFFFFF"

function rgbaToHex(r as integer, g as integer, b as integer, a as integer)
    hexArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
    hexColor = "0x"

    for i = 0 to 3
        colorChannel = invalid

        if(i = 0) then
            colorChannel = r

        else if(i = 1) then
            colorChannel = g

        else if(i=2) then  
            colorChannel = b

        else if(i=3) then
            colorChannel = a
        end if

        sixteens = int(colorChannel / 16)  // How many 16's can go into colorChannel (since hex is base 16)?
        ones = colorChannel mod 16         // How many 1's are in the remainder?

        hexColor += hexArray[sixteens] + hexArray[ones]
    end for

    return hexColor
end function
answered on Stack Overflow May 23, 2018 by mco • edited May 23, 2018 by mco
0

You could also use a bit shift it won't return a hex as requested but will return a color value that can be used

color =(r<<24)+(g<<16)+(b<<8)+a

answered on Stack Overflow Jul 18, 2019 by Matt

User contributions licensed under CC BY-SA 3.0