?? color.java
字號:
/* * Copyright (C) 2005, 2006 data2c GmbH (www.data2c.com) * * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com * * J2ME version of java.awt.Color. *///#ifndef j2sepackage org.awt;/** * The <code>Color</code> class is used encapsulate colors in the default * sRGB color space or colors in arbitrary color spaces identified by a * {@link ColorSpace}. Every color has an implicit alpha value of 1.0 or * an explicit one provided in the constructor. The alpha value * defines the transparency of a color and can be represented by * a float value in the range 0.0 - 1.0 or 0 - 255. * An alpha value of 1.0 or 255 means that the color is completely * opaque and an alpha value of 0 or 0.0 means that the color is * completely transparent. * When constructing a <code>Color</code> with an explicit alpha or * getting the color/alpha components of a <code>Color</code>, the color * components are never premultiplied by the alpha component. * <p> * The default color space for the Java 2D(tm) API is sRGB, a proposed * standard RGB color space. For further information on sRGB, * see <A href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html"> * http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html * </A>. * <p> */public class Color implements Transparency /*Paint, java.io.Serializable*/ { /** * The color white. In the default sRGB space. */ public final static Color white = new Color(255, 255, 255); public final static Color WHITE = white; /** * The color light gray. In the default sRGB space. */ public final static Color lightGray = new Color(192, 192, 192); public final static Color LIGHT_GRAY = lightGray; /** * The color gray. In the default sRGB space. */ public final static Color gray = new Color(128, 128, 128); public final static Color GRAY = gray; /** * The color dark gray. In the default sRGB space. */ public final static Color darkGray = new Color(64, 64, 64); public final static Color DARK_GRAY = darkGray; /** * The color black. In the default sRGB space. */ public final static Color black = new Color(0, 0, 0); public final static Color BLACK = black; /** * The color red. In the default sRGB space. */ public final static Color red = new Color(255, 0, 0); public final static Color RED = red; /** * The color pink. In the default sRGB space. */ public final static Color pink = new Color(255, 175, 175); public final static Color PINK = pink; /** * The color orange. In the default sRGB space. */ public final static Color orange = new Color(255, 200, 0); public final static Color ORANGE = orange; /** * The color yellow. In the default sRGB space. */ public final static Color yellow = new Color(255, 255, 0); public final static Color YELLOW = yellow; /** * The color green. In the default sRGB space. */ public final static Color green = new Color(0, 255, 0); public final static Color GREEN = green; /** * The color magenta. In the default sRGB space. */ public final static Color magenta = new Color(255, 0, 255); public final static Color MAGENTA = magenta; /** * The color cyan. In the default sRGB space. */ public final static Color cyan = new Color(0, 255, 255); public final static Color CYAN = cyan; /** * The color blue. In the default sRGB space. */ public final static Color blue = new Color(0, 0, 255); public final static Color BLUE = blue; /** * The color value. * @serial * @see #getRGB */ int value; /** * Checks the color integer components supplied for validity. * Throws an {@link IllegalArgumentException} if the value is out of * range. * @param r the Red component * @param g the Green component * @param b the Blue component **/ private static void testColorValueRange(int r, int g, int b, int a) { boolean rangeError = false; String badComponentString = ""; if ( a < 0 || a > 255) { rangeError = true; badComponentString = badComponentString + " Alpha"; } if ( r < 0 || r > 255) { rangeError = true; badComponentString = badComponentString + " Red"; } if ( g < 0 || g > 255) { rangeError = true; badComponentString = badComponentString + " Green"; } if ( b < 0 || b > 255) { rangeError = true; badComponentString = badComponentString + " Blue"; } if ( rangeError == true ) { throw new IllegalArgumentException("Color parameter outside of expected range:" + badComponentString); } } /** * Creates an opaque sRGB color with the specified red, green, * and blue values in the range (0 - 255). * The actual color used in rendering depends * on finding the best match given the color space * available for a given output device. * Alpha is defaulted to 255. * @param r the red component * @param g the green component * @param b the blue component * @see #getRed * @see #getGreen * @see #getBlue * @see #getRGB */ public Color(int r, int g, int b) { this(r, g, b, 255); } /** * Creates an sRGB color with the specified red, green, blue, and alpha * values in the range (0 - 255). * @param r the red component * @param g the green component * @param b the blue component * @param a the alpha component * @see #getRed * @see #getGreen * @see #getBlue * @see #getAlpha * @see #getRGB */ public Color(int r, int g, int b, int a) { value = ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0); testColorValueRange(r,g,b,a); } /** * Creates an opaque sRGB color with the specified combined RGB value * consisting of the red component in bits 16-23, the green component * in bits 8-15, and the blue component in bits 0-7. The actual color * used in rendering depends on finding the best match given the * color space available for a particular output device. Alpha is * defaulted to 255. * @param rgb the combined RGB components * @see java.awt.image.ColorModel#getRGBdefault * @see #getRed * @see #getGreen * @see #getBlue * @see #getRGB */ public Color(int rgb) { value = 0xff000000 | rgb; } /** * Creates an sRGB color with the specified combined RGBA value consisting * of the alpha component in bits 24-31, the red component in bits 16-23, * the green component in bits 8-15, and the blue component in bits 0-7. * If the <code>hasalpha</code> argument is <code>false</code>, alpha * is defaulted to 255. * @param rgba the combined RGBA components * @param hasalpha <code>true</code> if the alpha bits are valid; * <code>false</code> otherwise * @see java.awt.image.ColorModel#getRGBdefault * @see #getRed * @see #getGreen * @see #getBlue * @see #getAlpha * @see #getRGB */ public Color(int rgba, boolean hasalpha) { if (hasalpha) { value = rgba; } else { value = 0xff000000 | rgba; } } /** * Returns the red component in the range 0-255 in the default sRGB * space. * @return the red component. * @see #getRGB */ public int getRed() { return (getRGB() >> 16) & 0xFF; } /** * Returns the green component in the range 0-255 in the default sRGB * space. * @return the green component. * @see #getRGB */ public int getGreen() { return (getRGB() >> 8) & 0xFF; } /** * Returns the blue component in the range 0-255 in the default sRGB * space. * @return the blue component. * @see #getRGB */ public int getBlue() { return (getRGB() >> 0) & 0xFF; } /** * Returns the alpha component in the range 0-255. * @return the alpha component. * @see #getRGB */ public int getAlpha() { return (getRGB() >> 24) & 0xff; } /** * Returns the RGB value representing the color in the default sRGB * {@link ColorModel}. * (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are * blue). * @return the RGB value of the color in the default sRGB * <code>ColorModel</code>. * @see java.awt.image.ColorModel#getRGBdefault * @see #getRed * @see #getGreen * @see #getBlue * @since JDK1.0 */ public int getRGB() { return value; } private static final double FACTOR = 0.7;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -