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.

189 lines
4.4 KiB

4 years ago
  1. var COLORS = {
  2. aliceblue: '#f0f8ff',
  3. antiquewhite: '#faebd7',
  4. aqua: '#0ff',
  5. aquamarine: '#7fffd4',
  6. azure: '#f0ffff',
  7. beige: '#f5f5dc',
  8. bisque: '#ffe4c4',
  9. black: '#000',
  10. blanchedalmond: '#ffebcd',
  11. blue: '#00f',
  12. blueviolet: '#8a2be2',
  13. brown: '#a52a2a',
  14. burlywood: '#deb887',
  15. cadetblue: '#5f9ea0',
  16. chartreuse: '#7fff00',
  17. chocolate: '#d2691e',
  18. coral: '#ff7f50',
  19. cornflowerblue: '#6495ed',
  20. cornsilk: '#fff8dc',
  21. crimson: '#dc143c',
  22. cyan: '#0ff',
  23. darkblue: '#00008b',
  24. darkcyan: '#008b8b',
  25. darkgoldenrod: '#b8860b',
  26. darkgray: '#a9a9a9',
  27. darkgreen: '#006400',
  28. darkgrey: '#a9a9a9',
  29. darkkhaki: '#bdb76b',
  30. darkmagenta: '#8b008b',
  31. darkolivegreen: '#556b2f',
  32. darkorange: '#ff8c00',
  33. darkorchid: '#9932cc',
  34. darkred: '#8b0000',
  35. darksalmon: '#e9967a',
  36. darkseagreen: '#8fbc8f',
  37. darkslateblue: '#483d8b',
  38. darkslategray: '#2f4f4f',
  39. darkslategrey: '#2f4f4f',
  40. darkturquoise: '#00ced1',
  41. darkviolet: '#9400d3',
  42. deeppink: '#ff1493',
  43. deepskyblue: '#00bfff',
  44. dimgray: '#696969',
  45. dimgrey: '#696969',
  46. dodgerblue: '#1e90ff',
  47. firebrick: '#b22222',
  48. floralwhite: '#fffaf0',
  49. forestgreen: '#228b22',
  50. fuchsia: '#f0f',
  51. gainsboro: '#dcdcdc',
  52. ghostwhite: '#f8f8ff',
  53. gold: '#ffd700',
  54. goldenrod: '#daa520',
  55. gray: '#808080',
  56. green: '#008000',
  57. greenyellow: '#adff2f',
  58. grey: '#808080',
  59. honeydew: '#f0fff0',
  60. hotpink: '#ff69b4',
  61. indianred: '#cd5c5c',
  62. indigo: '#4b0082',
  63. ivory: '#fffff0',
  64. khaki: '#f0e68c',
  65. lavender: '#e6e6fa',
  66. lavenderblush: '#fff0f5',
  67. lawngreen: '#7cfc00',
  68. lemonchiffon: '#fffacd',
  69. lightblue: '#add8e6',
  70. lightcoral: '#f08080',
  71. lightcyan: '#e0ffff',
  72. lightgoldenrodyellow: '#fafad2',
  73. lightgray: '#d3d3d3',
  74. lightgreen: '#90ee90',
  75. lightgrey: '#d3d3d3',
  76. lightpink: '#ffb6c1',
  77. lightsalmon: '#ffa07a',
  78. lightseagreen: '#20b2aa',
  79. lightskyblue: '#87cefa',
  80. lightslategray: '#778899',
  81. lightslategrey: '#778899',
  82. lightsteelblue: '#b0c4de',
  83. lightyellow: '#ffffe0',
  84. lime: '#0f0',
  85. limegreen: '#32cd32',
  86. linen: '#faf0e6',
  87. magenta: '#ff00ff',
  88. maroon: '#800000',
  89. mediumaquamarine: '#66cdaa',
  90. mediumblue: '#0000cd',
  91. mediumorchid: '#ba55d3',
  92. mediumpurple: '#9370db',
  93. mediumseagreen: '#3cb371',
  94. mediumslateblue: '#7b68ee',
  95. mediumspringgreen: '#00fa9a',
  96. mediumturquoise: '#48d1cc',
  97. mediumvioletred: '#c71585',
  98. midnightblue: '#191970',
  99. mintcream: '#f5fffa',
  100. mistyrose: '#ffe4e1',
  101. moccasin: '#ffe4b5',
  102. navajowhite: '#ffdead',
  103. navy: '#000080',
  104. oldlace: '#fdf5e6',
  105. olive: '#808000',
  106. olivedrab: '#6b8e23',
  107. orange: '#ffa500',
  108. orangered: '#ff4500',
  109. orchid: '#da70d6',
  110. palegoldenrod: '#eee8aa',
  111. palegreen: '#98fb98',
  112. paleturquoise: '#afeeee',
  113. palevioletred: '#db7093',
  114. papayawhip: '#ffefd5',
  115. peachpuff: '#ffdab9',
  116. peru: '#cd853f',
  117. pink: '#ffc0cb',
  118. plum: '#dda0dd',
  119. powderblue: '#b0e0e6',
  120. purple: '#800080',
  121. rebeccapurple: '#663399',
  122. red: '#f00',
  123. rosybrown: '#bc8f8f',
  124. royalblue: '#4169e1',
  125. saddlebrown: '#8b4513',
  126. salmon: '#fa8072',
  127. sandybrown: '#f4a460',
  128. seagreen: '#2e8b57',
  129. seashell: '#fff5ee',
  130. sienna: '#a0522d',
  131. silver: '#c0c0c0',
  132. skyblue: '#87ceeb',
  133. slateblue: '#6a5acd',
  134. slategray: '#708090',
  135. slategrey: '#708090',
  136. snow: '#fffafa',
  137. springgreen: '#00ff7f',
  138. steelblue: '#4682b4',
  139. tan: '#d2b48c',
  140. teal: '#008080',
  141. thistle: '#d8bfd8',
  142. tomato: '#ff6347',
  143. turquoise: '#40e0d0',
  144. violet: '#ee82ee',
  145. wheat: '#f5deb3',
  146. white: '#fff',
  147. whitesmoke: '#f5f5f5',
  148. yellow: '#ff0',
  149. yellowgreen: '#9acd32'
  150. };
  151. var toHex = {};
  152. var toName = {};
  153. for (var name in COLORS) {
  154. var hex = COLORS[name];
  155. if (name.length < hex.length) {
  156. toName[hex] = name;
  157. } else {
  158. toHex[name] = hex;
  159. }
  160. }
  161. var toHexPattern = new RegExp('(^| |,|\\))(' + Object.keys(toHex).join('|') + ')( |,|\\)|$)', 'ig');
  162. var toNamePattern = new RegExp('(' + Object.keys(toName).join('|') + ')([^a-f0-9]|$)', 'ig');
  163. function hexConverter(match, prefix, colorValue, suffix) {
  164. return prefix + toHex[colorValue.toLowerCase()] + suffix;
  165. }
  166. function nameConverter(match, colorValue, suffix) {
  167. return toName[colorValue.toLowerCase()] + suffix;
  168. }
  169. function shortenHex(value) {
  170. var hasHex = value.indexOf('#') > -1;
  171. var shortened = value.replace(toHexPattern, hexConverter);
  172. if (shortened != value) {
  173. shortened = shortened.replace(toHexPattern, hexConverter);
  174. }
  175. return hasHex ?
  176. shortened.replace(toNamePattern, nameConverter) :
  177. shortened;
  178. }
  179. module.exports = shortenHex;