组合/混合两个ARGB(alpha - 红色,绿色)值

时间:2015-10-04 09:45:22

标签: colors alpha argb

我一直在努力将两个ARGB值结合起来 - 尽管有很多阅读,例如这里...... How to mix two ARGB pixels?

我没有得到我期待的结果 - 或者我所期待的是错误的;如果有人可以提出这样做​​的功能,那就太好了。

我想合并:

  1. ColorA(红色= 255,绿色= 255,蓝色= 0,alpha = 100)

  2. ColorB(红色= 0,绿色= 0,蓝色= 100,alpha = 200)

  3. 以红色,绿色,蓝色,Alpha格式输出值。

    我期待输出如下(绿色中央色):

    enter image description here

    非常感谢帮助这个愚蠢的程序员!

1 个答案:

答案 0 :(得分:0)

像大多数问题一样,最终会回答一个问题:

public static int MergeColors(int backgroundColor, int foregroundColor) {
    final byte ALPHA_CHANNEL = 24;
    final byte RED_CHANNEL   = 16;
    final byte GREEN_CHANNEL =  8;
    final byte BLUE_CHANNEL  =  0;

    final double ap1 = (double)(backgroundColor >> ALPHA_CHANNEL & 0xff) / 255d;
    final double ap2 = (double)(foregroundColor >> ALPHA_CHANNEL & 0xff) / 255d;
    final double ap = ap2 + (ap1 * (1 - ap2));

    final double amount1 = (ap1 * (1 - ap2)) / ap;
    final double amount2 = amount1 / ap;

    int a = ((int)(ap * 255d)) & 0xff;

    int r = ((int)(((float)(backgroundColor >> RED_CHANNEL & 0xff )*amount1) +
            ((float)(foregroundColor >> RED_CHANNEL & 0xff )*amount2))) & 0xff;
    int g = ((int)(((float)(backgroundColor >> GREEN_CHANNEL & 0xff )*amount1) +
            ((float)(foregroundColor >> GREEN_CHANNEL & 0xff )*amount2))) & 0xff;
    int b = ((int)(((float)(backgroundColor & 0xff )*amount1) +
            ((float)(foregroundColor & 0xff )*amount2))) & 0xff;

    return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL;
}

来源:Color mixing in android