home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Color.java < prev    next >
Text File  |  1996-05-03  |  11KB  |  391 lines

  1. /*
  2.  * @(#)Color.java    1.15 96/03/28 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.io.*;
  22. import java.lang.*;
  23.  
  24. /**
  25.  * A class to encapsulate RGB Colors.
  26.  *
  27.  * @version     1.15, 28 Mar 1996
  28.  * @author     Sami Shaio
  29.  * @author     Arthur van Hoff
  30.  */
  31. public final class Color {
  32.     
  33.     /**
  34.      * The color white.
  35.      */
  36.     public final static Color white     = new Color(255, 255, 255);
  37.  
  38.     /**
  39.      * The color light gray.
  40.      */
  41.     public final static Color lightGray = new Color(192, 192, 192);
  42.  
  43.     /**
  44.      * The color gray.
  45.      */
  46.     public final static Color gray     = new Color(128, 128, 128);
  47.  
  48.     /**
  49.      * The color dark gray.
  50.      */
  51.     public final static Color darkGray     = new Color(64, 64, 64);
  52.  
  53.     /**
  54.      * The color black.
  55.      */
  56.     public final static Color black     = new Color(0, 0, 0);
  57.     
  58.     /**
  59.      * The color red.
  60.      */
  61.     public final static Color red     = new Color(255, 0, 0);
  62.  
  63.     /**
  64.      * The color pink.
  65.      */
  66.     public final static Color pink     = new Color(255, 175, 175);
  67.  
  68.     /**
  69.      * The color orange.
  70.      */
  71.     public final static Color orange     = new Color(255, 200, 0);
  72.  
  73.     /**
  74.      * The color yellow.
  75.      */
  76.     public final static Color yellow     = new Color(255, 255, 0);
  77.  
  78.     /**
  79.      * The color green.
  80.      */
  81.     public final static Color green     = new Color(0, 255, 0);
  82.  
  83.     /**
  84.      * The color magneta.
  85.      */
  86.     public final static Color magenta    = new Color(255, 0, 255);
  87.  
  88.     /**
  89.      * The color cyan.
  90.      */
  91.     public final static Color cyan     = new Color(0, 255, 255);
  92.  
  93.     /**
  94.      * The color blue.
  95.      */
  96.     public final static Color blue     = new Color(0, 0, 255);
  97.  
  98.     /**
  99.      * Private data.
  100.      */
  101.     private int pData;
  102.  
  103.     /**
  104.      * The color value.
  105.      */
  106.     private int value;
  107.  
  108.     /**
  109.      * Creates a color with the specified red, green, and blue values in
  110.      * the range (0 - 255).  The actual color used in rendering will depend
  111.      * on finding the best match given the color space available for a
  112.      * given output device.
  113.      * @param r the red component
  114.      * @param g the green component
  115.      * @param b the blue component
  116.      * @see #getRed
  117.      * @see #getGreen
  118.      * @see #getBlue
  119.      * @see #getRGB
  120.      */
  121.     public Color(int r, int g, int b) {
  122.     this(((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0));
  123.     }
  124.  
  125.     /**
  126.      * Creates a color with the specified combined RGB value consisting of
  127.      * the red component in bits 16-23, the green component in bits 8-15,
  128.      * and the blue component in bits 0-7.  The actual color used in
  129.      * rendering will depend on finding the best match given the color space
  130.      * available for a given output device.
  131.      * @param rgb the combined RGB components
  132.      * @see java.awt.image.ColorModel#getRGBdefault
  133.      * @see #getRed
  134.      * @see #getGreen
  135.      * @see #getBlue
  136.      * @see #getRGB
  137.      */
  138.     public Color(int rgb) {
  139.     value = 0xff000000 | rgb;
  140.     }
  141.  
  142.     /**
  143.      * Creates a color with the specified red, green, and blue values in the
  144.      * range (0.0 - 1.0). The actual color
  145.      * used in rendering will depend on finding the best match given the
  146.      * color space available for a given output device.
  147.      * @param r the red component
  148.      * @param g the red component
  149.      * @param b the red component
  150.      * @see #getRed
  151.      * @see #getGreen
  152.      * @see #getBlue
  153.      * @see #getRGB
  154.      */
  155.     public Color(float r, float g, float b) {
  156.     this((int) (r * 255), (int) (g * 255), (int) (b * 255));
  157.     }
  158.  
  159.     /**
  160.      * Gets the red component.
  161.      * @see #getRGB
  162.      */
  163.     public int getRed() {
  164.     return (value >> 16) & 0xFF;
  165.     }
  166.  
  167.     /**
  168.      * Gets the green component.
  169.      * @see #getRGB
  170.      */
  171.     public int getGreen() {
  172.     return (value >> 8) & 0xFF;
  173.     }
  174.  
  175.     /**
  176.      * Gets the blue component.
  177.      * @see #getRGB
  178.      */
  179.     public int getBlue() {
  180.     return (value >> 0) & 0xFF;
  181.     }
  182.  
  183.     /**
  184.      * Gets the RGB value representing the color in the default RGB ColorModel.
  185.      * (Bits 24-31 are 0xff, 16-23 are red, 8-15 are green, 0-7 are blue).
  186.      * @see java.awt.image.ColorModel#getRGBdefault
  187.      * @see #getRed
  188.      * @see #getGreen
  189.      * @see #getBlue
  190.      */
  191.     public int getRGB() {
  192.     return value;
  193.     }
  194.  
  195.     private static final double FACTOR = 0.7;
  196.  
  197.     /**
  198.      * Returns a brighter version of this color.
  199.      */
  200.     public Color brighter() {
  201.     return new Color(Math.min((int)(getRed()  *(1/FACTOR)), 255), 
  202.              Math.min((int)(getGreen()*(1/FACTOR)), 255),
  203.              Math.min((int)(getBlue() *(1/FACTOR)), 255));
  204.     }
  205.  
  206.     /**
  207.      * Returns a darker version of this color.
  208.      */
  209.     public Color darker() {
  210.     return new Color(Math.max((int)(getRed()  *FACTOR), 0), 
  211.              Math.max((int)(getGreen()*FACTOR), 0),
  212.              Math.max((int)(getBlue() *FACTOR), 0));
  213.     }
  214.  
  215.     /**
  216.      * Computes the hash code.
  217.      */
  218.     public int hashCode() {
  219.     return value;
  220.     }
  221.  
  222.     /**
  223.      * Compares this object against the specified object.
  224.      * @param obj the object to compare with.
  225.      * @return true if the objects are the same; false otherwise.
  226.      */
  227.     public boolean equals(Object obj) {
  228.     if (obj instanceof Color) {
  229.         return value == ((Color)obj).value;
  230.     }
  231.     return false;
  232.     }
  233.  
  234.     /**
  235.      * Returns the String representation of this Color's values.
  236.      */
  237.     public String toString() {
  238.     return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]";
  239.     }
  240.  
  241.     /**
  242.      * Gets the specified Color property.
  243.      * @param nm the name of the color property
  244.      */
  245.     public static Color getColor(String nm) {
  246.     return getColor(nm, null);
  247.     }
  248.  
  249.     /**
  250.      * Gets the specified Color property of the specified Color.
  251.      * @param nm the name of the color property
  252.      * @param v the specified color
  253.      * @return the new color.
  254.      */
  255.     public static Color getColor(String nm, Color v) {
  256.     Integer intval = Integer.getInteger(nm);
  257.     if (intval == null) {
  258.         return v;
  259.     }
  260.     int i = intval.intValue();
  261.     return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
  262.     }
  263.  
  264.     /**
  265.      * Gets the specified Color property of the color value.
  266.      * @param nm the name of the color property
  267.      * @param v the color value
  268.      * @return the new color.
  269.      */
  270.     public static Color getColor(String nm, int v) {
  271.     Integer intval = Integer.getInteger(nm);
  272.     int i = (intval != null) ? intval.intValue() : v;
  273.     return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, (i >> 0) & 0xFF);
  274.     }
  275.  
  276.     /**
  277.      * Returns the RGB value defined by the default RGB ColorModel, of
  278.      * the color corresponding to the given HSB color components.
  279.      * @param hue the hue component of the color
  280.      * @param saturation the saturation of the color
  281.      * @param brightness the brightness of the color
  282.      * @see java.awt.image.ColorModel#getRGBdefault
  283.      * @see #getRGB
  284.      */
  285.     public static int HSBtoRGB(float hue, float saturation, float brightness) {
  286.     int r = 0, g = 0, b = 0;
  287.         if (saturation == 0) {
  288.         r = g = b = (int) (brightness * 255);
  289.     } else {
  290.         double h = hue * 6.0;
  291.         double f = h - java.lang.Math.floor(h);
  292.         double p = brightness * (1.0 - saturation);
  293.         double q = brightness * (1.0 - saturation * f);
  294.         double t = brightness * (1.0 - (saturation * (1.0 - f)));
  295.         switch ((int) h) {
  296.         case 0:
  297.         r = (int) (brightness * 255);
  298.         g = (int) (t * 255);
  299.         b = (int) (p * 255);
  300.         break;
  301.         case 1:
  302.         r = (int) (q * 255);
  303.         g = (int) (brightness * 255);
  304.         b = (int) (p * 255);
  305.         break;
  306.         case 2:
  307.         r = (int) (p * 255);
  308.         g = (int) (brightness * 255);
  309.         b = (int) (t * 255);
  310.         break;
  311.         case 3:
  312.         r = (int) (p * 255);
  313.         g = (int) (q * 255);
  314.         b = (int) (brightness * 255);
  315.         break;
  316.         case 4:
  317.         r = (int) (t * 255);
  318.         g = (int) (p * 255);
  319.         b = (int) (brightness * 255);
  320.         break;
  321.         case 5:
  322.         r = (int) (brightness * 255);
  323.         g = (int) (p * 255);
  324.         b = (int) (q * 255);
  325.         break;
  326.         }
  327.     }
  328.     return 0xff000000 | (r << 16) | (g << 8) | (b << 0);
  329.     }
  330.  
  331.     /**
  332.      * Returns the HSB values corresponding to the color defined by the
  333.      * red, green, and blue components.
  334.      * @param r the red component of the color
  335.      * @param g the green component of the color
  336.      * @param b the blue component of the color
  337.      * @param hsbvals the array to be used to return the 3 HSB values, or null
  338.      * @return the array used to store the results [hue, saturation, brightness]
  339.      * @see java.awt.image.ColorModel#getRGBdefault
  340.      * @see #getRGB
  341.      */
  342.     public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) {
  343.     float hue, saturation, brightness;
  344.     if (hsbvals == null) {
  345.         hsbvals = new float[3];
  346.     }
  347.         int cmax = (r > g) ? r : g;
  348.     if (b > cmax) cmax = b;
  349.     int cmin = (r < g) ? r : g;
  350.     if (b < cmin) cmin = b;
  351.  
  352.     brightness = ((float) cmax) / 255.0f;
  353.     if (cmax != 0)
  354.         saturation = ((float) (cmax - cmin)) / ((float) cmax);
  355.     else
  356.         saturation = 0;
  357.     if (saturation == 0)
  358.         hue = 0;
  359.     else {
  360.         float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
  361.         float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
  362.         float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
  363.         if (r == cmax)
  364.         hue = bluec - greenc;
  365.         else if (g == cmax)
  366.             hue = 2.0f + redc - bluec;
  367.             else
  368.         hue = 4.0f + greenc - redc;
  369.         hue = hue / 6.0f;
  370.         if (hue < 0)
  371.         hue = hue + 1.0f;
  372.     }
  373.     hsbvals[0] = hue;
  374.     hsbvals[1] = saturation;
  375.     hsbvals[2] = brightness;
  376.     return hsbvals;
  377.     }
  378.  
  379.     /**
  380.      * A static Color factory for generating a Color object from HSB
  381.      * values.
  382.      * @param h the hue component
  383.      * @param s the saturation of the color
  384.      * @param b the brightness of the color
  385.      * @return the Color object for the corresponding RGB color
  386.      */
  387.     public static Color getHSBColor(float h, float s, float b) {
  388.     return new Color(HSBtoRGB(h, s, b));
  389.     }
  390. }
  391.