将 RGB 值转换为二进制到十进制
我有以下 java 代码,它采用颜色 RGB 的三个双精度值(0 到 1 之间)并将它们转换为十进制格式。我了解第一个 8 位如何保存颜色 x,第二个 8 位颜色 y ...以及如何从生成的二进制中获取十进制值。我百分百不明白的是为什么我们乘以255(我知道128+64+32+16+8+4+2+1)。将 double 值乘以 255 到底能得到什么?它是一个可以存储在 8 位中的值吗?为什么我们不使用 256(一种颜色的可能数量)?
public final double getR() {
return (1 - cyan);
}
public final double getG() {
return (1 - magenta);
}
public final double getB() {
return (1 - yellow);
}
/**
* Gets the rgb color in one integer.
*
* @return an integer containing the red component in bits 16-23, the green component in bits 8-15
* and the blue component in bits 0-7. Bits 24-32 are zero.
*/
public int getRGB() {
int r = (int) Math.round(getB() * 255);
r |= (int) Math.round(getR() * 255) << 16;
r |= (int) Math.round(getG() * 255) << 8;
return r;
}
谢谢
I have the following java code, which takes three double values (between 0 and 1) of the colors RGB and converts them to decimal format. I understood how the first 8 bit save color x, the second 8 bit color y ... and also how to get the decimal value from the resulting binary. what i dont understand 100% is why we multiply with 255 (i know 128+64+32+16+8+4+2+1). What exactly do we get from multiplying the double value with 255. is it a value which can be stored in 8 bit? And why dont we use 256 (possible amount of one color)?
public final double getR() {
return (1 - cyan);
}
public final double getG() {
return (1 - magenta);
}
public final double getB() {
return (1 - yellow);
}
/**
* Gets the rgb color in one integer.
*
* @return an integer containing the red component in bits 16-23, the green component in bits 8-15
* and the blue component in bits 0-7. Bits 24-32 are zero.
*/
public int getRGB() {
int r = (int) Math.round(getB() * 255);
r |= (int) Math.round(getR() * 255) << 16;
r |= (int) Math.round(getG() * 255) << 8;
return r;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从
double
进行转换,因为您无法将double
值存储为 8 位。您的
double
值介于0.0
和1.0
之间。您可以将它们视为所用颜色的比例(例如,黄色的 0.33333333 表示使用了可能的黄色的三分之一)。正如您所看到的,这样的 double 可以有很多小数位,这意味着我们需要大量内存(64 位)来存储这样的颜色。您的函数现在尝试将 double 值存储为仅 8 位(256 个值)。正如我们所说,double 值可以被视为一部分(0 到 1 之间),并且该函数对 8 位(0 到 255 之间)计算相同的值。只需将
double
值乘以 255 即可完成。对于黄色示例(使用 0.33333333 黄色),其结果为:0.33333333 * 255 = 84,99999915
。意义还是一样,使用了255个黄色部分中的84,99999915个黄色部分,仍然是三分之一。为了获得该数字的压缩版本,它将四舍五入到下一个整数值。在我们的示例中,这是
85
,它非常接近实际部分,但我们节省了大量内存。对于最低的
double
值0.0
也有意义,它会转换为最低的int
值0
。最高双精度值1.0
转换为255
(最高 8 位数字)。总之,我们将 double(64 位)转换为仅 8 位数字,其颜色比例相同,但不那么准确。
编辑:因为 255 也存在混淆:8 位可以存储 256 个值(0 到 255)。如果您可以在某处选择 256 作为颜色值,那么它们会使用 1-256 范围(不带 0)。本质上,它是移动 1 的相同值。
You need the conversion from
double
because you cannot store thedouble
value into 8 bit.Your
double
values are between0.0
and1.0
. You can see them as the proportion of color used (e.g. 0.33333333 in yellow means that one-third of the possible yellow is used). As you can see such adouble
can have many decimal places, which means we need a lot of memory (64-bit) to store such a color.Your function now tries to store the
double
value into only 8 bit (256 values). As we said thedouble
value can be seen as a portion (between 0 and 1) and the function calculates the same for 8 bit (between 0 and 255). This can simply be done by multiplying thedouble
value with 255. For the example with yellow (0.33333333 of yellow is used) it is:0.33333333 * 255 = 84,99999915
. The meaning is still the same 84,99999915 yellow parts of 255 yellow parts are used, which is still a third.In order to get a compressed version of this number, it is rounded to the next integer value. In our example, this is
85
, which is really close to the actual portion, but we save a lot of memory.It makes also sense for the lowest
double
value0.0
, which is converted to the lowestint
value0
. The highest double value1.0
is converted to255
(highest 8-bit number).In conclusion, we convert a
double
(64-bit) into an only 8-bit number, which has the same proportion of the color, but it is not as accurate.Edit: As there is also a confusion with the 255: 8 bit can store 256 values (0 to 255). If you can choose 256 as a color value somewhere they use the range 1-256 without 0. Essentially it is the same one shifted by 1.