Java by API/java.awt.image/ColorModel

Материал из Java эксперт
Перейти к: навигация, поиск

ColorModel: getComponents(int pixel, int[] components, int offset)

import java.awt.Color;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
public class MainClass {
  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]);
    }
    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();
    }
  }
}





ColorModel: getDataElements(int rgb, Object pixel)

import java.awt.Color;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
public class MainClass {
  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]);
    }
    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();
    }
  }
}





ColorModel: getNormalizedComponents(int[] components, int offset, float[] normComponents, int normOffset)

import java.awt.Color;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.ColorModel;
import java.awt.image.ruponentColorModel;
import java.awt.image.DataBuffer;
public class MainClass {
  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();
    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] + " ");
  }
}





ColorModel: getUnnormalizedComponents(float[] normComponents, int normOffset, int[] components, int offset)

import java.awt.Color;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.ColorModel;
import java.awt.image.ruponentColorModel;
import java.awt.image.DataBuffer;
public class MainClass {
  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();
    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] + " ");
  }
}