UNPKG

2.21 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.getRGBA = exports.colorIsDark = exports.normalizeColor = void 0;
5
6var normalizeColor = function normalizeColor(color, theme, required) {
7 var colorSpec = theme.global.colors[color] || color; // If the color has a light or dark object, use that
8
9 var result = colorSpec;
10
11 if (colorSpec) {
12 if (theme.dark && colorSpec.dark) {
13 result = colorSpec.dark;
14 } else if (!theme.dark && colorSpec.light) {
15 result = colorSpec.light;
16 }
17 } // allow one level of indirection in color names
18
19
20 if (result && theme.global.colors[result]) {
21 result = normalizeColor(result, theme);
22 }
23
24 return required && result === color ? 'inherit' : result;
25};
26
27exports.normalizeColor = normalizeColor;
28
29var parseHexToRGB = function parseHexToRGB(color) {
30 return color.length === 4 ? color.match(/[A-Za-z0-9]{1}/g).map(function (v) {
31 return parseInt(v, 16);
32 }) : // https://stackoverflow.com/a/42429333
33 color.match(/[A-Za-z0-9]{2}/g).map(function (v) {
34 return parseInt(v, 16);
35 });
36};
37
38var canExtractRGBArray = function canExtractRGBArray(color) {
39 return /^#/.test(color) || /^rgb/.test(color);
40};
41
42var getRGBArray = function getRGBArray(color) {
43 if (/^#/.test(color)) {
44 return parseHexToRGB(color);
45 }
46
47 if (/^rgb/.test(color)) {
48 return color.match(/rgba?\((\s?[0-9]*\s?),(\s?[0-9]*\s?),(\s?[0-9]*\s?).*?\)/).splice(1);
49 }
50
51 return color;
52};
53
54var colorIsDark = function colorIsDark(color) {
55 var _getRGBArray = getRGBArray(color),
56 red = _getRGBArray[0],
57 green = _getRGBArray[1],
58 blue = _getRGBArray[2]; // http://www.had2know.com/technology/
59 // color-contrast-calculator-web-design.html
60
61
62 var brightness = (299 * red + 587 * green + 114 * blue) / 1000;
63 return brightness < 125;
64};
65
66exports.colorIsDark = colorIsDark;
67
68var getRGBA = function getRGBA(color, opacity) {
69 if (color && canExtractRGBArray(color)) {
70 var _getRGBArray2 = getRGBArray(color),
71 red = _getRGBArray2[0],
72 green = _getRGBArray2[1],
73 blue = _getRGBArray2[2];
74
75 return "rgba(" + red + ", " + green + ", " + blue + ", " + (opacity || 1) + ")";
76 }
77
78 return undefined;
79};
80
81exports.getRGBA = getRGBA;
\No newline at end of file