Java Utililty Methods Color Create

List of utility methods to do Color Create

Description

The list of methods to do Color Create are organized into topic(s).

Method

PaintgenerateTexturePaint(String text, Font font, Color textColor, Color bgColor, int width, int height)
Generates a TexturePaint of the given size, using the given text as a pattern.
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
g2.setColor(bgColor);
g2.fillRect(0, 0, width, height);
g2.setColor(textColor);
g2.setFont(font);
FontMetrics fm = g2.getFontMetrics();
g2.drawString(text, (width - fm.stringWidth(text)) / 2,
...
Color[]generateVisuallyDistinctColors(int ncolors, float minComponent, float maxComponent)
Returns an array of ncolors RGB triplets such that each is as unique from the rest as possible and each color has at least one component greater than minComponent and one less than maxComponent.
rand.setSeed(RAND_SEED); 
float[][] yuv = new float[ncolors][3];
for (int got = 0; got < ncolors;) {
    System.arraycopy(randYUVinRGBRange(minComponent, maxComponent), 0, yuv[got++], 0, 3);
for (int c = 0; c < ncolors * 1000; c++) {
    float worst = 8888;
    int worstID = 0;
...
ColortoColor(byte red, byte green, byte blue)
Convert RGB color model to a Color object
return new Color(red, green, blue);
ColortoColor(byte[] bytes)
to Color
return new Color((int) bytes[0], (int) bytes[1], (int) bytes[2], (int) bytes[3]);
ColortoColor(final String hexColor)
Converts hex color representation to java.awt.Color object.
Color color = null;
if (hexColor.length() == ALPHA_END_INDEX) {
    final String red = hexColor.substring(RED_BEGIN_INDEX, RED_END_INDEX);
    final String green = hexColor.substring(GREEN_BEGIN_INDEX, GREEN_END_INDEX);
    final String blue = hexColor.substring(BLUE_BEGIN_INDEX, BLUE_END_INDEX);
    final String alpha = hexColor.substring(ALPHA_BEGIN_INDEX, ALPHA_END_INDEX);
    color = new Color(Integer.parseInt(red, HEX_RADIX), Integer.parseInt(green, HEX_RADIX),
            Integer.parseInt(blue, HEX_RADIX), Integer.parseInt(alpha, HEX_RADIX));
...
ColortoColor(final String hexString)
In a lot of HTML documents and still others, colors are represented using the RGB values, concatenated as hexadecimal strings.
return new Color(Character.digit(hexString.charAt(1), 16) * 16 + Character.digit(hexString.charAt(2), 16),
        Character.digit(hexString.charAt(3), 16) * 16 + Character.digit(hexString.charAt(4), 16),
        Character.digit(hexString.charAt(5), 16) * 16 + Character.digit(hexString.charAt(6), 16));
ColortoColor(final String text, final Color defaultValue)
Re-conversion of the human readable RGB representation back to a Color :
  1. if the given text matches the expected pattern 'rgb(X, X, X)', the represented Color will be returned,
  2. if the given text equals 'none', null will be returned,
  3. otherwise the provided defaultValue will be returned.
if ("none".equals(text)) {
    return null;
if (text != null) {
    final String[] colorValues = text.replaceAll("[^0-9]+", " ").trim().split(" ");
    if (colorValues.length == 3) {
        final int red = Integer.valueOf(colorValues[0]).intValue();
        final int green = Integer.valueOf(colorValues[1]).intValue();
...
ColortoColor(float[] color)
to Color
if (color.length == 4)
    return new Color(color[0], color[1], color[2], color[3]);
else
    return new Color(color[0], color[1], color[2], 1);
ColortoColor(int rgba)
to Color
int r = rgba & 0xFF, g = rgba >> 8 & 0xFF, b = rgba >> 16 & 0xFF, a = rgba >> 24 & 0xFF;
return new Color(r, g, b, a);
ColortoColor(int x)
Converts an ARGB int to a Color.
return new Color(x, true);