Example usage for java.awt Color Color

List of usage examples for java.awt Color Color

Introduction

In this page you can find the example usage for java.awt Color Color.

Prototype

public Color(ColorSpace cspace, float[] components, float alpha) 

Source Link

Document

Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.

Usage

From source file:Main.java

public static Color getBackgroundColor() {
    if (backgroundColor != null) {
        return backgroundColor;
    }/*from   ww w  . j a  v a 2s . c om*/

    backgroundColor = UIManager.getColor("Table.background");
    if (backgroundColor == null) {
        backgroundColor = new JList().getBackground();
    }
    //sometimes the UIManager color won't work
    backgroundColor = new Color(backgroundColor.getRed(), backgroundColor.getGreen(),
            backgroundColor.getBlue());

    return backgroundColor;
}

From source file:Main.java

public static Color getForegroundColor() {
    if (foregroundColor != null) {
        return foregroundColor;
    }/* ww w  . j  av  a  2s .  c o  m*/

    foregroundColor = UIManager.getColor("Table.foreground");
    if (foregroundColor == null) {
        foregroundColor = new JList().getForeground();
    }

    //sometimes the UIManager color won't work
    foregroundColor = new Color(foregroundColor.getRed(), foregroundColor.getGreen(),
            foregroundColor.getBlue());

    //sometimes the UIManager color won't work
    return foregroundColor;
}

From source file:Main.java

public static Color getColorProperty(Element properties, String name) {
    String[] rgb = getPropertyNode(properties, name).getAttributes().getNamedItem(COLOR_ATTR).getNodeValue()
            .split(",");
    return new Color(new Integer(rgb[0]), new Integer(rgb[1]), new Integer(rgb[2]));
}

From source file:Main.java

/**
 * Constructs a rgb value from given document
 * @param elem/*  w  ww . j a v  a 2 s . c o m*/
 * @return
 */
public static Color getRGB(Element elem) {
    int r = Integer.parseInt(elem.getElementsByTagName(TAG_NAME_RED).item(0).getTextContent());
    int g = Integer.parseInt(elem.getElementsByTagName(TAG_NAME_GREEN).item(0).getTextContent());
    int b = Integer.parseInt(elem.getElementsByTagName(TAG_NAME_BLUE).item(0).getTextContent());
    return new Color(r, g, b);

}

From source file:ColorUtils.java

/**
 * Darkens a color by a given amount/*from  ww w.  ja v a 2 s  . c om*/
 * 
 * @param color The color to darken
 * @param amount The amount to darken the color. 0 will leave the color unchanged; 1 will make
 *        the color completely black
 * @return The stained color
 */
public static Color stain(Color color, float amount) {
    int red = (int) ((color.getRed() * (1 - amount) / 255) * 255);
    int green = (int) ((color.getGreen() * (1 - amount) / 255) * 255);
    int blue = (int) ((color.getBlue() * (1 - amount) / 255) * 255);
    return new Color(red, green, blue);
}

From source file:ColorUtils.java

/**
 * Lightens a color by a given amount/*  w  ww. j  ava2s.  c o  m*/
 * 
 * @param color The color to lighten
 * @param amount The amount to lighten the color. 0 will leave the color unchanged; 1 will make
 *        the color completely white
 * @return The bleached color
 */
public static Color bleach(Color color, float amount) {
    int red = (int) ((color.getRed() * (1 - amount) / 255 + amount) * 255);
    int green = (int) ((color.getGreen() * (1 - amount) / 255 + amount) * 255);
    int blue = (int) ((color.getBlue() * (1 - amount) / 255 + amount) * 255);
    return new Color(red, green, blue);
}

From source file:Main.java

public static Color rgbStringToColor(String rgbString) {
    String rgb[] = rgbString.split(",");

    if (rgb.length == 3) {
        return new Color(parseSingleChanel(rgb[0]), parseSingleChanel(rgb[1]), parseSingleChanel(rgb[2]));
    } else if (rgb.length == 4) {
        return new Color(parseSingleChanel(rgb[0]), parseSingleChanel(rgb[1]), parseSingleChanel(rgb[2]),
                parseSingleChanel(rgb[3]));
    }//from w w w. j ava  2s  .  c o m

    throw new RuntimeException("invalid rgb color specification: " + rgbString);
}

From source file:Main.java

/**
 * Create a darker color using a factor.
 *
 * @param color//  ww  w. j ava  2s.  com
 * @param factor
 * @return darker color
 */
public static Color darker(final Color color, float factor) {
    return new Color(Math.max((int) (color.getRed() * factor), 0),
            Math.max((int) (color.getGreen() * factor), 0), Math.max((int) (color.getBlue() * factor), 0));
}

From source file:Main.java

/**
 * Create a lighter color using a factor.
 *
 * @param color/* w w  w.  ja  v  a 2  s.c  om*/
 * @param factor
 * @return lighter color
 */
public static Color lighter(final Color color, float factor) {
    return new Color(Math.max((int) (color.getRed() / factor), 0),
            Math.max((int) (color.getGreen() / factor), 0), Math.max((int) (color.getBlue() / factor), 0));
}

From source file:ColorUtils.java

/**
 * Parses a java.awt.Color from an HTML color string in the form '#RRGGBB' where RR, GG, and BB
 * are the red, green, and blue bytes in hexadecimal form
 * //from   ww w .  j  a va  2 s .  c  o m
 * @param htmlColor The HTML color string to parse
 * @return The java.awt.Color represented by the HTML color string
 */
public static Color fromHTML(String htmlColor) {
    int r, g, b;
    if (htmlColor.length() != 7 || htmlColor.charAt(0) != '#')
        throw new IllegalArgumentException(htmlColor + " is not an HTML color string");
    r = Integer.parseInt(htmlColor.substring(1, 3), 16);
    g = Integer.parseInt(htmlColor.substring(3, 5), 16);
    b = Integer.parseInt(htmlColor.substring(5, 7), 16);
    return new Color(r, g, b);
}