📄 color.java
字号:
/** * Creates a new <code>Color</code> that is a brighter version of this * <code>Color</code>. * <p> * This method applies an arbitrary scale factor to each of the three RGB * components of this <code>Color</code> to create a brighter version * of this <code>Color</code>. Although <code>brighter</code> and * <code>darker</code> are inverse operations, the results of a * series of invocations of these two methods might be inconsistent * because of rounding errors. * @return a new <code>Color</code> object that is * a brighter version of this <code>Color</code>. * @see java.awt.Color#darker * @since JDK1.0 */ public Color brighter() { int r = getRed(); int g = getGreen(); int b = getBlue(); /* From 2D group: * 1. black.brighter() should return grey * 2. applying brighter to blue will always return blue, brighter * 3. non pure color (non zero rgb) will eventually return white */ int i = (int)(1.0/(1.0-FACTOR)); if ( r == 0 && g == 0 && b == 0) { return new Color(i, i, i); } if ( r > 0 && r < i ) r = i; if ( g > 0 && g < i ) g = i; if ( b > 0 && b < i ) b = i; return new Color(Math.min((int)(r/FACTOR), 255), Math.min((int)(g/FACTOR), 255), Math.min((int)(b/FACTOR), 255)); } /** * Creates a new <code>Color</code> that is a darker version of this * <code>Color</code>. * <p> * This method applies an arbitrary scale factor to each of the three RGB * components of this <code>Color</code> to create a darker version of * this <code>Color</code>. Although <code>brighter</code> and * <code>darker</code> are inverse operations, the results of a series * of invocations of these two methods might be inconsistent because * of rounding errors. * @return a new <code>Color</code> object that is * a darker version of this <code>Color</code>. * @see java.awt.Color#brighter * @since JDK1.0 */ public Color darker() { return new Color(Math.max((int)(getRed() *FACTOR), 0), Math.max((int)(getGreen()*FACTOR), 0), Math.max((int)(getBlue() *FACTOR), 0)); } /** * Computes the hash code for this <code>Color</code>. * @return a hash code value for this object. * @since JDK1.0 */ public int hashCode() { return value; } /** * Determines whether another object is equal to this * <code>Color</code>. * <p> * The result is <code>true</code> if and only if the argument is not * <code>null</code> and is a <code>Color</code> object that has the same * red, green, blue, and alpha values as this object. * @param obj the object to test for equality with this * <code>Color</code> * @return <code>true</code> if the objects are the same; * <code>false</code> otherwise. * @since JDK1.0 */ public boolean equals(Object obj) { return obj instanceof Color && ((Color)obj).value == this.value; } /** * Returns a string representation of this <code>Color</code>. This * method is intended to be used only for debugging purposes. The * content and format of the returned string might vary between * implementations. The returned string might be empty but cannot * be <code>null</code>. * * @return a string representation of this <code>Color</code>. */ public String toString() { return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]"; } /** * Converts a <code>String</code> to an integer and returns the * specified opaque <code>Color</code>. This method handles string * formats that are used to represent octal and hexidecimal numbers. * @param nm a <code>String</code> that represents * an opaque color as a 24-bit integer * @return the new <code>Color</code> object. * @see java.lang.Integer#decode * @exception NumberFormatException if the specified string cannot * be interpreted as a decimal, * octal, or hexidecimal integer. * @since JDK1.1 */ public static Color decode(String nm) throws NumberFormatException { Integer intval = Integer.valueOf(nm); int i = intval.intValue(); return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF); }//#ifdef notdef /** * Finds a color in the system properties. * <p> * The argument is treated as the name of a system property to * be obtained. The string value of this property is then interpreted * as an integer which is then converted to a <code>Color</code> * object. * <p> * If the specified property is not found or could not be parsed as * an integer then <code>null</code> is returned. * @param nm the name of the color property * @return the <code>Color</code> converted from the system * property. * @see java.lang.System#getProperty(java.lang.String) * @see java.lang.Integer#getInteger(java.lang.String) * @see java.awt.Color#Color(int) * @since JDK1.0 */ public static Color getColor(String nm) { return getColor(nm, null); } /** * Finds a color in the system properties. * <p> * The first argument is treated as the name of a system property to * be obtained. The string value of this property is then interpreted * as an integer which is then converted to a <code>Color</code> * object. * <p> * If the specified property is not found or cannot be parsed as * an integer then the <code>Color</code> specified by the second * argument is returned instead. * @param nm the name of the color property * @param v the default <code>Color</code> * @return the <code>Color</code> converted from the system * property, or the specified <code>Color</code>. * @see java.lang.System#getProperty(java.lang.String) * @see java.lang.Integer#getInteger(java.lang.String) * @see java.awt.Color#Color(int) * @since JDK1.0 */ public static Color getColor(String nm, Color v) { Integer intval = Integer.getInteger(nm); if (intval == null) { return v; } int i = intval.intValue(); return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF); } /** * Finds a color in the system properties. * <p> * The first argument is treated as the name of a system property to * be obtained. The string value of this property is then interpreted * as an integer which is then converted to a <code>Color</code> * object. * <p> * If the specified property is not found or could not be parsed as * an integer then the integer value <code>v</code> is used instead, * and is converted to a <code>Color</code> object. * @param nm the name of the color property * @param v the default color value, as an integer * @return the <code>Color</code> converted from the system * property or the <code>Color</code> converted from * the specified integer. * @see java.lang.System#getProperty(java.lang.String) * @see java.lang.Integer#getInteger(java.lang.String) * @see java.awt.Color#Color(int) * @since JDK1.0 */ public static Color getColor(String nm, int v) { Integer intval = Integer.getInteger(nm); int i = (intval != null) ? intval.intValue() : v; return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, (i >> 0) & 0xFF); }//#endif /** * Converts the components of a color, as specified by the HSB * model, to an equivalent set of values for the default RGB model. * <p> * The <code>saturation</code> and <code>brightness</code> components * should be floating-point values between zero and one * (numbers in the range 0.0-1.0). The <code>hue</code> component * can be any floating-point number. The floor of this number is * subtracted from it to create a fraction between 0 and 1. This * fractional number is then multiplied by 360 to produce the hue * angle in the HSB color model. * <p> * The integer that is returned by <code>HSBtoRGB</code> encodes the * value of a color in bits 0-23 of an integer value that is the same * format used by the method {@link #getRGB() <code>getRGB</code>}. * This integer can be supplied as an argument to the * <code>Color</code> constructor that takes a single integer argument. * @param hue the hue component of the color * @param saturation the saturation of the color * @param brightness the brightness of the color * @return the RGB value of the color with the indicated hue, * saturation, and brightness. * @see java.awt.Color#getRGB() * @see java.awt.Color#Color(int) * @see java.awt.image.ColorModel#getRGBdefault() * @since JDK1.0 */ public static int HSBtoRGB(float hue, float saturation, float brightness) { int r = 0, g = 0, b = 0; if (saturation == 0) { r = g = b = (int) (brightness * 255.0f + 0.5f); } else { float h = (hue - (float)Math.floor(hue)) * 6.0f; float f = h - (float)java.lang.Math.floor(h); float p = brightness * (1.0f - saturation); float q = brightness * (1.0f - saturation * f); float t = brightness * (1.0f - (saturation * (1.0f - f))); switch ((int) h) { case 0: r = (int) (brightness * 255.0f + 0.5f); g = (int) (t * 255.0f + 0.5f); b = (int) (p * 255.0f + 0.5f); break; case 1: r = (int) (q * 255.0f + 0.5f); g = (int) (brightness * 255.0f + 0.5f); b = (int) (p * 255.0f + 0.5f); break; case 2: r = (int) (p * 255.0f + 0.5f); g = (int) (brightness * 255.0f + 0.5f); b = (int) (t * 255.0f + 0.5f); break; case 3: r = (int) (p * 255.0f + 0.5f); g = (int) (q * 255.0f + 0.5f); b = (int) (brightness * 255.0f + 0.5f); break; case 4: r = (int) (t * 255.0f + 0.5f); g = (int) (p * 255.0f + 0.5f); b = (int) (brightness * 255.0f + 0.5f); break; case 5: r = (int) (brightness * 255.0f + 0.5f); g = (int) (p * 255.0f + 0.5f); b = (int) (q * 255.0f + 0.5f); break; } } return 0xff000000 | (r << 16) | (g << 8) | (b << 0); } /** * Returns the transparency mode for this <code>Color</code>. This is * required to implement the <code>Paint</code> interface. * @return this <code>Color</code> object's transparency mode. * @see Transparency * @see #createContext */ public int getTransparency() { int alpha = getAlpha(); if (alpha == 0xff) { return Transparency.OPAQUE; } else if (alpha == 0) { return Transparency.BITMASK; } else { return Transparency.TRANSLUCENT; } }}//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -