0

我无法理解以下代码:

int pixel = img.pixels[i];

    // println("Working on pixel " + i + " out of " + img.pixels.length); 

    int red = (int) red(pixel);
    int green = (int) green(pixel);
    int blue = (int) blue(pixel);

我认为img.pixels[]给出了一个color价值。那么它是如何存储在 an 中int,然后由 、 和 函数从中提取red()blue()green()

4

2 回答 2

1

color in Processing is stored as a plain 32-bit int. Each 8 bits (256 range) contains the value for ARGB (a = alpha), some ting like AAAARRRRGGGGBBBB, those when printed as an int gives weird results. red(), green() and blue() extracts the components from the int. In the reference for them there is the alternate (faster) method using bit shift operations, for instance:

red(c) = c >> 16 & 0xFF;

check the wiki entrance linked above.

于 2013-07-16T17:26:21.670 回答
0

您习惯使用的颜色值是颜色的十六进制版本,其中红绿蓝代表 RGB,这是另一种颜色格式,与 CMYK 一样

例如 0 red 0 green 和 0 blue 给黑色与 #000000 或 256 256 256 给你白色相同 #FFFFFF

我还找到了一些将 rgb 转换为 hex 的示例 JavaScript。见下文:

function rgbToHex(R,G,B) { return toHex(R)+toHex(G)+toHex(B); }
function toHex(n) {
    n = parseInt(n,10);
    if (isNaN(n)) return "00";
    n = Math.max(0,Math.min(n,255));
    return "0123456789ABCDEF".charAt((n-n%16)/16)
        + "0123456789ABCDEF".charAt(n%16);
}
于 2013-07-16T17:13:42.007 回答