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