Get random color using Golden Ratio - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Get random color using Golden Ratio

Demo Code


import java.awt.Color;
import java.util.Random;

public class Main{
    /**Get random color using Golden Ratio*/
    public static Color getRandomColorWithGoldenRatio(Random parRandom,
            float parSaturation, float parBrightness) {
        float goldenRatioConj = 0.618033988749895F;
        float hue = parRandom.nextFloat();
        hue += goldenRatioConj;//from   w ww  . j a  va 2s .  c  om
        hue %= 1;
        return getColor(HSBtoRGB(hue, parSaturation,
                parBrightness));
    }
    /**Get Color from hex*/
    public static Color getColor(int parHex) {
        return new Color(getRed(parHex), getGreen(parHex), getBlue(parHex));
    }
    /**Get HSB from RGB*/
    public static int HSBtoRGB(float parHue, float parSaturation,
            float parBrightness) {
        return Color.HSBtoRGB(parHue, parSaturation, parBrightness);
    }
    /**Get red from hex*/
    public static int getRed(int parHex) {
        return (parHex >> 16) & 0xFF;
    }
    /**Get green from hex*/
    public static int getGreen(int parHex) {
        return (parHex >> 8) & 0xFF;
    }
    /**Get blue from hex*/
    public static int getBlue(int parHex) {
        return parHex & 0xFF;
    }
}

Related Tutorials