0

How could I apply a gradient to something like this? Just got a solid colour.

for (int i = 0; i < screen.pixels.length; i++)
    screen.pixels[i] = 0xff0000;
Boodog
  • 176
  • 1
  • 2
  • 11
  • Well, the [second example](http://stackoverflow.com/questions/32814623/fillarc-in-java-not-blending-color-as-expected/32814713#32814713) is an impleemntation of [this example](http://stackoverflow.com/questions/13223065/color-fading-algorithm/13223818#13223818) which is also used in [this example](http://stackoverflow.com/questions/21270610/java-smooth-color-transition/21270957#21270957). Basically, it's just a simple color blending algorithm which can take an array of colors and points and give a percentage value, calculate the color which should be used – MadProgrammer Apr 14 '17 at 02:29
  • instead `screen.pixels[i] = 0xff0000;` do `screen.pixels[i] = ((256*i)/screen.pixels.length)*0x10000;` or use any gradient function on R,G,B channels based on `i/screen.pixels.length` as a parameter ... like [RGB values of visible spectrum](http://stackoverflow.com/a/22681410/2521214) or [Star B-V color index to apparent RGB color](http://stackoverflow.com/a/22630970/2521214) – Spektre Apr 14 '17 at 07:17

1 Answers1

1

Solved the issue by getting the RGB values of hex codes, looping through the size of the rectangle that got the gradient, and interpolated between the two r, g, and b values.

float r = Color.decode(colourOne).getRed();
float g = Color.decode(colourOne).getGreen();
float b = Color.decode(colourOne).getBlue();
float r2 = Color.decode(colourTwo).getRed();
float g2 = Color.decode(colourTwo).getGreen();
float b2 = Color.decode(colourTwo).getBlue();
float interp, newR, newG, newB;

for (int x = 0; x < width; x++)
{
    for (int y = 0; y < height; y++)
    {
        interp = (float) (x + y) / (float) (width + height);
        newR = r * (1 - interp) + r2 * interp;
        newG = g * (1 - interp) + g2 * interp;
        newB = b * (1 - interp) + b2 * interp;

        pixels[x + y * width] = (int) (Math.floor(newR) * 0x10000 + Math.floor(newG) * 0x100 + Math.floor(newB));
    }
}
Boodog
  • 176
  • 1
  • 2
  • 11