get Middle Color - Android Graphics

Android examples for Graphics:Color RGB Value

Description

get Middle Color

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;
        }//from w w  w.j a v  a2s  .  c o m
        ;

        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);
        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