0

So I need to blend 2 colors with different alpha values. Either color can have an alpha color of 0-255. Here's the algorithm I am using now and it doesn't work. If the alpha is above 1 for either color then the whole color turns solid. On a scale of 0-255 1 should be almost entirely transparent. What do I need to change in order to achieve this?

public void blend(Color color1, Color color2, double ratio) {
        float r = (float) ratio;
        float ir = (float) 1.0 - r;

        float[] rgb1 = new float[3];
        float[] rgb2 = new float[3];

        color1.getColorComponents(rgb1);
        color2.getColorComponents(rgb2);

        Color color = new Color(rgb1[0] * r + rgb2[0] * ir, rgb1[1] * r + rgb2[1] * ir, rgb1[2] * r + rgb2[2] * ir);
        this.color = color;

    }
devCorner
  • 193
  • 4
  • 15
  • Double check to make sure `ratio` is in the range from 0.0-1.0 – vandale Jul 20 '14 at 14:53
  • Just a note: In Java, you should declare your arrays with the brackets together with the type: `float[] rgb1 = new float[3]`. – Keppil Jul 20 '14 at 14:53
  • @vandale It's set to 0.5 – devCorner Jul 20 '14 at 14:55
  • For [example](http://stackoverflow.com/questions/13223065/color-fading-algorithm/13223818#13223818) and [example](http://stackoverflow.com/questions/21270610/java-smooth-color-transition/21270957#21270957) – MadProgrammer Jul 21 '14 at 01:41

1 Answers1

0

If all you need to do is have the alpha value be in the range of 0-255 instead of 0.0-1.0 , then take the alpha value and divide it by 255:

public void blend(Color color1, Color color2, int alpha) {
    float r = alpha/255.0f;
    float ir = 1.0f - alpha/255.0f;
    ...
}
vandale
  • 3,600
  • 3
  • 22
  • 39