Method returning color between start and end color proportional to given values. - Android Graphics

Android examples for Graphics:Color Value

Description

Method returning color between start and end color proportional to given values.

Demo Code


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

public class Main {
    /**/*w w  w.  j  ava 2 s . c o  m*/
     * Method returning color between start and end color proportional to given values.
     *
     * @param colorStart start color
     * @param colorEnd   end color
     * @param fullValue  total value
     * @param partValue  part of fullValue. When partValue equals 0, returning color is colorStart,
     *                   when partValue is fullValue returning color is endColor. Otherwise returning
     *                   color is from between those, relative to partValue/fullValue ratio.
     * @return color from between start and end color relative to partValue/fullValue ratio.
     */
    public static int getProportionalColor(int colorStart, int colorEnd,
            float fullValue, float partValue) {
        float progress = Math.min(Math.max(partValue, 0f), fullValue)
                / fullValue;
        return Color.argb(
                Math.round(Color.alpha(colorStart) * (1 - progress)
                        + Color.alpha(colorEnd) * progress),
                Math.round(Color.red(colorStart) * (1 - progress)
                        + Color.red(colorEnd) * progress),
                Math.round(Color.green(colorStart) * (1 - progress)
                        + Color.green(colorEnd) * progress),
                Math.round(Color.blue(colorStart) * (1 - progress)
                        + Color.blue(colorEnd) * progress));
    }
}

Related Tutorials