Gets a random darker color. - Android Graphics

Android examples for Graphics:Color Darken

Description

Gets a random darker color.

Demo Code


//package com.java2s;
import android.graphics.Color;
import java.util.Random;

public class Main {
    private static Random randomGenerator = new Random();

    /**/*from  ww  w  .j  a  v  a 2  s.  c  om*/
     * Gets a random darker color.
     * @return
     */
    public static int getRandomDarkColor() {
        int randColor = getRandomColor();
        return darker(randColor, 0.75f);
    }

    /**
     * Returns a random color.
     * @return
     */
    public static int getRandomColor() {
        return Color.rgb(randomGenerator.nextInt(255),
                randomGenerator.nextInt(255), randomGenerator.nextInt(255));
    }

    /**
     * Retuns a darker color from a specified color by the factor.
     * @param color
     * @param factor
     * @return
     */
    public static int darker(int color, float factor) {
        int a = Color.alpha(color);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);

        return Color.argb(a, Math.max((int) (r * factor), 0),
                Math.max((int) (g * factor), 0),
                Math.max((int) (b * factor), 0));
    }
}

Related Tutorials