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