colorlib.java
来自「用applet实现很多应用小程序」· Java 代码 · 共 597 行 · 第 1/2 页
JAVA
597 行
package prefuse.util;
import java.awt.Color;
import prefuse.util.collections.IntObjectHashMap;
/**
* <p>Library routines for processing color values. The standard color
* representation used by prefuse is to store each color as single
* primitive integer value, using 32 bits to represent 4 8-bit color
* channels: red, green, blue, and alpha (transparency). An alpha value
* of 0 indicates complete transparency while a maximum value (255)
* indicated complete opacity. The layout of the bit is as follows,
* moving from most significant bit on the left to least significant
* bit on the right:</p>
*
* <pre>
* AAAAAAAARRRRRRRRGGGGGGGBBBBBBBB
* </pre>
*
* <p>This class also maintains methods for mapping these values to actual
* Java {@link java.awt.Color} instances; a cache is maintained for
* quick-lookups, avoiding the need to continually allocate new Color
* instances.</p>
*
* <p>Finally, this class also contains routine for creating color
* palettes for use in visualization.</p>
*
* @author <a href="http://jheer.org">jeffrey heer</a>
*/
public class ColorLib {
public static final char HEX_PREFIX = '#';
private static final IntObjectHashMap colorMap = new IntObjectHashMap();
private static int misses = 0;
private static int lookups = 0;
// ------------------------------------------------------------------------
// Color Code Methods
/**
* Get the color code for the given red, green, and blue values.
* @param r the red color component (in the range 0-255)
* @param g the green color component (in the range 0-255)
* @param b the blue color component (in the range 0-255)
* @return the integer color code
*/
public static int rgb(int r, int g, int b) {
return rgba(r, g, b, 255);
}
/**
* Get the color code for the given grayscale value.
* @param v the grayscale value (in the range 0-255, 0 is
* black and 255 is white)
* @return the integer color code
*/
public static int gray(int v) {
return rgba(v, v, v, 255);
}
/**
* Get the color code for the given grayscale value.
* @param v the grayscale value (in the range 0-255, 0 is
* black and 255 is white)
* @param a the alpha (transparency) value (in the range 0-255)
* @return the integer color code
*/
public static int gray(int v, int a) {
return rgba(v, v, v, a);
}
/**
* Parse a hexadecimal String as a color code. The color convention
* is the same as that used in webpages, with two-decimal hexadecimal
* numbers representing RGB color values in the range 0-255. A single
* '#' character may be included at the beginning of the String, but
* is not required. For example '#000000' is black, 'FFFFFF' is white,
* '0000FF' is blue, and '#FFFF00' is orange. Color values may also
* include transparency (alpha) values, ranging from 00 (fully transparent)
* to FF (fully opaque). If included, alpha values should come first in
* the string. For example, "#770000FF" is a translucent blue.
* @param hex the color code value as a hexadecimal String
* @return the integer color code for the input String
*/
public static int hex(String hex) {
if ( hex.charAt(0) == HEX_PREFIX )
hex = hex.substring(1);
if ( hex.length() > 6 ) {
// break up number, as Integer will puke on a large unsigned int
int rgb = Integer.parseInt(hex.substring(2), 16);
int alpha = Integer.parseInt(hex.substring(0,2), 16);
return ColorLib.setAlpha(rgb, alpha);
} else {
return setAlpha(Integer.parseInt(hex, 16), 255);
}
}
/**
* Get the color code for the given hue, saturation, and brightness
* values, translating from HSB color space to RGB color space.
* @param h the hue value (in the range 0-1.0). This represents the
* actual color hue (blue, green, purple, etc).
* @param s the saturation value (in the range 0-1.0). This represents
* "how much" of the color is included. Lower values can result in
* more grayed out or pastel colors.
* @param b the brightness value (in the range 0-1.0). This represents
* how dark or light the color is.
* @return the integer color code
*/
public static int hsb(float h, float s, float b) {
return Color.HSBtoRGB(h,s,b);
}
/**
* Get the color code for the given hue, saturation, and brightness
* values, translating from HSB color space to RGB color space.
* @param h the hue value (in the range 0-1.0). This represents the
* actual color hue (blue, green, purple, etc).
* @param s the saturation value (in the range 0-1.0). This represents
* "how much" of the color is included. Lower values can result in
* more grayed out or pastel colors.
* @param b the brightness value (in the range 0-1.0). This represents
* how dark or light the color is.
* @param a the alpha value (in the range 0-1.0). This represents the
* transparency of the color.
* @return the integer color code
*/
public static int hsba(float h, float s, float b, float a) {
return setAlpha(Color.HSBtoRGB(h,s,b), (int)(a*255+0.5) & 0xFF);
}
/**
* Get the color code for the given red, green, blue, and alpha values.
* @param r the red color component (in the range 0-255)
* @param g the green color component (in the range 0-255)
* @param b the blue color component (in the range 0-255)
* @param a the alpha (transparency) component (in the range 0-255)
* @return the integer color code
*/
public static int rgba(int r, int g, int b, int a) {
return ((a & 0xFF) << 24) | ((r & 0xFF) << 16) |
((g & 0xFF) << 8) | ((b & 0xFF) << 0);
}
/**
* Get the color code for the given red, green, blue, and alpha values as
* floating point numbers in the range 0-1.0.
* @param r the red color component (in the range 0-1.0)
* @param g the green color component (in the range 0-1.0)
* @param b the blue color component (in the range 0-1.0)
* @param a the alpha (transparency) component (in the range 0-1.0)
* @return the integer color code
*/
public static int rgba(float r, float g, float b, float a) {
return ((((int)(a*255+0.5)) & 0xFF) << 24) |
((((int)(r*255+0.5)) & 0xFF) << 16) |
((((int)(g*255+0.5)) & 0xFF) << 8) |
(((int)(b*255+0.5)) & 0xFF);
}
/**
* Get the color code for the given Color instance.
* @param c the Java Color instance
* @return the integer color code
*/
public static int color(Color c) {
return c.getRGB();
}
/**
* Get the red component of the given color.
* @param color the color code
* @return the red component of the color (in the range 0-255)
*/
public static int red(int color) {
return (color>>16) & 0xFF;
}
/**
* Get the green component of the given color.
* @param color the color code
* @return the green component of the color (in the range 0-255)
*/
public static int green(int color) {
return (color>>8) & 0xFF;
}
/**
* Get the blue component of the given color.
* @param color the color code
* @return the blue component of the color (in the range 0-255)
*/
public static int blue(int color) {
return color & 0xFF;
}
/**
* Get the alpha component of the given color.
* @param color the color code
* @return the alpha component of the color (in the range 0-255)
*/
public static int alpha(int color) {
return (color>>24) & 0xFF;
}
/**
* Set the alpha component of the given color.
* @param c the color code
* @param alpha the alpha value to set
* @return the new color with updated alpha channel
*/
public static int setAlpha(int c, int alpha) {
return rgba(red(c), green(c), blue(c), alpha);
}
// ------------------------------------------------------------------------
// java.awt.Color Lookup Methods
/**
* Get a Java Color object for the given red, green, blue, and alpha values
* as floating point numbers in the range 0-1.0.
* @param r the red color component (in the range 0-1.0)
* @param g the green color component (in the range 0-1.0)
* @param b the blue color component (in the range 0-1.0)
* @param a the alpha (transparency) component (in the range 0-1.0)
* @return a Java Color object
*/
public static Color getColor(float r, float g, float b, float a) {
return getColor(rgba(r,g,b,a));
}
/**
* Get a Java Color object for the given red, green, and blue values
* as floating point numbers in the range 0-1.0.
* @param r the red color component (in the range 0-1.0)
* @param g the green color component (in the range 0-1.0)
* @param b the blue color component (in the range 0-1.0)
* @return a Java Color object
*/
public static Color getColor(float r, float g, float b) {
return getColor(r,g,b,1.0f);
}
/**
* Get a Java Color object for the given red, green, and blue values.
* @param r the red color component (in the range 0-255)
* @param g the green color component (in the range 0-255)
* @param b the blue color component (in the range 0-255)
* @param a the alpa (transparency) component (in the range 0-255)
* @return a Java Color object
*/
public static Color getColor(int r, int g, int b, int a) {
return getColor(rgba(r,g,b,a));
}
/**
* Get a Java Color object for the given red, green, and blue values.
* @param r the red color component (in the range 0-255)
* @param g the green color component (in the range 0-255)
* @param b the blue color component (in the range 0-255)
* @return a Java Color object
*/
public static Color getColor(int r, int g, int b) {
return getColor(r,g,b,255);
}
/**
* Get a Java Color object for the given grayscale value.
* @param v the grayscale value (in the range 0-255, 0 is
* black and 255 is white)
* @return a Java Color object
*/
public static Color getGrayscale(int v) {
return getColor(v,v,v,255);
}
/**
* Get a Java Color object for the given color code value.
* @param rgba the integer color code containing red, green,
* blue, and alpha channel information
* @return a Java Color object
*/
public static Color getColor(int rgba) {
Color c = null;
if ( (c=(Color)colorMap.get(rgba)) == null ) {
c = new Color(rgba,true);
colorMap.put(rgba,c);
misses++;
}
lookups++;
return c;
}
// ------------------------------------------------------------------------
// ColorLib Statistics and Cache Management
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?