/chronoscope-api/src/main/java/org/timepedia/chronoscope/client/canvas/Color.java

http://gwt-chronoscope.googlecode.com/ · Java · 220 lines · 195 code · 16 blank · 9 comment · 3 complexity · 21e1cfd8cae93fc33893529cb257b794 MD5 · raw file

  1. package org.timepedia.chronoscope.client.canvas;
  2. import java.util.HashMap;
  3. /**
  4. * Represents a PaintStyle which is an RGBA color
  5. */
  6. public class Color implements PaintStyle {
  7. /**
  8. * List of supported named colors in Chronoscope.
  9. *
  10. * The list has been taken from:
  11. * http://www.w3schools.com/html/html_colornames.asp
  12. */
  13. public static HashMap<String, String> colors = new HashMap<String, String>() {
  14. private static final long serialVersionUID = 1L;
  15. {
  16. put("aliceblue", "f0f8ff");
  17. put("antiquewhite", "faebd7");
  18. put("aqua", "00ffff");
  19. put("aquamarine", "7fffd4");
  20. put("azure", "f0ffff");
  21. put("beige", "f5f5dc");
  22. put("bisque", "ffe4c4");
  23. put("black", "000000");
  24. put("blanchedalmond", "ffebcd");
  25. put("blue", "0000ff");
  26. put("blueviolet", "8a2be2");
  27. put("brown", "a52a2a");
  28. put("burlywood", "deb887");
  29. put("cadetblue", "5f9ea0");
  30. put("chartreuse", "7fff00");
  31. put("chocolate", "d2691e");
  32. put("coral", "ff7f50");
  33. put("cornflowerblue", "6495ed");
  34. put("cornsilk", "fff8dc");
  35. put("crimson", "dc143c");
  36. put("cyan", "00ffff");
  37. put("darkblue", "00008b");
  38. put("darkcyan", "008b8b");
  39. put("darkgoldenrod", "b8860b");
  40. put("darkgray", "a9a9a9");
  41. put("darkgreen", "006400");
  42. put("darkkhaki", "bdb76b");
  43. put("darkmagenta", "8b008b");
  44. put("darkolivegreen", "556b2f");
  45. put("darkorange", "ff8c00");
  46. put("darkorchid", "9932cc");
  47. put("darkred", "8b0000");
  48. put("darksalmon", "e9967a");
  49. put("darkseagreen", "8fbc8f");
  50. put("darkslateblue", "483d8b");
  51. put("darkslategray", "2f4f4f");
  52. put("darkturquoise", "00ced1");
  53. put("darkviolet", "9400d3");
  54. put("deeppink", "ff1493");
  55. put("deepskyblue", "00bfff");
  56. put("dimgray", "696969");
  57. put("dodgerblue", "1e90ff");
  58. put("firebrick", "b22222");
  59. put("floralwhite", "fffaf0");
  60. put("forestgreen", "228b22");
  61. put("fuchsia", "ff00ff");
  62. put("gainsboro", "dcdcdc");
  63. put("ghostwhite", "f8f8ff");
  64. put("gold", "ffd700");
  65. put("goldenrod", "daa520");
  66. put("gray", "808080");
  67. put("green", "008000");
  68. put("greenyellow", "adff2f");
  69. put("honeydew", "f0fff0");
  70. put("hotpink", "ff69b4");
  71. put("indianred", "cd5c5c");
  72. put("indigo", "4b0082");
  73. put("ivory", "fffff0");
  74. put("khaki", "f0e68c");
  75. put("lavender", "e6e6fa");
  76. put("lavenderblush", "fff0f5");
  77. put("lawngreen", "7cfc00");
  78. put("lemonchiffon", "fffacd");
  79. put("lightblue", "add8e6");
  80. put("lightcoral", "f08080");
  81. put("lightcyan", "e0ffff");
  82. put("lightgoldenrodyellow", "fafad2");
  83. put("lightgray", "d3d3d3");
  84. put("lightgreen", "90ee90");
  85. put("lightpink", "ffb6c1");
  86. put("lightsalmon", "ffa07a");
  87. put("lightseagreen", "20b2aa");
  88. put("lightskyblue", "87cefa");
  89. put("lightslategray", "778899");
  90. put("lightsteelblue", "b0c4de");
  91. put("lightyellow", "ffffe0");
  92. put("lime", "00ff00");
  93. put("limegreen", "32cd32");
  94. put("linen", "faf0e6");
  95. put("magenta", "ff00ff");
  96. put("maroon", "800000");
  97. put("mediumaquamarine", "66cdaa");
  98. put("mediumblue", "0000cd");
  99. put("mediumorchid", "ba55d3");
  100. put("mediumpurple", "9370d8");
  101. put("mediumseagreen", "3cb371");
  102. put("mediumslateblue", "7b68ee");
  103. put("mediumspringgreen", "00fa9a");
  104. put("mediumturquoise", "48d1cc");
  105. put("mediumvioletred", "c71585");
  106. put("midnightblue", "191970");
  107. put("mintcream", "f5fffa");
  108. put("mistyrose", "ffe4e1");
  109. put("moccasin", "ffe4b5");
  110. put("navajowhite", "ffdead");
  111. put("navy", "000080");
  112. put("oldlace", "fdf5e6");
  113. put("olive", "808000");
  114. put("olivedrab", "6b8e23");
  115. put("orange", "ffa500");
  116. put("orangered", "ff4500");
  117. put("orchid", "da70d6");
  118. put("palegoldenrod", "eee8aa");
  119. put("palegreen", "98fb98");
  120. put("paleturquoise", "afeeee");
  121. put("palevioletred", "d87093");
  122. put("papayawhip", "ffefd5");
  123. put("peachpuff", "ffdab9");
  124. put("peru", "cd853f");
  125. put("pink", "ffc0cb");
  126. put("plum", "dda0dd");
  127. put("powderblue", "b0e0e6");
  128. put("purple", "800080");
  129. put("red", "ff0000");
  130. put("rosybrown", "bc8f8f");
  131. put("royalblue", "4169e1");
  132. put("saddlebrown", "8b4513");
  133. put("salmon", "fa8072");
  134. put("sandybrown", "f4a460");
  135. put("seagreen", "2e8b57");
  136. put("seashell", "fff5ee");
  137. put("sienna", "a0522d");
  138. put("silver", "c0c0c0");
  139. put("skyblue", "87ceeb");
  140. put("slateblue", "6a5acd");
  141. put("slategray", "708090");
  142. put("snow", "fffafa");
  143. put("springgreen", "00ff7f");
  144. put("steelblue", "4682b4");
  145. put("tan", "d2b48c");
  146. put("teal", "008080");
  147. put("thistle", "d8bfd8");
  148. put("tomato", "ff6347");
  149. put("turquoise", "40e0d0");
  150. put("violet", "ee82ee");
  151. put("wheat", "f5deb3");
  152. put("white", "ffffff");
  153. put("whitesmoke", "f5f5f5");
  154. put("yellow", "ffff00");
  155. put("yellowgreen", "9acd32");
  156. }
  157. };
  158. private final String color;
  159. private boolean rgbaSet = false;
  160. public static final Color TRANSPARENT = new Color(0, 0, 0, 0);
  161. public static final Color WHITE = new Color("white");
  162. public static final Color BLACK = new Color("black");
  163. public static final Color DIMGRAY = new Color("dimgray");
  164. public static final Color GREEN = new Color("green");
  165. public static final Color LIGHTGRAY = new Color("lightgray");
  166. public static final Color GRAY = new Color("gray");
  167. private int rgba = 0;
  168. public Color(int r, int g, int b) {
  169. this(r, g, b, 255);
  170. }
  171. public Color(int r, int g, int b, int a) {
  172. setRgba(r, g, b, a);
  173. this.rgbaSet = true;
  174. this.color = "rgba(" + r + "," + g + "," + b + "," + a + ")";
  175. }
  176. public Color(String color) {
  177. if (!color.startsWith("#")) {
  178. String rgba = colors.get(color);
  179. if (rgba != null) {
  180. color = "#" + rgba;
  181. }
  182. }
  183. this.color = color;
  184. }
  185. public String getCSSColor() {
  186. return color;
  187. }
  188. public int getRGBA() {
  189. return this.rgba;
  190. }
  191. public boolean isRgbaSet() {
  192. return rgbaSet;
  193. }
  194. public void setRgba(int rgba) {
  195. this.rgba = rgba;
  196. rgbaSet = true;
  197. }
  198. private void setRgba(int r, int g, int b, int a) {
  199. this.rgba = a << 24 | r << 16 | g << 8 | b;
  200. }
  201. public String toString() {
  202. return color;
  203. }
  204. }