So, I'm trying to create an image in Java where it represents every color in the RGB spectrum. However, I'm brand new in Java (just finished my first programming course, Introduction into Java) and I'm having trouble because my loops for r
, g
, and b
end up returning a color of whatever value they end at, for my code below (3,3,3)
. I need a way to return each color at each index of the picture. So at index 0
of the picture (0,0,0)
, index 1 (0,0,1)
and so on. Here's my code right now:
public static Color rainbowColor(int colorValue, int x){
Color newColor = null;
for(int r = 0; r < colorValue; r++){
for(int g = 0; g < colorValue; g++){
for(int b = 0; b < colorValue; b++){
System.out.println(x + ": " + r + "--" + g + "--" + b);
newColor = new Color(r,g,b);
}
}
}
return newColor;
}
public static Color[] rainbowPixels(int colorValue, int size){
Color[] newArray = new Color[size];
for(int x = 0; x < size; x++){
newArray[x] = Rainbow.rainbowColor(colorValue, x);
}
return newArray;
}
public static Image rainbow(){
int colorValue = 4;
int size = colorValue*colorValue*colorValue;
int width = (int)Math.sqrt(size);
int height = (int)Math.sqrt(size);
Color[] pixels = Rainbow.rainbowPixels(colorValue, size);
System.out.println(size + "," + colorValue);
return new Image(width, height, pixels);
}
Right now colorValue is set to 4 just so I'm working with a small image and running it in Powershell doesn't take forever. Also, that's all inside a class called Rainbow.java.