ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
153 lines (152 loc) • 3.36 kB
JavaScript
import { inputToRGB, rgbToHex, rgbToHsv } from "@ctrl/tinycolor";
const statusColors = [
"success",
"processing",
"error",
"default",
"warning"
];
const presetColors = [
"pink",
"red",
"yellow",
"orange",
"cyan",
"green",
"blue",
"purple",
"geekblue",
"magenta",
"volcano",
"gold",
"lime"
];
function isPresetColor(color) {
return presetColors.indexOf(color) !== -1;
}
function isStatusColor(color) {
return statusColors.indexOf(color) !== -1;
}
const hueStep = 2;
const saturationStep = .16;
const saturationStep2 = .05;
const brightnessStep1 = .05;
const brightnessStep2 = .15;
const lightColorCount = 5;
const darkColorCount = 4;
const darkColorMap = [
{
index: 7,
opacity: .15
},
{
index: 6,
opacity: .25
},
{
index: 5,
opacity: .3
},
{
index: 5,
opacity: .45
},
{
index: 5,
opacity: .65
},
{
index: 5,
opacity: .85
},
{
index: 4,
opacity: .9
},
{
index: 3,
opacity: .95
},
{
index: 2,
opacity: .97
},
{
index: 1,
opacity: .98
}
];
function toHsv({ r, g, b }) {
const hsv = rgbToHsv(r, g, b);
return {
h: hsv.h * 360,
s: hsv.s,
v: hsv.v
};
}
function toHex({ r, g, b }) {
return `#${rgbToHex(r, g, b, false)}`;
}
function mix(rgb1, rgb2, amount) {
const p = amount / 100;
return {
r: (rgb2.r - rgb1.r) * p + rgb1.r,
g: (rgb2.g - rgb1.g) * p + rgb1.g,
b: (rgb2.b - rgb1.b) * p + rgb1.b
};
}
function getHue(hsv, i, light) {
let hue;
if (Math.round(hsv.h) >= 60 && Math.round(hsv.h) <= 240) hue = light ? Math.round(hsv.h) - hueStep * i : Math.round(hsv.h) + hueStep * i;
else hue = light ? Math.round(hsv.h) + hueStep * i : Math.round(hsv.h) - hueStep * i;
if (hue < 0) hue += 360;
else if (hue >= 360) hue -= 360;
return hue;
}
function getSaturation(hsv, i, light) {
if (hsv.h === 0 && hsv.s === 0) return hsv.s;
let saturation;
if (light) saturation = hsv.s - saturationStep * i;
else if (i === darkColorCount) saturation = hsv.s + saturationStep;
else saturation = hsv.s + saturationStep2 * i;
if (saturation > 1) saturation = 1;
if (light && i === lightColorCount && saturation > .1) saturation = .1;
if (saturation < .06) saturation = .06;
return Number(saturation.toFixed(2));
}
function getValue(hsv, i, light) {
let value;
if (light) value = hsv.v + brightnessStep1 * i;
else value = hsv.v - brightnessStep2 * i;
if (value > 1) value = 1;
return Number(value.toFixed(2));
}
function generate(color, opts = {}) {
const patterns = [];
const pColor = inputToRGB(color);
for (let i = lightColorCount; i > 0; i -= 1) {
const hsv = toHsv(pColor);
const colorString = toHex(inputToRGB({
h: getHue(hsv, i, true),
s: getSaturation(hsv, i, true),
v: getValue(hsv, i, true)
}));
patterns.push(colorString);
}
patterns.push(toHex(pColor));
for (let i = 1; i <= darkColorCount; i += 1) {
const hsv = toHsv(pColor);
const colorString = toHex(inputToRGB({
h: getHue(hsv, i),
s: getSaturation(hsv, i),
v: getValue(hsv, i)
}));
patterns.push(colorString);
}
if (opts.theme === "dark") return darkColorMap.map(({ index, opacity }) => {
return toHex(mix(inputToRGB(opts.backgroundColor || "#141414"), inputToRGB(patterns[index]), opacity * 100));
});
return patterns;
}
export { generate, isPresetColor, isStatusColor, presetColors, statusColors };
//# sourceMappingURL=ng-zorro-antd-core-color.mjs.map