Example usage for java.awt Color getRGB

List of usage examples for java.awt Color getRGB

Introduction

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

Prototype

public int getRGB() 

Source Link

Document

Returns the RGB value representing the color in the default sRGB ColorModel .

Usage

From source file:Main.java

public static void main(String[] args) {
    Color myColor = Color.RED;

    System.out.println(myColor.getRGB());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage inputFile = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));

    for (int x = 0; x < inputFile.getWidth(); x++) {
        for (int y = 0; y < inputFile.getHeight(); y++) {
            int rgba = inputFile.getRGB(x, y);
            Color col = new Color(rgba, true);
            col = new Color(255 - col.getRed(), 255 - col.getGreen(), 255 - col.getBlue());
            inputFile.setRGB(x, y, col.getRGB());
        }/*w  ww  .  j ava 2 s  .co m*/
    }

    File outputFile = new File("invert.png");
    ImageIO.write(inputFile, "png", outputFile);

}

From source file:Main.java

public static Color fixSeveralColorRenderingIssues(Color color) {
    return new Color(color.getRGB());
}

From source file:Main.java

public static String convertColorToHexString(java.awt.Color c) {
    String str = Integer.toHexString(c.getRGB() & 0xFFFFFF);
    return ("#" + "000000".substring(str.length()) + str.toUpperCase());
}

From source file:Main.java

public static void writeColorAttr(Element element, String attributeName, Color value) {
    element.setAttribute(attributeName, String.format("#%x", value.getRGB() & 0xffffff));
}

From source file:ColorUtils.java

private static String toHexString(Color color) {
    return Integer.toHexString(color.getRGB()).substring(2);
}

From source file:com.gmind7.bakery.websocket.SnakeWebSocketHandler.java

public static String getRandomHexColor() {
    float hue = random.nextFloat();
    // sat between 0.1 and 0.3
    float saturation = (random.nextInt(2000) + 1000) / 10000f;
    float luminance = 0.9f;
    Color color = Color.getHSBColor(hue, saturation, luminance);
    return '#' + Integer.toHexString((color.getRGB() & 0xffffff) | 0x1000000).substring(1);
}

From source file:ch.rasc.s4ws.snake.SnakeWebSocketHandler.java

public static String getRandomHexColor() {
    float hue = random.nextFloat();
    // sat between 0.1 and 0.3
    float saturation = (random.nextInt(2000) + 1000) / 10000f;
    float luminance = 0.9f;
    Color color = Color.getHSBColor(hue, saturation, luminance);
    return '#' + Integer.toHexString(color.getRGB() & 0xffffff | 0x1000000).substring(1);
}

From source file:it.gualtierotesta.gdocx.GFactory.java

/**
 * Convert java.awt.Color in hex string code (RRGGBB)
 *
 * @param color the color to be converted
 * @return a string with color hex code//from   w w  w . ja  v  a  2 s  .c  om
 */
public static String color2hex(@Nonnull final Color color) {

    Validate.notNull(color, "Color not valid");

    final String rgb = Integer.toHexString(color.getRGB());
    return rgb.substring(2, rgb.length());
}

From source file:Main.java

public static byte[] getCompressedImage(byte[] rgb) {

    BufferedImage image;// w  w w.jav a2s .  c o  m
    int width;
    int height;

    try {
        image = ImageIO.read(new ByteArrayInputStream(rgb));
        width = image.getWidth();
        height = image.getHeight();

        for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                Color c = new Color(image.getRGB(j, i));
                int red = (int) (c.getRed() * 0.299);
                int green = (int) (c.getGreen() * 0.587);
                int blue = (int) (c.getBlue() * 0.114);
                Color newColor = new Color(red + green + blue,

                        red + green + blue, red + green + blue);

                image.setRGB(j, i, newColor.getRGB());
            }
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();

        return imageInByte;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}