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