I’m trying to make a gradient effect. I want to make a method that returns returns a color for a position on a gradient. So for example, if variable “x” can be anything from 0 to 9, if x was 0 I might want the method to return black. If x was 9, I might want it to return white, and if x was 2 I mighy want the function to return a dark gray. The colors don’t have to be white and black, but any two colors. (The method would return a “Color” from the Java Color class)
I tried to make a weighted average for each rgb value and put them back together, but it doesn’t work properly. The colors don’t mix properly
Color gradientColor(float x){
int r = (int) (x*color1.getRed() + (length-x)*color2.getRed())/length;
int g = (int) (x*color1.getGreen() + (length-x)*color2.getGreen())/length;
int b = (int) (x*color1.getBlue() + (length-x)*color2.getBlue())/length;
return new Color(r*0xffff+g*0xff+b);
}
“length” is the length of the gradient, which was 9 in the above example, and color1 might be white and color2 might be black
Thanks