Convert a RGB colour value to Decimal

前端 未结 6 1322
梦谈多话
梦谈多话 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:19

    if (color.substr(0, 1) === '#') {
        return color;
    }
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
    var red = parseInt(digits[2]);
    var green = parseInt(digits[3]);
    var blue = parseInt(digits[4]);
    var rgb = blue | (green << 8) | (red << 16);
    return rgb.toString(10);
    

提交回复
热议问题