代码之家  ›  专栏  ›  技术社区  ›  mco

如何将rgba转换成hex?

  •  1
  • mco  · 技术社区  · 7 年前

    我有一个来自服务器的rgba格式的颜色,但是我认为roku不能理解rgba,所以想知道是否有一个方便的api来将roku中的rgba转换为hex?

    例子: (255, 255 , 255, 255) - > 0xFFFFFFFF

    谢谢

    2 回复  |  直到 7 年前
        1
  •  2
  •   mco    7 年前

    我认为roku中没有api,所以我只是编写了一个函数。

    // 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
    
        2
  •  0
  •   Matt    5 年前

    您也可以使用位移位,它不会按要求返回十六进制,但会返回一个可以使用的颜色值

    颜色=(R<<24)+(G<<16)+(B<<8)+A