wix-style-react
Version:
50 lines (44 loc) • 1.16 kB
JavaScript
import gradient from 'gradient-parser';
import resolveColor from 'color';
import { COLOR_ICON_LIGHT, COLOR_ICON_DARK } from './constants';
export var parseColor = function parseColor(fill) {
if (typeof fill !== 'string') {
return;
}
try {
return resolveColor(fill);
} catch (_unused) {
return;
}
};
export var parseGradient = function parseGradient(fill) {
if (typeof fill !== 'string') {
return;
}
try {
return gradient.parse(fill);
} catch (_unused2) {
return;
}
};
export var parseContrastColor = function parseContrastColor(fill) {
var color = parseColor(fill);
if (color) {
return color.luminosity() > 0.5 ? COLOR_ICON_DARK : COLOR_ICON_LIGHT;
}
var gradients = parseGradient(fill);
if (gradients) {
var gradientList = gradients[0].colorStops.map(function (hex) {
return resolveColor("#".concat(hex.value));
});
var totalLights = gradientList.reduce(function (acc, curr) {
if (curr.luminosity() > 0.5) {
return acc + 1;
} else {
return acc - 1;
}
}, 0);
return totalLights >= 1 ? COLOR_ICON_DARK : COLOR_ICON_LIGHT;
}
return;
};