UNPKG

3.67 kBJavaScriptView Raw
1'use strict';
2
3exports.type = 'perItem';
4
5exports.active = true;
6
7exports.description = 'converts colors: rgb() to #rrggbb and #rrggbb to #rgb';
8
9exports.params = {
10 currentColor: false,
11 names2hex: true,
12 rgb2hex: true,
13 shorthex: true,
14 shortname: true
15};
16
17var collections = require('./_collections'),
18 rNumber = '([+-]?(?:\\d*\\.\\d+|\\d+\\.?)%?)',
19 rComma = '\\s*,\\s*',
20 regRGB = new RegExp('^rgb\\(\\s*' + rNumber + rComma + rNumber + rComma + rNumber + '\\s*\\)$'),
21 regHEX = /^\#(([a-fA-F0-9])\2){3}$/,
22 none = /\bnone\b/i;
23
24/**
25 * Convert different colors formats in element attributes to hex.
26 *
27 * @see http://www.w3.org/TR/SVG/types.html#DataTypeColor
28 * @see http://www.w3.org/TR/SVG/single-page.html#types-ColorKeywords
29 *
30 * @example
31 * Convert color name keyword to long hex:
32 * fuchsia ➡ #ff00ff
33 *
34 * Convert rgb() to long hex:
35 * rgb(255, 0, 255) ➡ #ff00ff
36 * rgb(50%, 100, 100%) ➡ #7f64ff
37 *
38 * Convert long hex to short hex:
39 * #aabbcc ➡ #abc
40 *
41 * Convert hex to short name
42 * #000080 ➡ navy
43 *
44 * @param {Object} item current iteration item
45 * @param {Object} params plugin params
46 * @return {Boolean} if false, item will be filtered out
47 *
48 * @author Kir Belevich
49 */
50exports.fn = function(item, params) {
51
52 if (item.elem) {
53
54 item.eachAttr(function(attr) {
55
56 if (collections.colorsProps.indexOf(attr.name) > -1) {
57
58 var val = attr.value,
59 match;
60
61 // Convert colors to currentColor
62 if (params.currentColor) {
63 if (typeof params.currentColor === 'string') {
64 match = val === params.currentColor;
65 } else if (params.currentColor.exec) {
66 match = params.currentColor.exec(val);
67 } else {
68 match = !val.match(none);
69 }
70 if (match) {
71 val = 'currentColor';
72 }
73 }
74
75 // Convert color name keyword to long hex
76 if (params.names2hex && val.toLowerCase() in collections.colorsNames) {
77 val = collections.colorsNames[val.toLowerCase()];
78 }
79
80 // Convert rgb() to long hex
81 if (params.rgb2hex && (match = val.match(regRGB))) {
82 match = match.slice(1, 4).map(function(m) {
83 if (m.indexOf('%') > -1)
84 m = Math.round(parseFloat(m) * 2.55);
85
86 return Math.max(0, Math.min(m, 255));
87 });
88
89 val = rgb2hex(match);
90 }
91
92 // Convert long hex to short hex
93 if (params.shorthex && (match = val.match(regHEX))) {
94 val = '#' + match[0][1] + match[0][3] + match[0][5];
95 }
96
97 // Convert hex to short name
98 if (params.shortname) {
99 var lowerVal = val.toLowerCase();
100 if (lowerVal in collections.colorsShortNames) {
101 val = collections.colorsShortNames[lowerVal];
102 }
103 }
104
105 attr.value = val;
106
107 }
108
109 });
110
111 }
112
113};
114
115/**
116 * Convert [r, g, b] to #rrggbb.
117 *
118 * @see https://gist.github.com/983535
119 *
120 * @example
121 * rgb2hex([255, 255, 255]) // '#ffffff'
122 *
123 * @param {Array} rgb [r, g, b]
124 * @return {String} #rrggbb
125 *
126 * @author Jed Schmidt
127 */
128function rgb2hex(rgb) {
129 return '#' + ('00000' + (rgb[0] << 16 | rgb[1] << 8 | rgb[2]).toString(16)).slice(-6).toUpperCase();
130}