Java Random Color makeRandomColor(Color color)

Here you can find the source of makeRandomColor(Color color)

Description

Generates a random Color by scaling each of the red, green and blue components of a specified color with independent random numbers.

License

Open Source License

Parameter

Parameter Description
color the color to generate a random color from. If null, the color white (0x000000aa) is used.

Return

a new color with random red, green and blue components.

Declaration

public static Color makeRandomColor(Color color) 

Method Source Code


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

public class Main {
    /**/*from   w  ww  . j  a  va 2s .c  o  m*/
     * Generates a random {@link Color} by scaling each of the red, green and blue components of a specified color with
     * independent random numbers. The alpha component is not scaled and is copied to the new color. The returned color
     * can be any value between white (0x000000aa) and black (0xffffffaa).
     * <p/>
     * Unless there's a reason to use a specific input color, the best color to use is white.
     *
     * @param color the color to generate a random color from. If null, the color white (0x000000aa) is used.
     *
     * @return a new color with random red, green and blue components.
     */
    public static Color makeRandomColor(Color color) {
        if (color == null)
            color = Color.WHITE;

        float[] cc = color.getRGBComponents(null);

        return new Color(cc[0] * (float) Math.random(), cc[1] * (float) Math.random(),
                cc[2] * (float) Math.random(), cc[3]);
    }

    /**
     * Generates a random {@link Color} by scaling each of the red, green and blue components of a specified color with
     * independent random numbers. The alpha component is not scaled and is copied to the new color. The returned color
     * can be any value between white (0x000000aa) and a specified darkest color.
     * <p/>
     * Unless there's a reason to use a specific input color, the best color to use is white.
     *
     * @param color        the color to generate a random color from. If null, the color white (0x000000aa) is used.
     * @param darkestColor the darkest color allowed. If any of the generated color's components are less than the
     *                     corresponding component in this color, new colors are generated until one satisfies this
     *                     requirement, up to the specified maximum number of attempts.
     * @param maxAttempts  the maximum number of attempts to create a color lighter than the specified darkestColor. If
     *                     this limit is reached, the last color generated is returned.
     *
     * @return a new color with random red, green and blue components.
     */
    public static Color makeRandomColor(Color color, Color darkestColor, int maxAttempts) {
        Color randomColor = makeRandomColor(color);

        if (darkestColor == null)
            return randomColor;

        float[] dc = darkestColor.getRGBComponents(null);

        float[] rc = randomColor.getRGBComponents(null);
        for (int i = 0; i < (maxAttempts - 1) && (rc[0] < dc[0] || rc[1] < dc[1] || rc[2] < dc[2]); i++) {
            rc = randomColor.getRGBComponents(null);
        }

        return randomColor;
    }
}

Related

  1. getRandomHexColor()
  2. getRandomIntColor()
  3. getRandomRgb()
  4. makeRandomColor()
  5. makeRandomColor()
  6. makeRandomColor(Color color, Color darkestColor, int maxAttempts)
  7. randomBrown()
  8. randomColor()
  9. randomColor()