1 7 8 package java.awt; 9 10 import java.io.*; 11 import java.lang.*; 12 import java.awt.image.ColorModel ; 13 import java.awt.geom.AffineTransform ; 14 import java.awt.geom.Rectangle2D ; 15 import java.awt.color.ColorSpace ; 16 17 43 public class Color implements Paint , java.io.Serializable { 44 45 48 public final static Color white = new Color (255, 255, 255); 49 50 53 public final static Color WHITE = white; 54 55 58 public final static Color lightGray = new Color (192, 192, 192); 59 60 63 public final static Color LIGHT_GRAY = lightGray; 64 65 68 public final static Color gray = new Color (128, 128, 128); 69 70 73 public final static Color GRAY = gray; 74 75 78 public final static Color darkGray = new Color (64, 64, 64); 79 80 83 public final static Color DARK_GRAY = darkGray; 84 85 88 public final static Color black = new Color (0, 0, 0); 89 90 93 public final static Color BLACK = black; 94 95 98 public final static Color red = new Color (255, 0, 0); 99 100 103 public final static Color RED = red; 104 105 108 public final static Color pink = new Color (255, 175, 175); 109 110 113 public final static Color PINK = pink; 114 115 118 public final static Color orange = new Color (255, 200, 0); 119 120 123 public final static Color ORANGE = orange; 124 125 128 public final static Color yellow = new Color (255, 255, 0); 129 130 133 public final static Color YELLOW = yellow; 134 135 138 public final static Color green = new Color (0, 255, 0); 139 140 143 public final static Color GREEN = green; 144 145 148 public final static Color magenta = new Color (255, 0, 255); 149 150 153 public final static Color MAGENTA = magenta; 154 155 158 public final static Color cyan = new Color (0, 255, 255); 159 160 163 public final static Color CYAN = cyan; 164 165 168 public final static Color blue = new Color (0, 0, 255); 169 170 173 public final static Color BLUE = blue; 174 175 178 transient private long pData; 179 180 185 int value; 186 187 197 private float frgbvalue[] = null; 198 199 209 private float fvalue[] = null; 210 211 219 private float falpha = 0.0f; 220 221 229 private ColorSpace cs = null; 230 231 234 private static final long serialVersionUID = 118526816881161077L; 235 236 239 private static native void initIDs(); 240 241 static { 242 248 249 250 Toolkit.loadLibraries(); 251 if (!GraphicsEnvironment.isHeadless()) { 252 initIDs(); 253 } 254 } 255 256 264 private static void testColorValueRange(int r, int g, int b, int a) { 265 boolean rangeError = false; 266 String badComponentString = ""; 267 268 if ( a < 0 || a > 255) { 269 rangeError = true; 270 badComponentString = badComponentString + " Alpha"; 271 } 272 if ( r < 0 || r > 255) { 273 rangeError = true; 274 badComponentString = badComponentString + " Red"; 275 } 276 if ( g < 0 || g > 255) { 277 rangeError = true; 278 badComponentString = badComponentString + " Green"; 279 } 280 if ( b < 0 || b > 255) { 281 rangeError = true; 282 badComponentString = badComponentString + " Blue"; 283 } 284 if ( rangeError == true ) { 285 throw new IllegalArgumentException ("Color parameter outside of expected range:" 286 + badComponentString); 287 } 288 } 289 290 299 private static void testColorValueRange(float r, float g, float b, float a) { 300 boolean rangeError = false; 301 String badComponentString = ""; 302 if ( a < 0.0 || a > 1.0) { 303 rangeError = true; 304 badComponentString = badComponentString + " Alpha"; 305 } 306 if ( r < 0.0 || r > 1.0) { 307 rangeError = true; 308 badComponentString = badComponentString + " Red"; 309 } 310 if ( g < 0.0 || g > 1.0) { 311 rangeError = true; 312 badComponentString = badComponentString + " Green"; 313 } 314 if ( b < 0.0 || b > 1.0) { 315 rangeError = true; 316 badComponentString = badComponentString + " Blue"; 317 } 318 if ( rangeError == true ) { 319 throw new IllegalArgumentException ("Color parameter outside of expected range:" 320 + badComponentString); 321 } 322 } 323 324 343 public Color(int r, int g, int b) { 344 this(r, g, b, 255); 345 } 346 347 364 public Color(int r, int g, int b, int a) { 365 value = ((a & 0xFF) << 24) | 366 ((r & 0xFF) << 16) | 367 ((g & 0xFF) << 8) | 368 ((b & 0xFF) << 0); 369 testColorValueRange(r,g,b,a); 370 } 371 372 387 public Color(int rgb) { 388 value = 0xff000000 | rgb; 389 } 390 391 408 public Color(int rgba, boolean hasalpha) { 409 if (hasalpha) { 410 value = rgba; 411 } else { 412 value = 0xff000000 | rgba; 413 } 414 } 415 416 434 public Color(float r, float g, float b) { 435 this( (int) (r*255+0.5), (int) (g*255+0.5), (int) (b*255+0.5)); 436 testColorValueRange(r,g,b,1.0f); 437 frgbvalue = new float[3]; 438 frgbvalue[0] = r; 439 frgbvalue[1] = g; 440 frgbvalue[2] = b; 441 falpha = 1.0f; 442 fvalue = frgbvalue; 443 } 444 445 463 public Color(float r, float g, float b, float a) { 464 this((int)(r*255+0.5), (int)(g*255+0.5), (int)(b*255+0.5), (int)(a*255+0.5)); 465 frgbvalue = new float[3]; 466 frgbvalue[0] = r; 467 frgbvalue[1] = g; 468 frgbvalue[2] = b; 469 falpha = a; 470 fvalue = frgbvalue; 471 } 472 473 491 public Color(ColorSpace cspace, float components[], float alpha) { 492 boolean rangeError = false; 493 String badComponentString = ""; 494 int n = cspace.getNumComponents(); 495 fvalue = new float[n]; 496 for (int i = 0; i < n; i++) { 497 if (components[i] < 0.0 || components[i] > 1.0) { 498 rangeError = true; 499 badComponentString = badComponentString + "Component " + i 500 + " "; 501 } else { 502 fvalue[i] = components[i]; 503 } 504 } 505 if (alpha < 0.0 || alpha > 1.0) { 506 rangeError = true; 507 badComponentString = badComponentString + "Alpha"; 508 } else { 509 falpha = alpha; 510 } 511 if (rangeError) { 512 throw new IllegalArgumentException ( 513 "Color parameter outside of expected range: " + 514 badComponentString); 515 } 516 frgbvalue = cspace.toRGB(fvalue); 517 cs = cspace; 518 value = ((((int)(falpha*255)) & 0xFF) << 24) | 519 ((((int)(frgbvalue[0]*255)) & 0xFF) << 16) | 520 ((((int)(frgbvalue[1]*255)) & 0xFF) << 8) | 521 ((((int)(frgbvalue[2]*255)) & 0xFF) << 0); 522 } 523 524 530 public int getRed() { 531 return (getRGB() >> 16) & 0xFF; 532 } 533 534 540 public int getGreen() { 541 return (getRGB() >> 8) & 0xFF; 542 } 543 544 550 public int getBlue() { 551 return (getRGB() >> 0) & 0xFF; 552 } 553 554 559 public int getAlpha() { 560 return (getRGB() >> 24) & 0xff; 561 } 562 563 576 public int getRGB() { 577 return value; 578 } 579 580 private static final double FACTOR = 0.7; 581 582 597 public Color brighter() { 598 int r = getRed(); 599 int g = getGreen(); 600 int b = getBlue(); 601 602 607 int i = (int)(1.0/(1.0-FACTOR)); 608 if ( r == 0 && g == 0 && b == 0) { 609 return new Color (i, i, i); 610 } 611 if ( r > 0 && r < i ) r = i; 612 if ( g > 0 && g < i ) g = i; 613 if ( b > 0 && b < i ) b = i; 614 615 return new Color (Math.min((int)(r/FACTOR), 255), 616 Math.min((int)(g/FACTOR), 255), 617 Math.min((int)(b/FACTOR), 255)); 618 } 619 620 635 public Color darker() { 636 return new Color (Math.max((int)(getRed() *FACTOR), 0), 637 Math.max((int)(getGreen()*FACTOR), 0), 638 Math.max((int)(getBlue() *FACTOR), 0)); 639 } 640 641 646 public int hashCode() { 647 return value; 648 } 649 650 663 public boolean equals(Object obj) { 664 return obj instanceof Color && ((Color )obj).value == this.value; 665 } 666 667 676 public String toString() { 677 return getClass().getName() + "[r=" + getRed() + ",g=" + getGreen() + ",b=" + getBlue() + "]"; 678 } 679 680 693 public static Color decode(String nm) throws NumberFormatException { 694 Integer intval = Integer.decode(nm); 695 int i = intval.intValue(); 696 return new Color ((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF); 697 } 698 699 717 public static Color getColor(String nm) { 718 return getColor(nm, null); 719 } 720 721 741 public static Color getColor(String nm, Color v) { 742 Integer intval = Integer.getInteger(nm); 743 if (intval == null) { 744 return v; 745 } 746 int i = intval.intValue(); 747 return new Color ((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF); 748 } 749 750 771 public static Color getColor(String nm, int v) { 772 Integer intval = Integer.getInteger(nm); 773 int i = (intval != null) ? intval.intValue() : v; 774 return new Color ((i >> 16) & 0xFF, (i >> 8) & 0xFF, (i >> 0) & 0xFF); 775 } 776 777 804 public static int HSBtoRGB(float hue, float saturation, float brightness) { 805 int r = 0, g = 0, b = 0; 806 if (saturation == 0) { 807 r = g = b = (int) (brightness * 255.0f + 0.5f); 808 } else { 809 float h = (hue - (float)Math.floor(hue)) * 6.0f; 810 float f = h - (float)java.lang.Math.floor(h); 811 float p = brightness * (1.0f - saturation); 812 float q = brightness * (1.0f - saturation * f); 813 float t = brightness * (1.0f - (saturation * (1.0f - f))); 814 switch ((int) h) { 815 case 0: 816 r = (int) (brightness * 255.0f + 0.5f); 817 g = (int) (t * 255.0f + 0.5f); 818 b = (int) (p * 255.0f + 0.5f); 819 break; 820 case 1: 821 r = (int) (q * 255.0f + 0.5f); 822 g = (int) (brightness * 255.0f + 0.5f); 823 b = (int) (p * 255.0f + 0.5f); 824 break; 825 case 2: 826 r = (int) (p * 255.0f + 0.5f); 827 g = (int) (brightness * 255.0f + 0.5f); 828 b = (int) (t * 255.0f + 0.5f); 829 break; 830 case 3: 831 r = (int) (p * 255.0f + 0.5f); 832 g = (int) (q * 255.0f + 0.5f); 833 b = (int) (brightness * 255.0f + 0.5f); 834 break; 835 case 4: 836 r = (int) (t * 255.0f + 0.5f); 837 g = (int) (p * 255.0f + 0.5f); 838 b = (int) (brightness * 255.0f + 0.5f); 839 break; 840 case 5: 841 r = (int) (brightness * 255.0f + 0.5f); 842 g = (int) (p * 255.0f + 0.5f); 843 b = (int) (q * 255.0f + 0.5f); 844 break; 845 } 846 } 847 return 0xff000000 | (r << 16) | (g << 8) | (b << 0); 848 } 849 850 872 public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) { 873 float hue, saturation, brightness; 874 if (hsbvals == null) { 875 hsbvals = new float[3]; 876 } 877 int cmax = (r > g) ? r : g; 878 if (b > cmax) cmax = b; 879 int cmin = (r < g) ? r : g; 880 if (b < cmin) cmin = b; 881 882 brightness = ((float) cmax) / 255.0f; 883 if (cmax != 0) 884 saturation = ((float) (cmax - cmin)) / ((float) cmax); 885 else 886 saturation = 0; 887 if (saturation == 0) 888 hue = 0; 889 else { 890 float redc = ((float) (cmax - r)) / ((float) (cmax - cmin)); 891 float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin)); 892 float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin)); 893 if (r == cmax) 894 hue = bluec - greenc; 895 else if (g == cmax) 896 hue = 2.0f + redc - bluec; 897 else 898 hue = 4.0f + greenc - redc; 899 hue = hue / 6.0f; 900 if (hue < 0) 901 hue = hue + 1.0f; 902 } 903 hsbvals[0] = hue; 904 hsbvals[1] = saturation; 905 hsbvals[2] = brightness; 906 return hsbvals; 907 } 908 909 927 public static Color getHSBColor(float h, float s, float b) { 928 return new Color (HSBtoRGB(h, s, b)); 929 } 930 931 943 public float[] getRGBComponents(float[] compArray) { 944 float[] f; 945 if (compArray == null) { 946 f = new float[4]; 947 } else { 948 f = compArray; 949 } 950 if (frgbvalue == null) { 951 f[0] = ((float)getRed())/255f; 952 f[1] = ((float)getGreen())/255f; 953 f[2] = ((float)getBlue())/255f; 954 f[3] = ((float)getAlpha())/255f; 955 } else { 956 f[0] = frgbvalue[0]; 957 f[1] = frgbvalue[1]; 958 f[2] = frgbvalue[2]; 959 f[3] = falpha; 960 } 961 return f; 962 } 963 964 975 public float[] getRGBColorComponents(float[] compArray) { 976 float[] f; 977 if (compArray == null) { 978 f = new float[3]; 979 } else { 980 f = compArray; 981 } 982 if (frgbvalue == null) { 983 f[0] = ((float)getRed())/255f; 984 f[1] = ((float)getGreen())/255f; 985 f[2] = ((float)getBlue())/255f; 986 } else { 987 f[0] = frgbvalue[0]; 988 f[1] = frgbvalue[1]; 989 f[2] = frgbvalue[2]; 990 } 991 return f; 992 } 993 994 1010 public float[] getComponents(float[] compArray) { 1011 if (fvalue == null) 1012 return getRGBComponents(compArray); 1013 float[] f; 1014 int n = fvalue.length; 1015 if (compArray == null) { 1016 f = new float[n + 1]; 1017 } else { 1018 f = compArray; 1019 } 1020 for (int i = 0; i < n; i++) { 1021 f[i] = fvalue[i]; 1022 } 1023 f[n] = falpha; 1024 return f; 1025 } 1026 1027 1042 public float[] getColorComponents(float[] compArray) { 1043 if (fvalue == null) 1044 return getRGBColorComponents(compArray); 1045 float[] f; 1046 int n = fvalue.length; 1047 if (compArray == null) { 1048 f = new float[n]; 1049 } else { 1050 f = compArray; 1051 } 1052 for (int i = 0; i < n; i++) { 1053 f[i] = fvalue[i]; 1054 } 1055 return f; 1056 } 1057 1058 1074 public float[] getComponents(ColorSpace cspace, float[] compArray) { 1075 if (cs == null) { 1076 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); 1077 } 1078 float f[]; 1079 if (fvalue == null) { 1080 f = new float[3]; 1081 f[0] = ((float)getRed())/255f; 1082 f[1] = ((float)getGreen())/255f; 1083 f[2] = ((float)getBlue())/255f; 1084 } else { 1085 f = fvalue; 1086 } 1087 float tmp[] = cs.toCIEXYZ(f); 1088 float tmpout[] = cspace.fromCIEXYZ(tmp); 1089 if (compArray == null) { 1090 compArray = new float[tmpout.length + 1]; 1091 } 1092 for (int i = 0 ; i < tmpout.length ; i++) { 1093 compArray[i] = tmpout[i]; 1094 } 1095 if (fvalue == null) { 1096 compArray[tmpout.length] = ((float)getAlpha())/255f; 1097 } else { 1098 compArray[tmpout.length] = falpha; 1099 } 1100 return compArray; 1101 } 1102 1103 1118 public float[] getColorComponents(ColorSpace cspace, float[] compArray) { 1119 if (cs == null) { 1120 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); 1121 } 1122 float f[]; 1123 if (fvalue == null) { 1124 f = new float[3]; 1125 f[0] = ((float)getRed())/255f; 1126 f[1] = ((float)getGreen())/255f; 1127 f[2] = ((float)getBlue())/255f; 1128 } else { 1129 f = fvalue; 1130 } 1131 float tmp[] = cs.toCIEXYZ(f); 1132 float tmpout[] = cspace.fromCIEXYZ(tmp); 1133 if (compArray == null) { 1134 return tmpout; 1135 } 1136 for (int i = 0 ; i < tmpout.length ; i++) { 1137 compArray[i] = tmpout[i]; 1138 } 1139 return compArray; 1140 } 1141 1142 1146 public ColorSpace getColorSpace() { 1147 if (cs == null) { 1148 cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); 1149 } 1150 return cs; 1151 } 1152 1153 1158 transient private PaintContext theContext; 1159 1160 1179 public synchronized PaintContext createContext(ColorModel cm, Rectangle r, 1180 Rectangle2D r2d, 1181 AffineTransform xform, 1182 RenderingHints hints) { 1183 PaintContext pc = theContext; 1184 if (pc == null) { 1185 pc = new ColorPaintContext (value, cm); 1186 theContext = pc; 1187 } 1188 return pc; 1189 } 1190 1191 1199 public int getTransparency() { 1200 int alpha = getAlpha(); 1201 if (alpha == 0xff) { 1202 return Transparency.OPAQUE; 1203 } 1204 else if (alpha == 0) { 1205 return Transparency.BITMASK; 1206 } 1207 else { 1208 return Transparency.TRANSLUCENT; 1209 } 1210 } 1211 1212} 1213 | Popular Tags |