grommet
Version:
focus on the essential experience
125 lines (99 loc) • 3.53 kB
JavaScript
export var normalizeColor = function normalizeColor(color, theme, required) {
var colorSpec = theme.global.colors[color] || color; // If the color has a light or dark object, use that
var result = colorSpec;
if (colorSpec) {
if (theme.dark && colorSpec.dark) {
result = colorSpec.dark;
} else if (!theme.dark && colorSpec.light) {
result = colorSpec.light;
}
} // allow one level of indirection in color names
if (result && theme.global.colors[result]) {
result = normalizeColor(result, theme);
}
return required && result === color ? 'inherit' : result;
};
var parseHexToRGB = function parseHexToRGB(color) {
return color.length === 4 ? color.match(/[A-Za-z0-9]{1}/g).map(function (v) {
return parseInt("" + v + v, 16);
}) : // https://stackoverflow.com/a/42429333
color.match(/[A-Za-z0-9]{2}/g).map(function (v) {
return parseInt(v, 16);
});
}; // From: https://stackoverflow.com/a/9493060/8513067
// Converts an HSL color value to RGB. Conversion formula
// adapted from http://en.wikipedia.org/wiki/HSL_color_space.
// Assumes h, s, and l are contained in the set [0, 1] and
// returns r, g, and b in the set [0, 255].
var hslToRGB = function hslToRGB(h, s, l) {
var r;
var g;
var b;
if (s === 0 || s === '0') {
// achromatic
r = l;
g = l;
b = l;
} else {
var hue2rgb = function hue2rgb(p, q, inT) {
var t = inT;
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 0.16666667) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 0.66666667) return p + (q - p) * (0.66666667 - t) * 6;
return p;
};
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 0.33333333);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 0.33333333);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
};
var hexExp = /^#[A-Za-z0-9]{3}$|^#[A-Za-z0-9]{6}$/;
var rgbExp = /rgba?\(\s?([0-9]*)\s?,\s?([0-9]*)\s?,\s?([0-9]*)\s?.*?\)/; // e.g. hsl(240, 60%, 50%)
var hslExp = /hsla?\(\s?([0-9]*)\s?,\s?([0-9]*)%?\s?,\s?([0-9]*)%?\s?.*?\)/;
var canExtractRGBArray = function canExtractRGBArray(color) {
return hexExp.test(color) || rgbExp.test(color) || hslExp.test(color);
};
var getRGBArray = function getRGBArray(color) {
if (hexExp.test(color)) {
return parseHexToRGB(color);
}
var match = color.match(rgbExp);
if (match) {
return match.splice(1);
}
match = color.match(hslExp);
if (match) {
var _match$splice$map = match.splice(1).map(function (v) {
return parseInt(v, 10);
}),
h = _match$splice$map[0],
s = _match$splice$map[1],
l = _match$splice$map[2];
return hslToRGB(h / 360.0, s / 100.0, l / 100.0);
}
return color;
};
export var colorIsDark = function colorIsDark(color) {
var _getRGBArray = getRGBArray(color),
red = _getRGBArray[0],
green = _getRGBArray[1],
blue = _getRGBArray[2]; // http://www.had2know.com/technology/
// color-contrast-calculator-web-design.html
var brightness = (299 * red + 587 * green + 114 * blue) / 1000;
return brightness < 125;
};
export var getRGBA = function getRGBA(color, opacity) {
if (color && canExtractRGBArray(color)) {
var _getRGBArray2 = getRGBArray(color),
red = _getRGBArray2[0],
green = _getRGBArray2[1],
blue = _getRGBArray2[2];
return "rgba(" + red + ", " + green + ", " + blue + ", " + (opacity || 1) + ")";
}
return undefined;
};