colorlib.java
来自「用applet实现很多应用小程序」· Java 代码 · 共 597 行 · 第 1/2 页
JAVA
597 行
/**
* Get the number of cache misses to the Color object cache.
* @return the number of cache misses
*/
public static int getCacheMissCount() {
return misses;
}
/**
* Get the number of cache lookups to the Color object cache.
* @return the number of cache lookups
*/
public static int getCacheLookupCount() {
return lookups;
}
/**
* Clear the Color object cache.
*/
public static void clearCache() {
colorMap.clear();
}
// ------------------------------------------------------------------------
// Color Calculations
private static final float scale = 0.7f;
/**
* Interpolate between two color values by the given mixing proportion.
* A mixing fraction of 0 will result in c1, a value of 1.0 will result
* in c2, and value of 0.5 will result in the color mid-way between the
* two in RGB color space.
* @param c1 the starting color
* @param c2 the target color
* @param frac a fraction between 0 and 1.0 controlling the interpolation
* amount.
* @return the interpolated color code
*/
public static int interp(int c1, int c2, double frac) {
double ifrac = 1-frac;
return rgba(
(int)Math.round(frac*red(c2) + ifrac*red(c1)),
(int)Math.round(frac*green(c2) + ifrac*green(c1)),
(int)Math.round(frac*blue(c2) + ifrac*blue(c1)),
(int)Math.round(frac*alpha(c2) + ifrac*alpha(c1)));
}
/**
* Get a darker shade of an input color.
* @param c a color code
* @return a darkened color code
*/
public static int darker(int c) {
return rgba(Math.max(0, (int)(scale*red(c))),
Math.max(0, (int)(scale*green(c))),
Math.max(0, (int)(scale*blue(c))),
alpha(c));
}
/**
* Get a brighter shade of an input color.
* @param c a color code
* @return a brighter color code
*/
public static int brighter(int c) {
int r = red(c), g = green(c), b = blue(c);
int i = (int)(1.0/(1.0-scale));
if ( r == 0 && g == 0 && b == 0) {
return rgba(i, i, i, alpha(c));
}
if ( r > 0 && r < i ) r = i;
if ( g > 0 && g < i ) g = i;
if ( b > 0 && b < i ) b = i;
return rgba(Math.min(255, (int)(r/scale)),
Math.min(255, (int)(g/scale)),
Math.min(255, (int)(b/scale)),
alpha(c));
}
/**
* Get a desaturated shade of an input color.
* @param c a color code
* @return a desaturated color code
*/
public static int desaturate(int c) {
int a = c & 0xff000000;
float r = ((c & 0xff0000) >> 16);
float g = ((c & 0x00ff00) >> 8);
float b = (c & 0x0000ff);
r *= 0.2125f; // red band weight
g *= 0.7154f; // green band weight
b *= 0.0721f; // blue band weight
int gray = Math.min(((int)(r+g+b)),0xff) & 0xff;
return a | (gray << 16) | (gray << 8) | gray;
}
/**
* Set the saturation of an input color.
* @param c a color code
* @param saturation the new sautration value
* @return a saturated color code
*/
public static int saturate(int c, float saturation) {
float[] hsb = Color.RGBtoHSB(red(c), green(c), blue(c), null);
return ColorLib.hsb(hsb[0], saturation, hsb[2]);
}
// ------------------------------------------------------------------------
// Color Palettes
/**
* Default palette of category hues.
*/
public static final float[] CATEGORY_HUES = {
0f, 1f/12f, 1f/6f, 1f/3f, 1f/2f, 7f/12f, 2f/3f, /*3f/4f,*/ 5f/6f, 11f/12f
};
/**
* The default length of a color palette if its size
* is not otherwise specified.
*/
public static final int DEFAULT_MAP_SIZE = 64;
/**
* Returns a color palette that uses a "cool", blue-heavy color scheme.
* @param size the size of the color palette
* @return the color palette
*/
public static int[] getCoolPalette(int size) {
int[] cm = new int[size];
for( int i=0; i<size; i++ ) {
float r = i / Math.max(size-1,1.f);
cm[i] = rgba(r,1-r,1.f,1.f);
}
return cm;
}
/**
* Returns a color palette of default size that uses a "cool",
* blue-heavy color scheme.
* @return the color palette
*/
public static int[] getCoolPalette() {
return getCoolPalette(DEFAULT_MAP_SIZE);
}
/**
* Returns a color map that moves from black to red to yellow
* to white.
* @param size the size of the color palette
* @return the color palette
*/
public static int[] getHotPalette(int size) {
int[] cm = new int[size];
for ( int i=0; i<size; i++ ) {
int n = (3*size)/8;
float r = ( i<n ? ((float)(i+1))/n : 1.f );
float g = ( i<n ? 0.f : ( i<2*n ? ((float)(i-n))/n : 1.f ));
float b = ( i<2*n ? 0.f : ((float)(i-2*n))/(size-2*n) );
cm[i] = rgba(r,g,b,1.0f);
}
return cm;
}
/**
* Returns a color map of default size that moves from black to
* red to yellow to white.
* @return the color palette
*/
public static int[] getHotPalette() {
return getHotPalette(DEFAULT_MAP_SIZE);
}
/**
* Returns a color palette of given size tries to provide colors
* appropriate as category labels. There are 12 basic color hues
* (red, orange, yellow, olive, green, cyan, blue, purple, magenta,
* and pink). If the size is greater than 12, these colors will be
* continually repeated, but with varying saturation levels.
* @param size the size of the color palette
* @param s1 the initial saturation to use
* @param s2 the final (most distant) saturation to use
* @param b the brightness value to use
* @param a the alpha value to use
*/
public static int[] getCategoryPalette(int size,
float s1, float s2, float b, float a)
{
int[] cm = new int[size];
float s = s1;
for ( int i=0; i<size; i++ ) {
int j = i % CATEGORY_HUES.length;
if ( j == 0 )
s = s1 + (((float)i)/size)*(s2-s1);
cm[i] = hsba(CATEGORY_HUES[j],s,b,a);
}
return cm;
}
/**
* Returns a color palette of given size tries to provide colors
* appropriate as category labels. There are 12 basic color hues
* (red, orange, yellow, olive, green, cyan, blue, purple, magenta,
* and pink). If the size is greater than 12, these colors will be
* continually repeated, but with varying saturation levels.
* @param size the size of the color palette
*/
public static int[] getCategoryPalette(int size) {
return getCategoryPalette(size, 1.f, 0.4f, 1.f, 1.0f);
}
/**
* Returns a color palette of given size that cycles through
* the hues of the HSB (Hue/Saturation/Brightness) color space.
* @param size the size of the color palette
* @param s the saturation value to use
* @param b the brightness value to use
* @return the color palette
*/
public static int[] getHSBPalette(int size, float s, float b) {
int[] cm = new int[size];
for ( int i=0; i<size; i++ ) {
float h = ((float)i)/(size-1);
cm[i] = hsb(h,s,b);
}
return cm;
}
/**
* Returns a color palette of default size that cycles through
* the hues of the HSB (Hue/Saturation/Brightness) color space at
* full saturation and brightness.
* @return the color palette
*/
public static int[] getHSBPalette() {
return getHSBPalette(DEFAULT_MAP_SIZE, 1.f, 1.f);
}
/**
* Returns a color palette of given size that ranges from one
* given color to the other.
* @param size the size of the color palette
* @param c1 the initial color in the color map
* @param c2 the final color in the color map
* @return the color palette
*/
public static int[] getInterpolatedPalette(int size, int c1, int c2)
{
int[] cm = new int[size];
for ( int i=0; i<size; i++ ) {
float f = ((float)i)/(size-1);
cm[i] = interp(c1,c2,f);
}
return cm;
}
/**
* Returns a color palette of default size that ranges from one
* given color to the other.
* @param c1 the initial color in the color map
* @param c2 the final color in the color map
* @return the color palette
*/
public static int[] getInterpolatedPalette(int c1, int c2) {
return getInterpolatedPalette(DEFAULT_MAP_SIZE, c1, c2);
}
/**
* Returns a color palette of specified size that ranges from white to
* black through shades of gray.
* @param size the size of the color palette
* @return the color palette
*/
public static int[] getGrayscalePalette(int size) {
int[] cm = new int[size];
for ( int i=0, g; i<size; i++ ) {
g = (int)Math.round(255*(0.2f + 0.6f*((float)i)/(size-1)));
cm[size-i-1] = gray(g);
}
return cm;
}
/**
* Returns a color palette of default size that ranges from white to
* black through shades of gray.
* @return the color palette
*/
public static int[] getGrayscalePalette() {
return getGrayscalePalette(DEFAULT_MAP_SIZE);
}
} // end of class ColorLib
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?