UNPKG

9.89 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.hexToRgb = hexToRgb;
7exports.rgbToHex = rgbToHex;
8exports.hslToRgb = hslToRgb;
9exports.decomposeColor = decomposeColor;
10exports.recomposeColor = recomposeColor;
11exports.getContrastRatio = getContrastRatio;
12exports.getLuminance = getLuminance;
13exports.emphasize = emphasize;
14exports.fade = fade;
15exports.alpha = alpha;
16exports.darken = darken;
17exports.lighten = lighten;
18
19var _utils = require("@material-ui/utils");
20
21/* eslint-disable no-use-before-define */
22
23/**
24 * Returns a number whose value is limited to the given range.
25 *
26 * @param {number} value The value to be clamped
27 * @param {number} min The lower boundary of the output range
28 * @param {number} max The upper boundary of the output range
29 * @returns {number} A number in the range [min, max]
30 */
31function 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 * Converts a color from CSS hex format to CSS rgb format.
45 *
46 * @param {string} color - Hex color, i.e. #nnn or #nnnnnn
47 * @returns {string} A CSS rgb color string
48 */
49
50
51function 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
67function intToHex(int) {
68 var hex = int.toString(16);
69 return hex.length === 1 ? "0".concat(hex) : hex;
70}
71/**
72 * Converts a color from CSS rgb format to CSS hex format.
73 *
74 * @param {string} color - RGB color, i.e. rgb(n, n, n)
75 * @returns {string} A CSS rgb color string, i.e. #nnnnnn
76 */
77
78
79function rgbToHex(color) {
80 // Idempotent
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 * Converts a color from hsl format to rgb format.
94 *
95 * @param {string} color - HSL color values
96 * @returns {string} rgb color values
97 */
98
99
100function 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 * Returns an object with the type and values of a color.
129 *
130 * Note: Does not support rgb % values.
131 *
132 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
133 * @returns {object} - A MUI color object: {type: string, values: number[]}
134 */
135
136
137function decomposeColor(color) {
138 // Idempotent
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 * Converts a color object with type and values to a string.
165 *
166 * @param {object} color - Decomposed color
167 * @param {string} color.type - One of: 'rgb', 'rgba', 'hsl', 'hsla'
168 * @param {array} color.values - [n,n,n] or [n,n,n,n]
169 * @returns {string} A CSS color string
170 */
171
172
173function recomposeColor(color) {
174 var type = color.type;
175 var values = color.values;
176
177 if (type.indexOf('rgb') !== -1) {
178 // Only convert the first 3 values to int (i.e. not alpha)
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 * Calculates the contrast ratio between two colors.
191 *
192 * Formula: https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
193 *
194 * @param {string} foreground - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
195 * @param {string} background - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
196 * @returns {number} A contrast ratio value in the range 0 - 21.
197 */
198
199
200function 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 * The relative brightness of any point in a color space,
207 * normalized to 0 for darkest black and 1 for lightest white.
208 *
209 * Formula: https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
210 *
211 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
212 * @returns {number} The relative brightness of the color in the range 0 - 1
213 */
214
215
216function 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; // normalized
221
222 return val <= 0.03928 ? val / 12.92 : Math.pow((val + 0.055) / 1.055, 2.4);
223 }); // Truncate at 3 digits
224
225 return Number((0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]).toFixed(3));
226}
227/**
228 * Darken or lighten a color, depending on its luminance.
229 * Light colors are darkened, dark colors are lightened.
230 *
231 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
232 * @param {number} coefficient=0.15 - multiplier in the range 0 - 1
233 * @returns {string} A CSS color string. Hex input values are returned as rgb
234 */
235
236
237function 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
242var warnedOnce = false;
243/**
244 * Set the absolute transparency of a color.
245 * Any existing alpha values are overwritten.
246 *
247 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
248 * @param {number} value - value to set the alpha channel to in the range 0 -1
249 * @returns {string} A CSS color string. Hex input values are returned as rgb
250 *
251 * @deprecated
252 * Use `import { alpha } from '@material-ui/core/styles'` instead.
253 */
254
255function 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 * Set the absolute transparency of a color.
267 * Any existing alpha value is overwritten.
268 *
269 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
270 * @param {number} value - value to set the alpha channel to in the range 0-1
271 * @returns {string} A CSS color string. Hex input values are returned as rgb
272 */
273
274
275function 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 * Darkens a color.
288 *
289 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
290 * @param {number} coefficient - multiplier in the range 0 - 1
291 * @returns {string} A CSS color string. Hex input values are returned as rgb
292 */
293
294
295function 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 * Lightens a color.
311 *
312 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
313 * @param {number} coefficient - multiplier in the range 0 - 1
314 * @returns {string} A CSS color string. Hex input values are returned as rgb
315 */
316
317
318function 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