color.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,004 行 · 第 1/3 页
JAVA
1,004 行
* @see ColorModel#getRGBdefault()
* @see #getRed()
* @see #getGreen()
* @see #getBlue()
* @see #getRGB()
* @see #Color(int, boolean)
*/
public Color(int value)
{
this(value, false);
}
/**
* Initializes a new instance of <code>Color</code> using the specified
* RGB value. The blue value is in bits 0-7, green in bits 8-15, and
* red in bits 16-23. The alpha value is in bits 24-31, unless hasalpha
* is false, in which case alpha is set to 255. When drawing to screen, the
* actual color may be adjusted to the best match of hardware capabilities.
*
* @param value the RGB value
* @param hasalpha true if value includes the alpha
* @see ColorModel#getRGBdefault()
* @see #getRed()
* @see #getGreen()
* @see #getBlue()
* @see #getAlpha()
* @see #getRGB()
*/
public Color(int value, boolean hasalpha)
{
// Note: SystemColor calls this constructor, setting falpha to 0; but
// code in getRGBComponents correctly reports falpha as 1.0 to the user
// for all instances of SystemColor since frgbvalue is left null here.
if (hasalpha)
falpha = ((value & ALPHA_MASK) >> 24) / 255f;
else
{
value |= ALPHA_MASK;
falpha = 1;
}
this.value = value;
cs = null;
}
/**
* Initializes a new instance of <code>Color</code> using the specified
* RGB values. These must be in the range of 0.0-1.0. Alpha is assigned
* the value of 1.0 (opaque). When drawing to screen, the actual color may
* be adjusted to the best match of hardware capabilities.
*
* @param red the red component of the RGB value
* @param green the green component of the RGB value
* @param blue the blue component of the RGB value
* @throws IllegalArgumentException tf the values are out of range 0.0f-1.0f
* @see #getRed()
* @see #getGreen()
* @see #getBlue()
* @see #getRGB()
* @see #Color(float, float, float, float)
*/
public Color(float red, float green, float blue)
{
this(red, green, blue, 1.0f);
}
/**
* Initializes a new instance of <code>Color</code> using the specified
* RGB and alpha values. These must be in the range of 0.0-1.0. When drawing
* to screen, the actual color may be adjusted to the best match of
* hardware capabilities.
*
* @param red the red component of the RGB value
* @param green the green component of the RGB value
* @param blue the blue component of the RGB value
* @param alpha the alpha value of the color
* @throws IllegalArgumentException tf the values are out of range 0.0f-1.0f
* @see #getRed()
* @see #getGreen()
* @see #getBlue()
* @see #getAlpha()
* @see #getRGB()
*/
public Color(float red, float green, float blue, float alpha)
{
value = convert(red, green, blue, alpha);
frgbvalue = new float[] {red, green, blue};
falpha = alpha;
cs = null;
}
/**
* Creates a color in the given ColorSpace with the specified alpha. The
* array must be non-null and have enough elements for the color space
* (for example, RGB requires 3 elements, CMYK requires 4). When drawing
* to screen, the actual color may be adjusted to the best match of
* hardware capabilities.
*
* @param space the color space of components
* @param components the color components, except alpha
* @param alpha the alpha value of the color
* @throws NullPointerException if cpsace or components is null
* @throws ArrayIndexOutOfBoundsException if components is too small
* @throws IllegalArgumentException if alpha or any component is out of range
* @see #getComponents(float[])
* @see #getColorComponents(float[])
*/
public Color(ColorSpace space, float[] components, float alpha)
{
frgbvalue = space.toRGB(components);
fvalue = components;
falpha = alpha;
cs = space;
value = convert(frgbvalue[0], frgbvalue[1], frgbvalue[2], alpha);
}
/**
* Returns the red value for this color, as an integer in the range 0-255
* in the sRGB color space.
*
* @return the red value for this color
* @see #getRGB()
*/
public int getRed()
{
// Do not inline getRGB() to value, because of SystemColor.
return (getRGB() & RED_MASK) >> 16;
}
/**
* Returns the green value for this color, as an integer in the range 0-255
* in the sRGB color space.
*
* @return the green value for this color
* @see #getRGB()
*/
public int getGreen()
{
// Do not inline getRGB() to value, because of SystemColor.
return (getRGB() & GREEN_MASK) >> 8;
}
/**
* Returns the blue value for this color, as an integer in the range 0-255
* in the sRGB color space.
*
* @return the blue value for this color
* @see #getRGB()
*/
public int getBlue()
{
// Do not inline getRGB() to value, because of SystemColor.
return getRGB() & BLUE_MASK;
}
/**
* Returns the alpha value for this color, as an integer in the range 0-255.
*
* @return the alpha value for this color
* @see #getRGB()
*/
public int getAlpha()
{
// Do not inline getRGB() to value, because of SystemColor.
return (getRGB() & ALPHA_MASK) >> 24;
}
/**
* Returns the RGB value for this color, in the sRGB color space. The blue
* value will be in bits 0-7, green in 8-15, red in 6-23, and alpha value in
* 24-31.
*
* @return the RGB value for this color
* @see ColorModel#getRGBdefault()
* @see #getRed()
* @see #getGreen()
* @see #getBlue()
* @see #getAlpha()
*/
public int getRGB()
{
return value;
}
/**
* Returns a brighter version of this color. This is done by increasing the
* RGB values by an arbitrary scale factor. The new color is opaque (an
* alpha of 255). Note that this method and the <code>darker()</code>
* method are not necessarily inverses.
*
* @return a brighter version of this color
* @see #darker()
*/
public Color brighter()
{
// Do not inline getRGB() to this.value, because of SystemColor.
int value = getRGB();
int red = (value & RED_MASK) >> 16;
int green = (value & GREEN_MASK) >> 8;
int blue = value & BLUE_MASK;
// We have to special case 0-2 because they won't scale by division.
red = red < 3 ? 3 : (int) Math.min(255, red / BRIGHT_SCALE);
green = green < 3 ? 3 : (int) Math.min(255, green / BRIGHT_SCALE);
blue = blue < 3 ? 3 : (int) Math.min(255, blue / BRIGHT_SCALE);
return new Color(red, green, blue, 255);
}
/**
* Returns a darker version of this color. This is done by decreasing the
* RGB values by an arbitrary scale factor. The new color is opaque (an
* alpha of 255). Note that this method and the <code>brighter()</code>
* method are not necessarily inverses.
*
* @return a darker version of this color
* @see #brighter()
*/
public Color darker()
{
// Do not inline getRGB() to this.value, because of SystemColor.
int value = getRGB();
return new Color((int) (((value & RED_MASK) >> 16) * BRIGHT_SCALE),
(int) (((value & GREEN_MASK) >> 8) * BRIGHT_SCALE),
(int) ((value & BLUE_MASK) * BRIGHT_SCALE), 255);
}
/**
* Returns a hash value for this color. This is simply the color in 8-bit
* precision, in the format 0xAARRGGBB (alpha, red, green, blue).
*
* @return a hash value for this color
*/
public int hashCode()
{
return value;
}
/**
* Tests this object for equality against the specified object. This will
* be true if and only if the specified object is an instance of
* <code>Color</code> and has the same 8-bit integer red, green, and blue
* values as this object. Note that two colors may be slightly different
* as float values, but round to the same integer values. Also note that
* this does not accurately compare SystemColors, since that class does
* not store its internal data in RGB format like regular colors.
*
* @param obj the object to compare to
* @return true if the specified object is semantically equal to this one
*/
public boolean equals(Object obj)
{
return obj instanceof Color && ((Color) obj).value == value;
}
/**
* Returns a string representation of this object. Subclasses may return
* any desired format, except for null, but this implementation returns
* <code>getClass().getName() + "[r=" + getRed() + ",g=" + getGreen()
* + ",b=" + getBlue() + ']'</code>.
*
* @return a string representation of this object
*/
public String toString()
{
return getClass().getName() + "[r=" + ((value & RED_MASK) >> 16)
+ ",g=" + ((value & GREEN_MASK) >> 8) + ",b=" + (value & BLUE_MASK)
+ ']';
}
/**
* Converts the specified string to a number, using Integer.decode, and
* creates a new instance of <code>Color</code> from the value. The alpha
* value will be 255 (opaque).
*
* @param str the numeric color string
* @return a new instance of <code>Color</code> for the string
* @throws NumberFormatException if the string cannot be parsed
* @throws NullPointerException if the string is null
* @see Integer#decode(String)
* @see #Color(int)
* @since 1.1
*/
public static Color decode(String str)
{
return new Color(Integer.decode(str).intValue(), false);
}
/**
* Returns a new instance of <code>Color</code> from the value of the
* system property named by the specified string. If the property does not
* exist, or cannot be parsed, then <code>null</code> will be returned.
*
* @param prop the system property to retrieve
* @throws SecurityException if getting the property is denied
* @see #getColor(String, Color)
* @see Integer#getInteger(String)
*/
public static Color getColor(String prop)
{
return getColor(prop, null);
}
/**
* Returns a new instance of <code>Color</code> from the value of the
* system property named by the specified string. If the property does
* not exist, or cannot be parsed, then the default color value will be
* returned.
*
* @param prop the system property to retrieve
* @param defcolor the default color
* @throws SecurityException if getting the property is denied
* @see Integer#getInteger(String)
*/
public static Color getColor(String prop, Color defcolor)
{
Integer val = Integer.getInteger(prop, null);
return val == null ? defcolor
: new Color(val.intValue(), false);
}
/**
* Returns a new instance of <code>Color</code> from the value of the
* system property named by the specified string. If the property does
* not exist, or cannot be parsed, then the default RGB value will be
* used to create a return value.
*
* @param prop the system property to retrieve
* @param defrgb the default RGB value
* @throws SecurityException if getting the property is denied
* @see #getColor(String, Color)
* @see Integer#getInteger(String, int)
*/
public static Color getColor(String prop, int defrgb)
{
Color c = getColor(prop, null);
return c == null ? new Color(defrgb, false) : c;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?