You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColorType.java 18KB

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