Java ColorSpace CS_sRGB

PreviousNext

Java ColorSpace CS_sRGB The sRGB color space defined at http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html.

Syntax

The field CS_sRGB() from ColorSpace is declared as:

@Native
    public static final int CS_sRGB = 1000;

Example

The following code shows how to use Java ColorSpace.CS_sRGB

Example 1

import java.awt.Color;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;

public class ComponentTest {
    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  .   de  m   o  2  s .   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] + " ");
        System.out.println();
    }
}

Result

Example 2

import java.awt.Color;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;

public class Main {
    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();/*w   w w  .  d    e  m o  2  s  .   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] + " ");
    }

}

Result

PreviousNext

Related