Blend of color c1 and c2 - Android Graphics

Android examples for Graphics:Color Blend

Description

Blend of color c1 and c2

Demo Code


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

public class Main {
    /**//from ww w . j  a v a 2 s.c  om
     * 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 applying f(c1, c2) = 1 - (1 - c1) * (1 - c2)
     * per channel.
     *
     * @param c1 first color to blend.
     * @param c2 second color to blend.
     * @return the result of blending color c1 and c2.
     */
    public static int screenBlendTwoColors(int c1, int c2) {
        double a1 = Color.alpha(c1) / ((double) CHANNEL_MAX);
        double r1 = Color.red(c1) / ((double) CHANNEL_MAX);
        double g1 = Color.green(c1) / ((double) CHANNEL_MAX);
        double b1 = Color.blue(c1) / ((double) CHANNEL_MAX);

        double a2 = Color.alpha(c2) / ((double) CHANNEL_MAX);
        double r2 = Color.red(c2) / ((double) CHANNEL_MAX);
        double g2 = Color.green(c2) / ((double) CHANNEL_MAX);
        double b2 = Color.blue(c2) / ((double) CHANNEL_MAX);

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

        return c;
    }
}

Related Tutorials