[英]Convert RGB values to binary to decimal
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.我有以下 java 代码,它采用 colors RGB 的三个双精度值(介于 0 和 1 之间)并将它们转换为十进制格式。 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.我了解第一个 8 位如何保存颜色 x,第二个 8 位颜色 y...以及如何从生成的二进制文件中获取十进制值。 what i dont understand 100% is why we multiply with 255 (i know 128+64+32+16+8+4+2+1).我 100% 不明白的是为什么我们乘以 255(我知道 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?将 double 值乘以 255 到底得到了什么?它是一个可以存储在 8 位中的值吗? And why dont we use 256 (possible amount of one color)?为什么我们不使用 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;
}
Thanks谢谢
You need the conversion from double
because you cannot store the double
value into 8 bit.您需要从double
进行转换,因为您不能将double
值存储为 8 位。
Your double
values are between 0.0
and 1.0
.您的double
值介于0.0
和1.0
之间。 You can see them as the proportion of color used (eg 0.33333333 in yellow means that one-third of the possible yellow is used).您可以将它们视为所用颜色的比例(例如,黄色中的 0.33333333 表示使用了可能的黄色的三分之一)。 As you can see such a double
can have many decimal places, which means we need a lot of memory (64-bit) to store such a color.如您所见,这样的double
可以有很多小数位,这意味着我们需要很多 memory(64 位)来存储这样的颜色。
Your function now tries to store the double
value into only 8 bit (256 values).您的 function 现在尝试将double
精度值存储为仅 8 位(256 个值)。 As we said the double
value can be seen as a portion (between 0 and 1) and the function calculates the same for 8 bit (between 0 and 255).正如我们所说, double
值可以看作是一部分(0 到 1 之间),而 function 对 8 位(0 到 255 之间)计算相同。 This can simply be done by multiplying the double
value with 255. For the example with yellow (0.33333333 of yellow is used) it is: 0.33333333 * 255 = 84,99999915
.这可以简单地通过将double
值乘以 255 来完成。对于黄色的示例(使用 0.33333333 黄色)它是: 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.意思还是一样 84,99999915 黄色部分 255 个黄色部分用到了,还是三分之一。 In order to get a compressed version of this number, it is rounded to the next integer value.为了获得这个数字的压缩版本,它被四舍五入到下一个 integer 值。 In our example, this is 85
, which is really close to the actual portion, but we save a lot of memory.在我们的示例中,这是85
,它非常接近实际部分,但我们节省了很多 memory。
It makes also sense for the lowest double
value 0.0
, which is converted to the lowest int
value 0
.对于最低的double
值0.0
也是有意义的,它被转换为最低的int
值0
。 The highest double value 1.0
is converted to 255
(highest 8-bit number).最高双精度值1.0
转换为255
(最高 8 位数)。
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.总之,我们将一个double
精度(64 位)转换为一个只有 8 位的数字,它具有相同的颜色比例,但不太准确。
Edit: As there is also a confusion with the 255: 8 bit can store 256 values (0 to 255).编辑:因为 255 也存在混淆:8 位可以存储 256 个值(0 到 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.如果您可以在某处选择 256 作为颜色值,他们会使用 1-256 范围内不带 0 的颜色值。本质上,它是相同的值被移动了 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.