2

I want to combine the values of two colors. For example:

// color 1:
int aa = 255;
int rr = 255;
int gg = 0;
int bb = 0;

// color 2:
int aa = 128; 
int rr = 0; 
int gg = 0; 
int bb = 0;

The first color is pure red at 100% alpha. The second color is black at 50% alpha. I want to find the final combined color, as if I layered the second color on top of the first, as you could do in photoshop via layers.

What is the formula for doing this? I'm not sure of the correct terminology here, so getting search results that I don't think are what I want.

Thanks

user3203425
  • 2,919
  • 4
  • 29
  • 48
  • 3
    [Maybe this will help](http://stackoverflow.com/questions/9352124/computing-the-combined-color-of-two-colors-over-operator) – gtgaxiola Jun 12 '14 at 13:28
  • @gtgaxiola I used the solution posted in your link. – user3203425 Jun 12 '14 at 21:25
  • @gtgaxiola has the right answer, Porter and Duff's "DST_OVER" operator. Use the top image's alpha to blend the top image values over the bottom image values. – Dithermaster Jun 14 '14 at 20:37

1 Answers1

0

Have yo tried adding the two colors to see if the results is what you expect?

Class Color
{
int aa ;
int rr ;
int gg ;
int bb;
}

 Color Combined( Color a, Color b)
 {
    Color result=new Color();
    result.aa=(int)((a.aa+b.aa)/2);
    result.rr=(int)( (a.rr+b.rr)/2);
    result.gg=(int)( (a.gg+b.gg)/2);
    result.bb=(int)( (a.bb+b.bb)/2);
    return result;
 }
apomene
  • 14,282
  • 9
  • 46
  • 72
  • This is not correct; it is averaging the alpha and should instead use the top image alpha to blend the top image over the bottom image. – Dithermaster Jun 14 '14 at 20:34