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

Android examples for Graphics:Color Blend

Description

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

Demo Code


//package com.java2s;
import android.graphics.Color;

public class Main {
    /**//from  w ww .  j  a v  a 2 s  .  c o m
     * 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 Tutorials