Convert a RGB colour value to Decimal

前端 未结 6 1302
梦谈多话
梦谈多话 2021-02-04 13:09

How do I convert a RGB colour value to just plain decimal?

So I have: RGB(255,255,255) is white
Its decimal equivalent is: 16777215

I have tried thinking it

6条回答
  •  囚心锁ツ
    2021-02-04 13:34

    RGB integers are typically treated as three distinct bytes where the left-most (highest-order) byte is red, the middle byte is green and the right-most (lowest-order) byte is blue. You can retrieve the values of these individual bytes like this:

    var c = 0xff03c0; // 16712640
    var components = {
        r: (c & 0xff0000) >> 16, 
        g: (c & 0x00ff00) >> 8, 
        b: (c & 0x0000ff)
    };
    

    You can re-create a color from its components by shifting the bytes back in:

    c = (components.r << 16) + (components.g << 8) + (components.b);
    

    In your case, simply substitute components.r (etc) with your actual variables.

提交回复
热议问题