get Middle Color between two color values and factor - Android Graphics

Android examples for Graphics:Color Value

Description

get Middle Color between two color values and factor

Demo Code


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

public class Main {
    public static int getMiddleColor(int prevColor, int curColor,
            float factor) {
        if (prevColor == curColor)
            return curColor;

        if (factor == 0f)
            return prevColor;
        else if (factor == 1f)
            return curColor;

        int a = getMiddleValue(Color.alpha(prevColor),
                Color.alpha(curColor), factor);
        int r = getMiddleValue(Color.red(prevColor), Color.red(curColor),
                factor);/*w w  w. jav  a  2 s .  c  o  m*/
        int g = getMiddleValue(Color.green(prevColor),
                Color.green(curColor), factor);
        int b = getMiddleValue(Color.blue(prevColor), Color.blue(curColor),
                factor);

        return Color.argb(a, r, g, b);
    }

    private static int getMiddleValue(int prev, int next, float factor) {
        return Math.round(prev + (next - prev) * factor);
    }
}

Related Tutorials