Example usage for java.awt Color getComponents

List of usage examples for java.awt Color getComponents

Introduction

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

Prototype

public float[] getComponents(float[] compArray) 

Source Link

Document

Returns a float array containing the color and alpha components of the Color , in the ColorSpace of the Color .

Usage

From source file:Main.java

public static void main(String[] args) {

    Color myColor = Color.RED;

    System.out.println(Arrays.toString(myColor.getComponents(null)));

}

From source file:ComponentTest.java

public static void main(String[] args) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, new int[] { 5, 6, 5 }, false, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);

    Color fifty = new Color(cs, new float[] { 1.0f, 1.0f, 1.0f }, 0);
    float[] components = fifty.getComponents(null);
    System.out.print("Original normalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(components[i] + " ");
    System.out.println();//  w  w  w . j a v  a 2  s .co m
    int[] unnormalized = cm.getUnnormalizedComponents(components, 0, null, 0);
    System.out.print("Original unnormalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(unnormalized[i] + " ");
    System.out.println();
    Object pixel = cm.getDataElements(unnormalized, 0, (Object) null);
    System.out.print("Pixel samples: ");
    byte[] pixelBytes = (byte[]) pixel;
    for (int i = 0; i < 3; i++)
        System.out.print(pixelBytes[i] + " ");
    System.out.println();

    unnormalized = cm.getComponents(pixel, null, 0);
    System.out.print("Derived unnormalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(unnormalized[i] + " ");
    System.out.println();
    components = cm.getNormalizedComponents(unnormalized, 0, null, 0);
    System.out.print("Derived normalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(components[i] + " ");
    System.out.println();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, new int[] { 5, 6, 5 }, false, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);

    Color fifty = new Color(cs, new float[] { 1.0f, 1.0f, 1.0f }, 0);
    float[] components = fifty.getComponents(null);
    System.out.print("Original normalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(components[i] + " ");
    System.out.println();/*from ww  w .j  av a 2s .c  o  m*/
    int[] unnormalized = cm.getUnnormalizedComponents(components, 0, null, 0);
    System.out.print("Original unnormalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(unnormalized[i] + " ");
    System.out.println();

    Object pixel = cm.getDataElements(unnormalized, 0, (Object) null);
    System.out.print("Pixel samples: ");
    byte[] pixelBytes = (byte[]) pixel;
    for (int i = 0; i < 3; i++)
        System.out.print(pixelBytes[i] + " ");
    System.out.println();

    unnormalized = cm.getComponents(pixel, null, 0);
    System.out.print("Derived unnormalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(unnormalized[i] + " ");
    System.out.println();
    components = cm.getNormalizedComponents(unnormalized, 0, null, 0);
    System.out.print("Derived normalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(components[i] + " ");
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * @param c1/*from  w  ww .  ja va2s  .co  m*/
 * @param pct1
 * @param c2
 * @param pct2
 * @return
 */
public static Color getMixedColor(Color c1, float pct1, Color c2, float pct2) {
    float[] clr1 = c1.getComponents(null);
    float[] clr2 = c2.getComponents(null);
    for (int i = 0; i < clr1.length; i++) {
        clr1[i] = (clr1[i] * pct1) + (clr2[i] * pct2);
    }
    return new Color(clr1[0], clr1[1], clr1[2], clr1[3]);
}

From source file:org.apache.fop.util.ColorUtil.java

private static Color toSRGBColor(Color color) {
    float[] comps;
    ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    comps = color.getRGBColorComponents(null);
    float[] allComps = color.getComponents(null);
    float alpha = allComps[allComps.length - 1]; //Alpha is on last component
    return new Color(sRGB, comps, alpha);
}

From source file:org.shaman.rpg.editor.textures.impl.TextureIOTest.java

private void assertColorEquals(int rgbExpected, int rgbActual, double delta) {
    Color exp = new Color(rgbExpected);
    float[] expA = exp.getComponents(null);
    Color act = new Color(rgbActual);
    float[] actA = act.getComponents(null);
    for (int i = 0; i < 3; ++i) {
        assertEquals(expA[i] * expA[3], actA[i] * actA[3], delta); //multiply with alpha
    }//  w  w w .java  2 s . c  o  m
}