1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.hexToRgb = hexToRgb;
|
7 | exports.rgbToHex = rgbToHex;
|
8 | exports.hslToRgb = hslToRgb;
|
9 | exports.decomposeColor = decomposeColor;
|
10 | exports.recomposeColor = recomposeColor;
|
11 | exports.getContrastRatio = getContrastRatio;
|
12 | exports.getLuminance = getLuminance;
|
13 | exports.emphasize = emphasize;
|
14 | exports.fade = fade;
|
15 | exports.darken = darken;
|
16 | exports.lighten = lighten;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function clamp(value) {
|
29 | var min = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
30 | var max = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
31 |
|
32 | if (process.env.NODE_ENV !== 'production') {
|
33 | if (value < min || value > max) {
|
34 | console.error("Material-UI: the value provided ".concat(value, " is out of range [").concat(min, ", ").concat(max, "]."));
|
35 | }
|
36 | }
|
37 |
|
38 | if (value < min) {
|
39 | return min;
|
40 | }
|
41 |
|
42 | if (value > max) {
|
43 | return max;
|
44 | }
|
45 |
|
46 | return value;
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function hexToRgb(color) {
|
57 | color = color.substr(1);
|
58 | var re = new RegExp(".{1,".concat(color.length / 3, "}"), 'g');
|
59 | var colors = color.match(re);
|
60 |
|
61 | if (colors && colors[0].length === 1) {
|
62 | colors = colors.map(function (n) {
|
63 | return n + n;
|
64 | });
|
65 | }
|
66 |
|
67 | return colors ? "rgb(".concat(colors.map(function (n) {
|
68 | return parseInt(n, 16);
|
69 | }).join(', '), ")") : '';
|
70 | }
|
71 |
|
72 | function intToHex(int) {
|
73 | var hex = int.toString(16);
|
74 | return hex.length === 1 ? "0".concat(hex) : hex;
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function rgbToHex(color) {
|
85 |
|
86 | if (color.indexOf('#') === 0) {
|
87 | return color;
|
88 | }
|
89 |
|
90 | var _decomposeColor = decomposeColor(color),
|
91 | values = _decomposeColor.values;
|
92 |
|
93 | return "#".concat(values.map(function (n) {
|
94 | return intToHex(n);
|
95 | }).join(''));
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function hslToRgb(color) {
|
106 | color = decomposeColor(color);
|
107 | var _color = color,
|
108 | values = _color.values;
|
109 | var h = values[0];
|
110 | var s = values[1] / 100;
|
111 | var l = values[2] / 100;
|
112 | var a = s * Math.min(l, 1 - l);
|
113 |
|
114 | var f = function f(n) {
|
115 | var k = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (n + h / 30) % 12;
|
116 | return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
117 | };
|
118 |
|
119 | var type = 'rgb';
|
120 | var rgb = [Math.round(f(0) * 255), Math.round(f(8) * 255), Math.round(f(4) * 255)];
|
121 |
|
122 | if (color.type === 'hsla') {
|
123 | type += 'a';
|
124 | rgb.push(values[3]);
|
125 | }
|
126 |
|
127 | return recomposeColor({
|
128 | type: type,
|
129 | values: rgb
|
130 | });
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function decomposeColor(color) {
|
143 |
|
144 | if (color.type) {
|
145 | return color;
|
146 | }
|
147 |
|
148 | if (color.charAt(0) === '#') {
|
149 | return decomposeColor(hexToRgb(color));
|
150 | }
|
151 |
|
152 | var marker = color.indexOf('(');
|
153 | var type = color.substring(0, marker);
|
154 |
|
155 | if (['rgb', 'rgba', 'hsl', 'hsla'].indexOf(type) === -1) {
|
156 | throw new Error(["Material-UI: unsupported `".concat(color, "` color."), 'We support the following formats: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla().'].join('\n'));
|
157 | }
|
158 |
|
159 | var values = color.substring(marker + 1, color.length - 1).split(',');
|
160 | values = values.map(function (value) {
|
161 | return parseFloat(value);
|
162 | });
|
163 | return {
|
164 | type: type,
|
165 | values: values
|
166 | };
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | function recomposeColor(color) {
|
179 | var type = color.type;
|
180 | var values = color.values;
|
181 |
|
182 | if (type.indexOf('rgb') !== -1) {
|
183 |
|
184 | values = values.map(function (n, i) {
|
185 | return i < 3 ? parseInt(n, 10) : n;
|
186 | });
|
187 | } else if (type.indexOf('hsl') !== -1) {
|
188 | values[1] = "".concat(values[1], "%");
|
189 | values[2] = "".concat(values[2], "%");
|
190 | }
|
191 |
|
192 | return "".concat(type, "(").concat(values.join(', '), ")");
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | function getContrastRatio(foreground, background) {
|
206 | var lumA = getLuminance(foreground);
|
207 | var lumB = getLuminance(background);
|
208 | return (Math.max(lumA, lumB) + 0.05) / (Math.min(lumA, lumB) + 0.05);
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | function getLuminance(color) {
|
222 | color = decomposeColor(color);
|
223 | var rgb = color.type === 'hsl' ? decomposeColor(hslToRgb(color)).values : color.values;
|
224 | rgb = rgb.map(function (val) {
|
225 | val /= 255;
|
226 |
|
227 | return val <= 0.03928 ? val / 12.92 : Math.pow((val + 0.055) / 1.055, 2.4);
|
228 | });
|
229 |
|
230 | return Number((0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]).toFixed(3));
|
231 | }
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | function emphasize(color) {
|
243 | var coefficient = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0.15;
|
244 | return getLuminance(color) > 0.5 ? darken(color, coefficient) : lighten(color, coefficient);
|
245 | }
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | function fade(color, value) {
|
257 | color = decomposeColor(color);
|
258 | value = clamp(value);
|
259 |
|
260 | if (color.type === 'rgb' || color.type === 'hsl') {
|
261 | color.type += 'a';
|
262 | }
|
263 |
|
264 | color.values[3] = value;
|
265 | return recomposeColor(color);
|
266 | }
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | function darken(color, coefficient) {
|
277 | color = decomposeColor(color);
|
278 | coefficient = clamp(coefficient);
|
279 |
|
280 | if (color.type.indexOf('hsl') !== -1) {
|
281 | color.values[2] *= 1 - coefficient;
|
282 | } else if (color.type.indexOf('rgb') !== -1) {
|
283 | for (var i = 0; i < 3; i += 1) {
|
284 | color.values[i] *= 1 - coefficient;
|
285 | }
|
286 | }
|
287 |
|
288 | return recomposeColor(color);
|
289 | }
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | function lighten(color, coefficient) {
|
300 | color = decomposeColor(color);
|
301 | coefficient = clamp(coefficient);
|
302 |
|
303 | if (color.type.indexOf('hsl') !== -1) {
|
304 | color.values[2] += (100 - color.values[2]) * coefficient;
|
305 | } else if (color.type.indexOf('rgb') !== -1) {
|
306 | for (var i = 0; i < 3; i += 1) {
|
307 | color.values[i] += (255 - color.values[i]) * coefficient;
|
308 | }
|
309 | }
|
310 |
|
311 | return recomposeColor(color);
|
312 | } |
\ | No newline at end of file |