Android Color Average averageBlendTwoColors(int c1, int c2)

Here you can find the source of averageBlendTwoColors(int c1, int c2)

Description

Blend of color c1 and c2 by adding the components and dividing the results with 2.

License

Open Source License

Parameter

Parameter Description
c1 first color to blend.
c2 second color to blend.

Return

the result of blending color c1 and c2.

Declaration

public static int averageBlendTwoColors(int c1, int c2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import android.graphics.Color;

public class Main {
    /**//  w ww . j ava 2s .  com
     * Max value for a byte, used for limiting a channels max value.
     */
    public static final int CHANNEL_MAX = 0xff;

    /**
     * Blend of color c1 and c2 by adding the components and dividing the
     * results with 2.
     *
     * @param c1 first color to blend.
     * @param c2 second color to blend.
     * @return the result of blending color c1 and c2.
     */
    public static int averageBlendTwoColors(int c1, int c2) {
        int a1 = Color.alpha(c1);
        int r1 = Color.red(c1);
        int g1 = Color.green(c1);
        int b1 = Color.blue(c1);

        int a2 = Color.alpha(c2);
        int r2 = Color.red(c2);
        int g2 = Color.green(c2);
        int b2 = Color.blue(c2);

        int a = Math.min((a1 + a2) >> 1, CHANNEL_MAX);
        int r = Math.min((r1 + r2) >> 1, CHANNEL_MAX);
        int g = Math.min((g1 + g2) >> 1, CHANNEL_MAX);
        int b = Math.min((b1 + b2) >> 1, CHANNEL_MAX);
        int c = Color.argb(a, r, g, b);

        return c;
    }
}

Related

  1. average(int color, int color2)
  2. multiplyBlendTwoColors(int c1, int c2)