libs/rgbcolor.js

  1. /**
  2. * A class to parse color values
  3. * @author Stoyan Stefanov <sstoo@gmail.com>
  4. * {@link http://www.phpied.com/rgb-color-parser-in-javascript/}
  5. * @license Use it if you like it
  6. */
  7. function RGBColor(color_string) {
  8. color_string = color_string || "";
  9. this.ok = false;
  10. // strip any leading #
  11. if (color_string.charAt(0) == "#") {
  12. // remove # if any
  13. color_string = color_string.substr(1, 6);
  14. }
  15. color_string = color_string.replace(/ /g, "");
  16. color_string = color_string.toLowerCase();
  17. var channels;
  18. // before getting into regexps, try simple matches
  19. // and overwrite the input
  20. var simple_colors = {
  21. aliceblue: "f0f8ff",
  22. antiquewhite: "faebd7",
  23. aqua: "00ffff",
  24. aquamarine: "7fffd4",
  25. azure: "f0ffff",
  26. beige: "f5f5dc",
  27. bisque: "ffe4c4",
  28. black: "000000",
  29. blanchedalmond: "ffebcd",
  30. blue: "0000ff",
  31. blueviolet: "8a2be2",
  32. brown: "a52a2a",
  33. burlywood: "deb887",
  34. cadetblue: "5f9ea0",
  35. chartreuse: "7fff00",
  36. chocolate: "d2691e",
  37. coral: "ff7f50",
  38. cornflowerblue: "6495ed",
  39. cornsilk: "fff8dc",
  40. crimson: "dc143c",
  41. cyan: "00ffff",
  42. darkblue: "00008b",
  43. darkcyan: "008b8b",
  44. darkgoldenrod: "b8860b",
  45. darkgray: "a9a9a9",
  46. darkgreen: "006400",
  47. darkkhaki: "bdb76b",
  48. darkmagenta: "8b008b",
  49. darkolivegreen: "556b2f",
  50. darkorange: "ff8c00",
  51. darkorchid: "9932cc",
  52. darkred: "8b0000",
  53. darksalmon: "e9967a",
  54. darkseagreen: "8fbc8f",
  55. darkslateblue: "483d8b",
  56. darkslategray: "2f4f4f",
  57. darkturquoise: "00ced1",
  58. darkviolet: "9400d3",
  59. deeppink: "ff1493",
  60. deepskyblue: "00bfff",
  61. dimgray: "696969",
  62. dodgerblue: "1e90ff",
  63. feldspar: "d19275",
  64. firebrick: "b22222",
  65. floralwhite: "fffaf0",
  66. forestgreen: "228b22",
  67. fuchsia: "ff00ff",
  68. gainsboro: "dcdcdc",
  69. ghostwhite: "f8f8ff",
  70. gold: "ffd700",
  71. goldenrod: "daa520",
  72. gray: "808080",
  73. green: "008000",
  74. greenyellow: "adff2f",
  75. honeydew: "f0fff0",
  76. hotpink: "ff69b4",
  77. indianred: "cd5c5c",
  78. indigo: "4b0082",
  79. ivory: "fffff0",
  80. khaki: "f0e68c",
  81. lavender: "e6e6fa",
  82. lavenderblush: "fff0f5",
  83. lawngreen: "7cfc00",
  84. lemonchiffon: "fffacd",
  85. lightblue: "add8e6",
  86. lightcoral: "f08080",
  87. lightcyan: "e0ffff",
  88. lightgoldenrodyellow: "fafad2",
  89. lightgrey: "d3d3d3",
  90. lightgreen: "90ee90",
  91. lightpink: "ffb6c1",
  92. lightsalmon: "ffa07a",
  93. lightseagreen: "20b2aa",
  94. lightskyblue: "87cefa",
  95. lightslateblue: "8470ff",
  96. lightslategray: "778899",
  97. lightsteelblue: "b0c4de",
  98. lightyellow: "ffffe0",
  99. lime: "00ff00",
  100. limegreen: "32cd32",
  101. linen: "faf0e6",
  102. magenta: "ff00ff",
  103. maroon: "800000",
  104. mediumaquamarine: "66cdaa",
  105. mediumblue: "0000cd",
  106. mediumorchid: "ba55d3",
  107. mediumpurple: "9370d8",
  108. mediumseagreen: "3cb371",
  109. mediumslateblue: "7b68ee",
  110. mediumspringgreen: "00fa9a",
  111. mediumturquoise: "48d1cc",
  112. mediumvioletred: "c71585",
  113. midnightblue: "191970",
  114. mintcream: "f5fffa",
  115. mistyrose: "ffe4e1",
  116. moccasin: "ffe4b5",
  117. navajowhite: "ffdead",
  118. navy: "000080",
  119. oldlace: "fdf5e6",
  120. olive: "808000",
  121. olivedrab: "6b8e23",
  122. orange: "ffa500",
  123. orangered: "ff4500",
  124. orchid: "da70d6",
  125. palegoldenrod: "eee8aa",
  126. palegreen: "98fb98",
  127. paleturquoise: "afeeee",
  128. palevioletred: "d87093",
  129. papayawhip: "ffefd5",
  130. peachpuff: "ffdab9",
  131. peru: "cd853f",
  132. pink: "ffc0cb",
  133. plum: "dda0dd",
  134. powderblue: "b0e0e6",
  135. purple: "800080",
  136. red: "ff0000",
  137. rosybrown: "bc8f8f",
  138. royalblue: "4169e1",
  139. saddlebrown: "8b4513",
  140. salmon: "fa8072",
  141. sandybrown: "f4a460",
  142. seagreen: "2e8b57",
  143. seashell: "fff5ee",
  144. sienna: "a0522d",
  145. silver: "c0c0c0",
  146. skyblue: "87ceeb",
  147. slateblue: "6a5acd",
  148. slategray: "708090",
  149. snow: "fffafa",
  150. springgreen: "00ff7f",
  151. steelblue: "4682b4",
  152. tan: "d2b48c",
  153. teal: "008080",
  154. thistle: "d8bfd8",
  155. tomato: "ff6347",
  156. turquoise: "40e0d0",
  157. violet: "ee82ee",
  158. violetred: "d02090",
  159. wheat: "f5deb3",
  160. white: "ffffff",
  161. whitesmoke: "f5f5f5",
  162. yellow: "ffff00",
  163. yellowgreen: "9acd32"
  164. };
  165. color_string = simple_colors[color_string] || color_string;
  166. // array of color definition objects
  167. var color_defs = [
  168. {
  169. re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
  170. example: ["rgb(123, 234, 45)", "rgb(255,234,245)"],
  171. process: function(bits) {
  172. return [parseInt(bits[1]), parseInt(bits[2]), parseInt(bits[3])];
  173. }
  174. },
  175. {
  176. re: /^(\w{2})(\w{2})(\w{2})$/,
  177. example: ["#00ff00", "336699"],
  178. process: function(bits) {
  179. return [
  180. parseInt(bits[1], 16),
  181. parseInt(bits[2], 16),
  182. parseInt(bits[3], 16)
  183. ];
  184. }
  185. },
  186. {
  187. re: /^(\w{1})(\w{1})(\w{1})$/,
  188. example: ["#fb0", "f0f"],
  189. process: function(bits) {
  190. return [
  191. parseInt(bits[1] + bits[1], 16),
  192. parseInt(bits[2] + bits[2], 16),
  193. parseInt(bits[3] + bits[3], 16)
  194. ];
  195. }
  196. }
  197. ];
  198. // search through the definitions to find a match
  199. for (var i = 0; i < color_defs.length; i++) {
  200. var re = color_defs[i].re;
  201. var processor = color_defs[i].process;
  202. var bits = re.exec(color_string);
  203. if (bits) {
  204. channels = processor(bits);
  205. this.r = channels[0];
  206. this.g = channels[1];
  207. this.b = channels[2];
  208. this.ok = true;
  209. }
  210. }
  211. // validate/cleanup values
  212. this.r = this.r < 0 || isNaN(this.r) ? 0 : this.r > 255 ? 255 : this.r;
  213. this.g = this.g < 0 || isNaN(this.g) ? 0 : this.g > 255 ? 255 : this.g;
  214. this.b = this.b < 0 || isNaN(this.b) ? 0 : this.b > 255 ? 255 : this.b;
  215. // some getters
  216. this.toRGB = function() {
  217. return "rgb(" + this.r + ", " + this.g + ", " + this.b + ")";
  218. };
  219. this.toHex = function() {
  220. var r = this.r.toString(16);
  221. var g = this.g.toString(16);
  222. var b = this.b.toString(16);
  223. if (r.length == 1) r = "0" + r;
  224. if (g.length == 1) g = "0" + g;
  225. if (b.length == 1) b = "0" + b;
  226. return "#" + r + g + b;
  227. };
  228. }
  229. export { RGBColor };