@tiger-ui/color-palette-generator
Version:
Install the package in your project directory with: ### npm: ``` npm install @tiger-ui/color-palette-generator ``` ### yarn: ``` yarn add @tiger-ui/color-palette-generator ```
146 lines • 5.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.color = void 0;
var tslib_1 = require("tslib");
var color_converter_1 = tslib_1.__importDefault(require("@tiger-ui/color-converter"));
var contrast_color_1 = require("@tiger-ui/contrast-color");
/* *************** COLOR *************** */
var rgbaToHslaCodes = color_converter_1.default.rgbaToHslaCodes, rgbaToRgbaCodes = color_converter_1.default.rgbaToRgbaCodes, rgbToHslCodes = color_converter_1.default.rgbToHslCodes, rgbToRgbCodes = color_converter_1.default.rgbToRgbCodes, hslaToRgbaCodes = color_converter_1.default.hslaToRgbaCodes, hexToHslCodes = color_converter_1.default.hexToHslCodes, rgbToHex = color_converter_1.default.rgbToHex;
function getColorType(color) {
var type = undefined;
if (color.startsWith('#') && color.length <= 7 && color.length >= 4) {
type = 'hex';
}
if (color.startsWith('rgb')) {
type = 'rgb';
}
if (color.startsWith('rgba')) {
type = 'rgba';
}
return type;
}
/**
*
* @param lightness (in percent) - example: 5 (5%)
* @param operator - 'add' | 'subtract' | 'set'
*
* OPERATORS:
* Sets what action the lightness value will be subjected to.
* - set: sets lightness to typed percentage.
*
* - add: adds the lightness value of the color.
* Example:
* color: hsl(0, 100%, 50%)
* color after added lightness: hsl(0, 100%, 55%) (5% applied)
*
* - subtract: reduces the lightness value of the color
* Example:
* color: hsl(0, 100%, 50%)
* color after lightness reduction: hsl(0, 100%, 45%) (5% applied)
*/
function lightnessSelective(lightness, operator, color) {
// color values
var colorType = getColorType(color);
// value to return
var finalColor = '';
var hsl = {
h: 0,
s: 0,
l: 0,
a: 1,
};
// check color type
if (colorType === 'hex') {
hsl = hexToHslCodes(color);
}
if (colorType === 'rgb') {
var rgb = rgbToRgbCodes(color);
hsl = rgbToHslCodes(rgb.r, rgb.g, rgb.b);
}
;
if (colorType === 'rgba') {
var rgba = rgbaToRgbaCodes(color);
hsl = rgbaToHslaCodes(rgba.r, rgba.g, rgba.b, rgba.a);
}
// lightness values
// The lightness range is between 100 and 0. The maximum value should be 100.
var lightnessMax = 100;
// The lightness range is between 100 and 0. The minimum value should be 0.
var lightnessMin = 0;
// get hsl(a) values
var h = hsl.h, s = hsl.s, l = hsl.l, a = hsl.a;
var lValue = (function () {
var result = 0;
if (operator === 'add') {
var value = l + lightness; /* lightness + %x */
result = value < lightnessMax ? value : lightnessMax;
}
if (operator === 'subtract') {
var value = l - lightness /* lightness - %x */;
result = value > lightnessMin ? value : lightnessMin;
}
if (operator === 'set') {
if (lightness > lightnessMax)
result = lightnessMax;
else if (lightness < lightnessMin)
result = lightnessMin;
else
result = lightness;
}
return result;
})();
var colorRgba = hslaToRgbaCodes(h, s, lValue, a !== null && a !== void 0 ? a : 1);
if (colorType === 'hex') {
finalColor = rgbToHex(colorRgba.r, colorRgba.g, colorRgba.b);
}
if (colorType === 'rgb') {
finalColor = 'rgb(' + colorRgba.r + ',' + colorRgba.g + ',' + colorRgba.b + ')';
}
if (colorType === 'rgba') {
finalColor = 'rgba(' + colorRgba.r + ', ' + colorRgba.g + ', ' + colorRgba.b + ', ' + colorRgba.a + ')';
}
return finalColor;
}
function getDarkColor(mainColor, options) {
return lightnessSelective(options.darkColorLightness /* x% */, 'subtract', mainColor);
}
function getLightColor(color, options) {
if (color)
return color;
return lightnessSelective(options.lightColorLightness /* x% */, 'add', color);
}
function getContrastColor(mainColor, options) {
var contrastDarkValue = options.contrastDarkValue, contrastLightValue = options.contrastLightValue, contrastThresholdValue = options.contrastThresholdValue;
return (0, contrast_color_1.contrastColor)(mainColor, {
darkColor: contrastDarkValue,
lightColor: contrastLightValue,
threshold: contrastThresholdValue,
});
}
/**
* @param colorPalette - 'hex' | 'rgb' | 'rgba' | { main: string; light?: string; dark?: string; contrast?: string }
*/
function color(colorPalette, colorOptions) {
var mainColor = '';
var lightColor;
var darkColor;
var contrastColor;
var options = tslib_1.__assign({ lightColorLightness: 3, darkColorLightness: 3, contrastDarkValue: '#000000', contrastLightValue: '#ffffff', contrastThresholdValue: 128 }, colorOptions);
if (typeof colorPalette === 'string') {
mainColor = colorPalette;
}
if (typeof colorPalette === 'object') {
mainColor = colorPalette.main;
lightColor = colorPalette.light;
darkColor = colorPalette.dark;
contrastColor = colorPalette.contrast;
}
return {
main: mainColor,
dark: darkColor || getDarkColor(mainColor, options),
light: lightColor || getLightColor(mainColor, options),
contrast: contrastColor || getContrastColor(mainColor, options),
};
}
exports.color = color;
//# sourceMappingURL=staticFunction.js.map