/src/main/resources/editors/svg-edit-2.6/canvg/rgbcolor.js

https://bitbucket.org/eddiewebb/confluence-scribbles · JavaScript · 287 lines · 256 code · 14 blank · 17 comment · 20 complexity · 541f1242faed652fa675fe61097ca066 MD5 · raw file

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