Example usage for java.awt.image ColorModel getDataElements

List of usage examples for java.awt.image ColorModel getDataElements

Introduction

In this page you can find the example usage for java.awt.image ColorModel getDataElements.

Prototype

public Object getDataElements(int rgb, Object pixel) 

Source Link

Document

Returns a data element array representation of a pixel in this ColorModel , given an integer pixel representation in the default RGB color model.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    byte ff = (byte) 0xff;
    byte[] r = { ff, 0, 0, ff, 0 };
    byte[] g = { 0, ff, 0, ff, 0 };
    byte[] b = { 0, 0, ff, ff, 0 };

    ColorModel cm = new IndexColorModel(3, 5, r, g, b);

    Color[] colors = { new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255) };

    for (int i = 0; i < colors.length; i++) {
        int rgb = colors[i].getRGB();
        Object pixel = cm.getDataElements(rgb, null);
        System.out.println(colors[i] + " -> " + ((byte[]) pixel)[0]);
    }/*w w w.  j  a  v a 2  s .  c  om*/

    for (byte i = 0; i < 5; i++) {
        int[] unnormalizedComponents = cm.getComponents(i, null, 0);
        System.out.print(i + " -> unnormalized components");
        for (int j = 0; j < unnormalizedComponents.length; j++)
            System.out.print(" " + unnormalizedComponents[j]);
        System.out.println();
    }
}

From source file:Main.java

public static void main(String[] args) {
    byte ff = (byte) 0xff;
    byte[] r = { ff, 0, 0, ff, 0 };
    byte[] g = { 0, ff, 0, ff, 0 };
    byte[] b = { 0, 0, ff, ff, 0 };

    ColorModel cm = new IndexColorModel(3, 5, r, g, b);

    Color[] colors = { new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255), new Color(64, 255, 64),
            new Color(255, 255, 0), new Color(0, 255, 255) };

    for (int i = 0; i < colors.length; i++) {
        float[] normalizedComponents = colors[i].getComponents(null);
        int[] unnormalizedComponents = cm.getUnnormalizedComponents(normalizedComponents, 0, null, 0);
        int rgb = colors[i].getRGB();
        Object pixel = cm.getDataElements(rgb, null);
        System.out.println(colors[i] + " -> " + ((byte[]) pixel)[0]);
    }/* w  w w  . j  ava2s. c om*/

    for (byte i = 0; i < 5; i++) {
        int[] unnormalizedComponents = cm.getComponents(i, null, 0);
        System.out.print(i + " -> unnormalized components");
        for (int j = 0; j < unnormalizedComponents.length; j++)
            System.out.print(" " + unnormalizedComponents[j]);
        System.out.println();
    }
}

From source file:RasterImageTest.java

/**
 * Makes the Mandelbrot image.//from  www. ja  v a 2 s . c o  m
 * @param width the width
 * @parah height the height
 * @return the image
 */
public BufferedImage makeMandelbrot(int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = image.getRaster();
    ColorModel model = image.getColorModel();

    Color fractalColor = Color.red;
    int argb = fractalColor.getRGB();
    Object colorData = model.getDataElements(argb, null);

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            double a = XMIN + i * (XMAX - XMIN) / width;
            double b = YMIN + j * (YMAX - YMIN) / height;
            if (!escapesToInfinity(a, b))
                raster.setDataElements(i, j, colorData);
        }
    return image;
}