将RGB颜色值转换为十进制 [英] Convert a RGB colour value to Decimal

查看:1510
本文介绍了将RGB颜色值转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将RGB颜色值转换为纯十进制?

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

所以我有:RGB(255,255,255)是白色的
其十进制等效值为:16777215

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

我试图认为可能只是:

var dec = r*g*b; // but this doesn't work

尽管不起作用.

是否有人知道从RGB(或rgba)转换为十进制的算法/方程式?

Anyone know of the algorithm/equation to convert from RGB (or rgba) to decimal?

推荐答案

RGB整数通常被视为三个不同的字节,其中最左边(最高位)字节为红色,中间字节为绿色,最右边的字节为(最低位)字节为蓝色.您可以像下面这样检索这些单个字节的值:

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);

在您的情况下,只需将components.r(等)替换为您的实际变量即可.

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

这篇关于将RGB颜色值转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆