get Random Color by base color - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

get Random Color by base color

Demo Code


//package com.java2s;
import java.awt.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getRandomColor());
    }/*from ww w . ja  va 2  s .  c o m*/

    private static int INSIDE_CLUSTER_DELTA = 15;

    public static Color getRandomColor() {
        int red = (int) (Math.random() * 255);
        int green = (int) (Math.random() * 255);
        int blue = (int) (Math.random() * 255);
        return new Color(red, green, blue);
    }

    public static Color getRandomColor(Color baseColor, int count) {
        int realm = count * INSIDE_CLUSTER_DELTA;
        int red = (int) ((baseColor.getRed() - (realm / 2)) + (Math
                .random() * realm));
        int green = (int) ((baseColor.getGreen() - (realm / 2)) + (Math
                .random() * realm));
        int blue = (int) ((baseColor.getBlue() - (realm / 2)) + (Math
                .random() * realm));
        return new Color(red, green, blue);
    }
}

Related Tutorials