1 package prefuse.util; 2 3 import java.awt.Color ; 4 5 import prefuse.util.collections.IntObjectHashMap; 6 7 31 public class ColorLib { 32 33 public static final char HEX_PREFIX = '#'; 34 35 private static final IntObjectHashMap colorMap = new IntObjectHashMap(); 36 private static int misses = 0; 37 private static int lookups = 0; 38 39 42 49 public static int rgb(int r, int g, int b) { 50 return rgba(r, g, b, 255); 51 } 52 53 59 public static int gray(int v) { 60 return rgba(v, v, v, 255); 61 } 62 63 70 public static int gray(int v, int a) { 71 return rgba(v, v, v, a); 72 } 73 74 87 public static int hex(String hex) { 88 if ( hex.charAt(0) == HEX_PREFIX ) 89 hex = hex.substring(1); 90 91 if ( hex.length() > 6 ) { 92 int rgb = Integer.parseInt(hex.substring(2), 16); 94 int alpha = Integer.parseInt(hex.substring(0,2), 16); 95 return ColorLib.setAlpha(rgb, alpha); 96 } else { 97 return setAlpha(Integer.parseInt(hex, 16), 255); 98 } 99 } 100 101 113 public static int hsb(float h, float s, float b) { 114 return Color.HSBtoRGB(h,s,b); 115 } 116 117 131 public static int hsba(float h, float s, float b, float a) { 132 return setAlpha(Color.HSBtoRGB(h,s,b), (int)(a*255+0.5) & 0xFF); 133 } 134 135 143 public static int rgba(int r, int g, int b, int a) { 144 return ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | 145 ((g & 0xFF) << 8) | ((b & 0xFF) << 0); 146 } 147 148 157 public static int rgba(float r, float g, float b, float a) { 158 return ((((int)(a*255+0.5)) & 0xFF) << 24) | 159 ((((int)(r*255+0.5)) & 0xFF) << 16) | 160 ((((int)(g*255+0.5)) & 0xFF) << 8) | 161 (((int)(b*255+0.5)) & 0xFF); 162 } 163 164 169 public static int color(Color c) { 170 return c.getRGB(); 171 } 172 173 178 public static int red(int color) { 179 return (color>>16) & 0xFF; 180 } 181 182 187 public static int green(int color) { 188 return (color>>8) & 0xFF; 189 } 190 191 196 public static int blue(int color) { 197 return color & 0xFF; 198 } 199 200 205 public static int alpha(int color) { 206 return (color>>24) & 0xFF; 207 } 208 209 215 public static int setAlpha(int c, int alpha) { 216 return rgba(red(c), green(c), blue(c), alpha); 217 } 218 219 222 231 public static Color getColor(float r, float g, float b, float a) { 232 return getColor(rgba(r,g,b,a)); 233 } 234 235 243 public static Color getColor(float r, float g, float b) { 244 return getColor(r,g,b,1.0f); 245 } 246 247 255 public static Color getColor(int r, int g, int b, int a) { 256 return getColor(rgba(r,g,b,a)); 257 } 258 259 266 public static Color getColor(int r, int g, int b) { 267 return getColor(r,g,b,255); 268 } 269 270 276 public static Color getGrayscale(int v) { 277 return getColor(v,v,v,255); 278 } 279 280 286 public static Color getColor(int rgba) { 287 Color c = null; 288 if ( (c=(Color )colorMap.get(rgba)) == null ) { 289 c = new Color (rgba,true); 290 colorMap.put(rgba,c); 291 misses++; 292 } 293 lookups++; 294 return c; 295 } 296 297 300 304 public static int getCacheMissCount() { 305 return misses; 306 } 307 308 312 public static int getCacheLookupCount() { 313 return lookups; 314 } 315 316 319 public static void clearCache() { 320 colorMap.clear(); 321 } 322 323 324 327 private static final float scale = 0.7f; 328 329 340 public static int interp(int c1, int c2, double frac) { 341 double ifrac = 1-frac; 342 return rgba( 343 (int)Math.round(frac*red(c2) + ifrac*red(c1)), 344 (int)Math.round(frac*green(c2) + ifrac*green(c1)), 345 (int)Math.round(frac*blue(c2) + ifrac*blue(c1)), 346 (int)Math.round(frac*alpha(c2) + ifrac*alpha(c1))); 347 } 348 349 354 public static int darker(int c) { 355 return rgba(Math.max(0, (int)(scale*red(c))), 356 Math.max(0, (int)(scale*green(c))), 357 Math.max(0, (int)(scale*blue(c))), 358 alpha(c)); 359 } 360 361 366 public static int brighter(int c) { 367 int r = red(c), g = green(c), b = blue(c); 368 int i = (int)(1.0/(1.0-scale)); 369 if ( r == 0 && g == 0 && b == 0) { 370 return rgba(i, i, i, alpha(c)); 371 } 372 if ( r > 0 && r < i ) r = i; 373 if ( g > 0 && g < i ) g = i; 374 if ( b > 0 && b < i ) b = i; 375 376 return rgba(Math.min(255, (int)(r/scale)), 377 Math.min(255, (int)(g/scale)), 378 Math.min(255, (int)(b/scale)), 379 alpha(c)); 380 } 381 382 387 public static int desaturate(int c) { 388 int a = c & 0xff000000; 389 float r = ((c & 0xff0000) >> 16); 390 float g = ((c & 0x00ff00) >> 8); 391 float b = (c & 0x0000ff); 392 393 r *= 0.2125f; g *= 0.7154f; b *= 0.0721f; 397 int gray = Math.min(((int)(r+g+b)),0xff) & 0xff; 398 return a | (gray << 16) | (gray << 8) | gray; 399 } 400 401 407 public static int saturate(int c, float saturation) { 408 float[] hsb = Color.RGBtoHSB(red(c), green(c), blue(c), null); 409 return ColorLib.hsb(hsb[0], saturation, hsb[2]); 410 } 411 412 415 418 public static final float[] CATEGORY_HUES = { 419 0f, 1f/12f, 1f/6f, 1f/3f, 1f/2f, 7f/12f, 2f/3f, 5f/6f, 11f/12f 420 }; 421 422 426 public static final int DEFAULT_MAP_SIZE = 64; 427 428 433 public static int[] getCoolPalette(int size) { 434 int[] cm = new int[size]; 435 for( int i=0; i<size; i++ ) { 436 float r = i / Math.max(size-1,1.f); 437 cm[i] = rgba(r,1-r,1.f,1.f); 438 } 439 return cm; 440 } 441 442 447 public static int[] getCoolPalette() { 448 return getCoolPalette(DEFAULT_MAP_SIZE); 449 } 450 451 457 public static int[] getHotPalette(int size) { 458 int[] cm = new int[size]; 459 for ( int i=0; i<size; i++ ) { 460 int n = (3*size)/8; 461 float r = ( i<n ? ((float)(i+1))/n : 1.f ); 462 float g = ( i<n ? 0.f : ( i<2*n ? ((float)(i-n))/n : 1.f )); 463 float b = ( i<2*n ? 0.f : ((float)(i-2*n))/(size-2*n) ); 464 cm[i] = rgba(r,g,b,1.0f); 465 } 466 return cm; 467 } 468 469 474 public static int[] getHotPalette() { 475 return getHotPalette(DEFAULT_MAP_SIZE); 476 } 477 478 490 public static int[] getCategoryPalette(int size, 491 float s1, float s2, float b, float a) 492 { 493 int[] cm = new int[size]; 494 float s = s1; 495 for ( int i=0; i<size; i++ ) { 496 int j = i % CATEGORY_HUES.length; 497 if ( j == 0 ) 498 s = s1 + (((float)i)/size)*(s2-s1); 499 cm[i] = hsba(CATEGORY_HUES[j],s,b,a); 500 } 501 return cm; 502 } 503 504 512 public static int[] getCategoryPalette(int size) { 513 return getCategoryPalette(size, 1.f, 0.4f, 1.f, 1.0f); 514 } 515 516 524 public static int[] getHSBPalette(int size, float s, float b) { 525 int[] cm = new int[size]; 526 for ( int i=0; i<size; i++ ) { 527 float h = ((float)i)/(size-1); 528 cm[i] = hsb(h,s,b); 529 } 530 return cm; 531 } 532 533 539 public static int[] getHSBPalette() { 540 return getHSBPalette(DEFAULT_MAP_SIZE, 1.f, 1.f); 541 } 542 543 551 public static int[] getInterpolatedPalette(int size, int c1, int c2) 552 { 553 int[] cm = new int[size]; 554 for ( int i=0; i<size; i++ ) { 555 float f = ((float)i)/(size-1); 556 cm[i] = interp(c1,c2,f); 557 } 558 return cm; 559 } 560 561 568 public static int[] getInterpolatedPalette(int c1, int c2) { 569 return getInterpolatedPalette(DEFAULT_MAP_SIZE, c1, c2); 570 } 571 572 578 public static int[] getGrayscalePalette(int size) { 579 int[] cm = new int[size]; 580 for ( int i=0, g; i<size; i++ ) { 581 g = (int)Math.round(255*(0.2f + 0.6f*((float)i)/(size-1))); 582 cm[size-i-1] = gray(g); 583 } 584 return cm; 585 } 586 587 592 public static int[] getGrayscalePalette() { 593 return getGrayscalePalette(DEFAULT_MAP_SIZE); 594 } 595 596 } | Popular Tags |