【发布时间】:2018-04-28 14:15:06
【问题描述】:
我得到了一些代码,用于从 rgb 值计算 R、G 和 B 值。它们看起来像这样:
public static int getR(int rgb) {
return (rgb >> 16) & 0xff;
}
public static int getG(int rgb) {
return (rgb >> 8) & 0xff;
}
public static int getB(int rgb) {
return rgb & 0xff;
}
现在我必须做以下练习。我正在更改图像的正确部分,但一切都是黑色的。
/**
* Converts a picture by dividing it in three equal parts along the X axis.
* In the first (left) part, only the red component is drawn. In the second
* (middle) part, only the green component is drawn. In the third (right) part,
* only the blue component is drawn.
*
* @param pixels The input pixels.
* @return The output pixels.
*/
public static int[][] andyWarhol(int[][] pixels) {
int i, j;
//Convert red part:
for (i = 0; i < pixels.length / 3; i++) {
for (j = 0; j < pixels[0].length; j++) {
pixels[i][j] = Colors.getR(pixels[i][j]);
}
}
//Convert yellow part:
for (i = pixels.length / 3; i < pixels.length * 2 / 3; i++) {
for (j = 0; j < pixels[0].length; j++) {
pixels[i][j] = Colors.getG(pixels[i][j]);
}
}
//Convert blue part:
for (i = pixels.length * 2 / 3; i < pixels.length; i++) {
for (j = 0; j < pixels[0].length; j++) {
pixels[i][j] = Colors.getB(pixels[i][j]);
}
}
return pixels;
}
【问题讨论】:
-
能否也提供getG()和getB()?
-
我把它加到原文里了,不知道怎么在cmets里做。我觉得 getR() 等函数的返回不是您需要写入图像数组的数字类型
-
那是你应该添加它的地方:-)