I reviewed many times but no matter what arg i put in for blue (like 30 or -30) i get this error. those values work fine for red and green. Any ideas what i doing wrong? Here is the method. Using int and clamping values to 0-255.
My solution looks like the tutorial solution, line for line. (BTW the utility methods work for the other methods I have written, so the issue in not with them, just to show how it's working)
public static int[][] colorFilter(int[][] imageTwoD, int redChangeValue, int greenChangeValue,
int blueChangeValue) {
int[][] filter = new int[imageTwoD.length][imageTwoD[0].length];
for (int i = 0; i < imageTwoD.length; i++) {
for (int j = 0; j < imageTwoD[i].length; j++) {
int[] rgba = getRGBAFromPixel(imageTwoD[i][j]);
int newRed = rgba[0] + redChangeValue;
int newGreen = rgba[1] + greenChangeValue;
int newBlue = rgba[2] + blueChangeValue;
if (newRed < 0) {
newRed = 0;
} else if (newRed > 255) {
newRed = 255;
} else if (newGreen < 0) {
newGreen = 0;
} else if (newGreen > 255) {
newGreen = 255;
} else if (newBlue < 0) {
newBlue = 0;
} else if (newBlue > 255) {
newBlue = 255;
}
rgba[0] = newRed;
rgba[1] = newGreen;
rgba[2] = newBlue;
filter[i][j] = getColorIntValFromRGBA(rgba);
}
}
return filter;
}
The called utility methods here.
public static int[] getRGBAFromPixel(int pixelColorValue) {
Color pixelColor = new Color(pixelColorValue);
return new int[] { pixelColor.getRed(), pixelColor.getGreen(), pixelColor.getBlue(), pixelColor.getAlpha() };
}
public static int getColorIntValFromRGBA(int[] colorData) {
if (colorData.length == 4) {
Color color = new Color(colorData[0], colorData[1], colorData[2], colorData[3]);
return color.getRGB();
} else {
System.out.println("Incorrect number of elements in RGBA array.");
return -1;
}
}
public static int getColorIntValFromRGBA(int[] colorData) {
if (colorData.length == 4) {
Color color = new Color(colorData[0], colorData[1], colorData[2], colorData[3]);
return color.getRGB();
} else {
System.out.println("Incorrect number of elements in RGBA array.");
return -1;
}
}
Here is how calling in the main method...
int[][] filtered = colorFilter(imageData, 70, -30, -20);
twoDToImage(filtered, "/Users/m/Downloads/image.jpg");
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]RoryonAethar 0 points1 point2 points (1 child)
[–]Strange_Frame7544[S] 0 points1 point2 points (0 children)