UNPKG

8.85 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.darken = darken;
16exports.lighten = lighten;
17
18/* eslint-disable no-use-before-define */
19
20/**
21 * Returns a number whose value is limited to the given range.
22 *
23 * @param {number} value The value to be clamped
24 * @param {number} min The lower boundary of the output range
25 * @param {number} max The upper boundary of the output range
26 * @returns {number} A number in the range [min, max]
27 */
28function 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 * Converts a color from CSS hex format to CSS rgb format.
50 *
51 * @param {string} color - Hex color, i.e. #nnn or #nnnnnn
52 * @returns {string} A CSS rgb color string
53 */
54
55
56function 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
72function intToHex(int) {
73 var hex = int.toString(16);
74 return hex.length === 1 ? "0".concat(hex) : hex;
75}
76/**
77 * Converts a color from CSS rgb format to CSS hex format.
78 *
79 * @param {string} color - RGB color, i.e. rgb(n, n, n)
80 * @returns {string} A CSS rgb color string, i.e. #nnnnnn
81 */
82
83
84function rgbToHex(color) {
85 // Idempotent
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 * Converts a color from hsl format to rgb format.
99 *
100 * @param {string} color - HSL color values
101 * @returns {string} rgb color values
102 */
103
104
105function 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 * Returns an object with the type and values of a color.
134 *
135 * Note: Does not support rgb % values.
136 *
137 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
138 * @returns {object} - A MUI color object: {type: string, values: number[]}
139 */
140
141
142function decomposeColor(color) {
143 // Idempotent
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 * Converts a color object with type and values to a string.
170 *
171 * @param {object} color - Decomposed color
172 * @param {string} color.type - One of: 'rgb', 'rgba', 'hsl', 'hsla'
173 * @param {array} color.values - [n,n,n] or [n,n,n,n]
174 * @returns {string} A CSS color string
175 */
176
177
178function recomposeColor(color) {
179 var type = color.type;
180 var values = color.values;
181
182 if (type.indexOf('rgb') !== -1) {
183 // Only convert the first 3 values to int (i.e. not alpha)
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 * Calculates the contrast ratio between two colors.
196 *
197 * Formula: https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
198 *
199 * @param {string} foreground - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
200 * @param {string} background - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
201 * @returns {number} A contrast ratio value in the range 0 - 21.
202 */
203
204
205function 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 * The relative brightness of any point in a color space,
212 * normalized to 0 for darkest black and 1 for lightest white.
213 *
214 * Formula: https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
215 *
216 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
217 * @returns {number} The relative brightness of the color in the range 0 - 1
218 */
219
220
221function 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; // normalized
226
227 return val <= 0.03928 ? val / 12.92 : Math.pow((val + 0.055) / 1.055, 2.4);
228 }); // Truncate at 3 digits
229
230 return Number((0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]).toFixed(3));
231}
232/**
233 * Darken or lighten a color, depending on its luminance.
234 * Light colors are darkened, dark colors are lightened.
235 *
236 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
237 * @param {number} coefficient=0.15 - multiplier in the range 0 - 1
238 * @returns {string} A CSS color string. Hex input values are returned as rgb
239 */
240
241
242function 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 * Set the absolute transparency of a color.
248 * Any existing alpha values are overwritten.
249 *
250 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
251 * @param {number} value - value to set the alpha channel to in the range 0 -1
252 * @returns {string} A CSS color string. Hex input values are returned as rgb
253 */
254
255
256function 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 * Darkens a color.
269 *
270 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
271 * @param {number} coefficient - multiplier in the range 0 - 1
272 * @returns {string} A CSS color string. Hex input values are returned as rgb
273 */
274
275
276function 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 * Lightens a color.
292 *
293 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
294 * @param {number} coefficient - multiplier in the range 0 - 1
295 * @returns {string} A CSS color string. Hex input values are returned as rgb
296 */
297
298
299function 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