1 51 package org.apache.fop.datatypes; 52 53 import java.util.*; 54 import org.apache.fop.messaging.MessageHandler; 55 56 59 public class ColorType { 60 61 64 protected float red; 65 66 69 protected float green; 70 71 74 protected float blue; 75 76 79 protected float alpha = 0; 80 81 public ColorType(float red, float green, float blue) { 82 this.red = red; 83 this.green = green; 84 this.blue = blue; 85 } 86 87 91 public ColorType(String value) { 92 if (value.startsWith("#")) { 93 try { 94 if (value.length() == 4) { 95 this.red = Integer.parseInt(value.substring(1, 2), 16) 97 / 15f; 98 this.green = Integer.parseInt(value.substring(2, 3), 16) 99 / 15f; 100 this.blue = Integer.parseInt(value.substring(3), 16) 101 / 15f; 102 } else if (value.length() == 7) { 103 this.red = Integer.parseInt(value.substring(1, 3), 16) 105 / 255f; 106 this.green = Integer.parseInt(value.substring(3, 5), 16) 107 / 255f; 108 this.blue = Integer.parseInt(value.substring(5), 16) 109 / 255f; 110 } else { 111 this.red = 0; 112 this.green = 0; 113 this.blue = 0; 114 MessageHandler.errorln("unknown colour format. Must be #RGB or #RRGGBB"); 115 } 116 } catch (Exception e) { 117 this.red = 0; 118 this.green = 0; 119 this.blue = 0; 120 MessageHandler.errorln("unknown colour format. Must be #RGB or #RRGGBB"); 121 } 122 } else if (value.startsWith("rgb(")) { 123 int poss = value.indexOf("("); 124 int pose = value.indexOf(")"); 125 if (poss != -1 && pose != -1) { 126 value = value.substring(poss + 1, pose); 127 StringTokenizer st = new StringTokenizer(value, ","); 128 try { 129 if (st.hasMoreTokens()) { 130 String str = st.nextToken().trim(); 131 if (str.endsWith("%")) { 132 this.red = 133 Integer.parseInt(str.substring(0, str.length() - 1)) 134 * 2.55f; 135 } else { 136 this.red = Integer.parseInt(str) / 255f; 137 } 138 } 139 if (st.hasMoreTokens()) { 140 String str = st.nextToken().trim(); 141 if (str.endsWith("%")) { 142 this.green = 143 Integer.parseInt(str.substring(0, str.length() - 1)) 144 * 2.55f; 145 } else { 146 this.green = Integer.parseInt(str) / 255f; 147 } 148 } 149 if (st.hasMoreTokens()) { 150 String str = st.nextToken().trim(); 151 if (str.endsWith("%")) { 152 this.blue = 153 Integer.parseInt(str.substring(0, str.length() - 1)) 154 * 2.55f; 155 } else { 156 this.blue = Integer.parseInt(str) / 255f; 157 } 158 } 159 } catch (Exception e) { 160 this.red = 0; 161 this.green = 0; 162 this.blue = 0; 163 MessageHandler.errorln("unknown colour format. Must be #RGB or #RRGGBB"); 164 } 165 } 166 } else if (value.startsWith("url(")) { 167 } else { 169 if (value.toLowerCase().equals("transparent")) { 170 this.red = 0; 171 this.green = 0; 172 this.blue = 0; 173 this.alpha = 1; 174 } else { 175 boolean found = false; 176 for (int count = 0; count < names.length; count++) { 177 if (value.toLowerCase().equals(names[count])) { 178 this.red = vals[count][0] / 255f; 179 this.green = vals[count][1] / 255f; 180 this.blue = vals[count][2] / 255f; 181 found = true; 182 break; 183 } 184 } 185 if (!found) { 186 this.red = 0; 187 this.green = 0; 188 this.blue = 0; 189 MessageHandler.errorln("unknown colour name: " 190 + value); 191 } 192 } 193 } 194 } 195 196 public float blue() { 197 return this.blue; 198 } 199 200 public float green() { 201 return this.green; 202 } 203 204 public float red() { 205 return this.red; 206 } 207 208 public float alpha() { 209 return this.alpha; 210 } 211 212 static final String [] names = { 213 "aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", 214 "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown", 215 "burlywood", "cadetblue", "chartreuse", "chocolate", "coral", 216 "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", 217 "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkgrey", 218 "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", 219 "darkorchid", "darkred", "darksalmon", "darkseagreen", 220 "darkslateblue", "darkslategray", "darkslategrey", "darkturquoise", 221 "darkviolet", "deeppink", "deepskyblue", "dimgray", "dimgrey", 222 "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", 223 "gainsboro", "lightpink", "lightsalmon", "lightseagreen", 224 "lightskyblue", "lightslategray", "lightslategrey", "lightsteelblue", 225 "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon", 226 "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", 227 "mediumseagreen", "mediumslateblue", "mediumspringgreen", 228 "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream", 229 "mistyrose", "moccasin", "navajowhite", "navy", "oldlace", "olive", 230 "olivedrab", "orange", "orangered", "orchid", "palegoldenrod", 231 "palegreen", "paleturquoise", "palevioletred", "papayawhip", 232 "peachpuff", "peru", "pink", "plum", "powderblue", "purple", "red", 233 "rosybrown", "royalblue", "saddlebrown", "salmon", "ghostwhite", 234 "gold", "goldenrod", "gray", "grey", "green", "greenyellow", 235 "honeydew", "hotpink", "indianred", "indigo", "ivory", "khaki", 236 "lavender", "lavenderblush", "lawngreen", "lemonchiffon", 237 "lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow", 238 "lightgray", "lightgreen", "lightgrey", "sandybrown", "seagreen", 239 "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", 240 "slategrey", "snow", "springgreen", "steelblue", "tan", "teal", 241 "thistle", "tomato", "turquoise", "violet", "wheat", "white", 242 "whitesmoke", "yellow", "yellowgreen" 243 }; 244 245 static final int[][] vals = { 246 { 247 240, 248, 255 248 }, { 249 250, 235, 215 250 }, { 251 0, 255, 255 252 }, { 253 127, 255, 212 254 }, { 255 240, 255, 255 256 }, { 257 245, 245, 220 258 }, { 259 255, 228, 196 260 }, { 261 0, 0, 0 262 }, { 263 255, 235, 205 264 }, { 265 0, 0, 255 266 }, { 267 138, 43, 226 268 }, { 269 165, 42, 42 270 }, { 271 222, 184, 135 272 }, { 273 95, 158, 160 274 }, { 275 127, 255, 0 276 }, { 277 210, 105, 30 278 }, { 279 255, 127, 80 280 }, { 281 100, 149, 237 282 }, { 283 255, 248, 220 284 }, { 285 220, 20, 60 286 }, { 287 0, 255, 255 288 }, { 289 0, 0, 139 290 }, { 291 0, 139, 139 292 }, { 293 184, 134, 11 294 }, { 295 169, 169, 169 296 }, { 297 0, 100, 0 298 }, { 299 169, 169, 169 300 }, { 301 189, 183, 107 302 }, { 303 139, 0, 139 304 }, { 305 85, 107, 47 306 }, { 307 255, 140, 0 308 }, { 309 153, 50, 204 310 }, { 311 139, 0, 0 312 }, { 313 233, 150, 122 314 }, { 315 143, 188, 143 316 }, { 317 72, 61, 139 318 }, { 319 47, 79, 79 320 }, { 321 47, 79, 79 322 }, { 323 0, 206, 209 324 }, { 325 148, 0, 211 326 }, { 327 255, 20, 147 328 }, { 329 0, 191, 255 330 }, { 331 105, 105, 105 332 }, { 333 105, 105, 105 334 }, { 335 30, 144, 255 336 }, { 337 178, 34, 34 338 }, { 339 255, 250, 240 340 }, { 341 34, 139, 34 342 }, { 343 255, 0, 255 344 }, { 345 220, 220, 220 346 }, { 347 255, 182, 193 348 }, { 349 255, 160, 122 350 }, { 351 32, 178, 170 352 }, { 353 135, 206, 250 354 }, { 355 119, 136, 153 356 }, { 357 119, 136, 153 358 }, { 359 176, 196, 222 360 }, { 361 255, 255, 224 362 }, { 363 0, 255, 0 364 }, { 365 50, 205, 50 366 }, { 367 250, 240, 230 368 }, { 369 255, 0, 255 370 }, { 371 128, 0, 0 372 }, { 373 102, 205, 170 374 }, { 375 0, 0, 205 376 }, { 377 186, 85, 211 378 }, { 379 147, 112, 219 380 }, { 381 60, 179, 113 382 }, { 383 123, 104, 238 384 }, { 385 0, 250, 154 386 }, { 387 72, 209, 204 388 }, { 389 199, 21, 133 390 }, { 391 25, 25, 112 392 }, { 393 245, 255, 250 394 }, { 395 255, 228, 225 396 }, { 397 255, 228, 181 398 }, { 399 255, 222, 173 400 }, { 401 0, 0, 128 402 }, { 403 253, 245, 230 404 }, { 405 128, 128, 0 406 }, { 407 107, 142, 35 408 }, { 409 255, 165, 0 410 }, { 411 255, 69, 0 412 }, { 413 218, 112, 214 414 }, { 415 238, 232, 170 416 }, { 417 152, 251, 152 418 }, { 419 175, 238, 238 420 }, { 421 219, 112, 147 422 }, { 423 255, 239, 213 424 }, { 425 255, 218, 185 426 }, { 427 205, 133, 63 428 }, { 429 255, 192, 203 430 }, { 431 221, 160, 221 432 }, { 433 176, 224, 230 434 }, { 435 128, 0, 128 436 }, { 437 255, 0, 0 438 }, { 439 188, 143, 143 440 }, { 441 65, 105, 225 442 }, { 443 139, 69, 19 444 }, { 445 250, 128, 114 446 }, { 447 248, 248, 255 448 }, { 449 255, 215, 0 450 }, { 451 218, 165, 32 452 }, { 453 128, 128, 128 454 }, { 455 128, 128, 128 456 }, { 457 0, 128, 0 458 }, { 459 173, 255, 47 460 }, { 461 240, 255, 240 462 }, { 463 255, 105, 180 464 }, { 465 205, 92, 92 466 }, { 467 75, 0, 130 468 }, { 469 255, 255, 240 470 }, { 471 240, 230, 140 472 }, { 473 230, 230, 250 474 }, { 475 255, 240, 245 476 }, { 477 124, 252, 0 478 }, { 479 255, 250, 205 480 }, { 481 173, 216, 230 482 }, { 483 240, 128, 128 484 }, { 485 224, 255, 255 486 }, { 487 250, 250, 210 488 }, { 489 211, 211, 211 490 }, { 491 144, 238, 144 492 }, { 493 211, 211, 211 494 }, { 495 244, 164, 96 496 }, { 497 46, 139, 87 498 }, { 499 255, 245, 238 500 }, { 501 160, 82, 45 502 }, { 503 192, 192, 192 504 }, { 505 135, 206, 235 506 }, { 507 106, 90, 205 508 }, { 509 112, 128, 144 510 }, { 511 112, 128, 144 512 }, { 513 255, 250, 250 514 }, { 515 0, 255, 127 516 }, { 517 70, 130, 180 518 }, { 519 210, 180, 140 520 }, { 521 0, 128, 128 522 }, { 523 216, 191, 216 524 }, { 525 255, 99, 71 526 }, { 527 64, 224, 208 528 }, { 529 238, 130, 238 530 }, { 531 245, 222, 179 532 }, { 533 255, 255, 255 534 }, { 535 245, 245, 245 536 }, { 537 255, 255, 0 538 }, { 539 154, 205, 50 540 } 541 }; 542 } 543 544 693 | Popular Tags |