toolset.io
Version:
471 lines (470 loc) • 14.8 kB
TypeScript
interface RGB extends Record<any, any> {
r: number;
g: number;
b: number;
a?: number;
}
export interface RGBColor extends RGB {
a?: 1;
format?: 'name' | 'rgb';
}
export interface RGBAColor extends RGB {
a: number;
format?: 'name' | 'rgba';
}
export interface HEX3Color extends RGB {
a?: 1;
format?: 'name' | 'hex3';
}
export interface HEX4Color extends RGB {
a: number;
format?: 'name' | 'hex4';
}
export interface HEX6Color extends RGB {
a?: 1;
format?: 'name' | 'hex6';
}
export interface HEX8Color extends RGB {
a: number;
format?: 'name' | 'hex8';
}
interface HSL extends Record<any, any> {
h: number;
s: number;
l: number;
a?: number;
}
export interface HSLColor extends HSL {
a?: 1;
format?: 'name' | 'hsl';
}
export interface HSLAColor extends HSL {
a: number;
format?: 'name' | 'hsla';
}
interface HSV extends Record<any, any> {
h: number;
s: number;
v: number;
a?: number;
}
export interface HSVColor extends HSV {
a?: 1;
format?: 'name' | 'hsv';
}
export interface HSVAColor extends HSV {
a: number;
format?: 'name' | 'hsva';
}
export type Color = string | RGBColor | RGBAColor | HSLColor | HSLAColor | HSVColor | HSVAColor | HEX3Color | HEX4Color | HEX6Color | HEX8Color;
export type ObjectColor = Exclude<Color, string>;
export interface ColorMeta {
rgb: RGBColor;
hsl: HSLColor;
hsv: HSVColor;
hex: string;
alpha: number;
rgba: RGBAColor;
hsla: HSLAColor;
hsva: HSVAColor;
hex8: string;
gray: number;
origin: Color;
}
export type ColorType = 'hex' | 'rgb' | 'hsv' | 'hsl';
export declare const RGB_REG: RegExp;
export declare const RGBA_REG: RegExp;
export declare const HSL_REG: RegExp;
export declare const HSLA_REG: RegExp;
export declare const HSV_REG: RegExp;
export declare const HSVA_REG: RegExp;
export declare const HEX_REG_3: RegExp;
export declare const HEX_REG_4: RegExp;
export declare const HEX_REG_6: RegExp;
export declare const HEX_REG_8: RegExp;
export declare const NAMED_COLORS: Readonly<{
aliceblue: "f0f8ff";
antiquewhite: "faebd7";
aqua: "0ff";
aquamarine: "7fffd4";
azure: "f0ffff";
beige: "f5f5dc";
bisque: "ffe4c4";
black: "000";
blanchedalmond: "ffebcd";
blue: "00f";
blueviolet: "8a2be2";
brown: "a52a2a";
burlywood: "deb887";
burntsienna: "ea7e5d";
cadetblue: "5f9ea0";
chartreuse: "7fff00";
chocolate: "d2691e";
coral: "ff7f50";
cornflowerblue: "6495ed";
cornsilk: "fff8dc";
crimson: "dc143c";
cyan: "0ff";
darkblue: "00008b";
darkcyan: "008b8b";
darkgoldenrod: "b8860b";
darkgray: "a9a9a9";
darkgreen: "006400";
darkgrey: "a9a9a9";
darkkhaki: "bdb76b";
darkmagenta: "8b008b";
darkolivegreen: "556b2f";
darkorange: "ff8c00";
darkorchid: "9932cc";
darkred: "8b0000";
darksalmon: "e9967a";
darkseagreen: "8fbc8f";
darkslateblue: "483d8b";
darkslategray: "2f4f4f";
darkslategrey: "2f4f4f";
darkturquoise: "00ced1";
darkviolet: "9400d3";
deeppink: "ff1493";
deepskyblue: "00bfff";
dimgray: "696969";
dimgrey: "696969";
dodgerblue: "1e90ff";
firebrick: "b22222";
floralwhite: "fffaf0";
forestgreen: "228b22";
fuchsia: "f0f";
gainsboro: "dcdcdc";
ghostwhite: "f8f8ff";
gold: "ffd700";
goldenrod: "daa520";
gray: "808080";
green: "008000";
greenyellow: "adff2f";
grey: "808080";
honeydew: "f0fff0";
hotpink: "ff69b4";
indianred: "cd5c5c";
indigo: "4b0082";
ivory: "fffff0";
khaki: "f0e68c";
lavender: "e6e6fa";
lavenderblush: "fff0f5";
lawngreen: "7cfc00";
lemonchiffon: "fffacd";
lightblue: "add8e6";
lightcoral: "f08080";
lightcyan: "e0ffff";
lightgoldenrodyellow: "fafad2";
lightgray: "d3d3d3";
lightgreen: "90ee90";
lightgrey: "d3d3d3";
lightpink: "ffb6c1";
lightsalmon: "ffa07a";
lightseagreen: "20b2aa";
lightskyblue: "87cefa";
lightslategray: "789";
lightslategrey: "789";
lightsteelblue: "b0c4de";
lightyellow: "ffffe0";
lime: "0f0";
limegreen: "32cd32";
linen: "faf0e6";
magenta: "f0f";
maroon: "800000";
mediumaquamarine: "66cdaa";
mediumblue: "0000cd";
mediumorchid: "ba55d3";
mediumpurple: "9370db";
mediumseagreen: "3cb371";
mediumslateblue: "7b68ee";
mediumspringgreen: "00fa9a";
mediumturquoise: "48d1cc";
mediumvioletred: "c71585";
midnightblue: "191970";
mintcream: "f5fffa";
mistyrose: "ffe4e1";
moccasin: "ffe4b5";
navajowhite: "ffdead";
navy: "000080";
oldlace: "fdf5e6";
olive: "808000";
olivedrab: "6b8e23";
orange: "ffa500";
orangered: "ff4500";
orchid: "da70d6";
palegoldenrod: "eee8aa";
palegreen: "98fb98";
paleturquoise: "afeeee";
palevioletred: "db7093";
papayawhip: "ffefd5";
peachpuff: "ffdab9";
peru: "cd853f";
pink: "ffc0cb";
plum: "dda0dd";
powderblue: "b0e0e6";
purple: "800080";
rebeccapurple: "663399";
red: "f00";
rosybrown: "bc8f8f";
royalblue: "4169e1";
saddlebrown: "8b4513";
salmon: "fa8072";
sandybrown: "f4a460";
seagreen: "2e8b57";
seashell: "fff5ee";
sienna: "a0522d";
silver: "c0c0c0";
skyblue: "87ceeb";
slateblue: "6a5acd";
slategray: "708090";
slategrey: "708090";
snow: "fffafa";
springgreen: "00ff7f";
steelblue: "4682b4";
tan: "d2b48c";
teal: "008080";
thistle: "d8bfd8";
tomato: "ff6347";
turquoise: "40e0d0";
violet: "ee82ee";
wheat: "f5deb3";
white: "fff";
whitesmoke: "f5f5f5";
yellow: "ff0";
yellowgreen: "9acd32";
}>;
export type ColorName = keyof typeof NAMED_COLORS;
export declare const COLOR_NAMES: Readonly<Set<"aliceblue" | "antiquewhite" | "aqua" | "aquamarine" | "azure" | "beige" | "bisque" | "black" | "blanchedalmond" | "blue" | "blueviolet" | "brown" | "burlywood" | "burntsienna" | "cadetblue" | "chartreuse" | "chocolate" | "coral" | "cornflowerblue" | "cornsilk" | "crimson" | "cyan" | "darkblue" | "darkcyan" | "darkgoldenrod" | "darkgray" | "darkgreen" | "darkgrey" | "darkkhaki" | "darkmagenta" | "darkolivegreen" | "darkorange" | "darkorchid" | "darkred" | "darksalmon" | "darkseagreen" | "darkslateblue" | "darkslategray" | "darkslategrey" | "darkturquoise" | "darkviolet" | "deeppink" | "deepskyblue" | "dimgray" | "dimgrey" | "dodgerblue" | "firebrick" | "floralwhite" | "forestgreen" | "fuchsia" | "gainsboro" | "ghostwhite" | "gold" | "goldenrod" | "gray" | "green" | "greenyellow" | "grey" | "honeydew" | "hotpink" | "indianred" | "indigo" | "ivory" | "khaki" | "lavender" | "lavenderblush" | "lawngreen" | "lemonchiffon" | "lightblue" | "lightcoral" | "lightcyan" | "lightgoldenrodyellow" | "lightgray" | "lightgreen" | "lightgrey" | "lightpink" | "lightsalmon" | "lightseagreen" | "lightskyblue" | "lightslategray" | "lightslategrey" | "lightsteelblue" | "lightyellow" | "lime" | "limegreen" | "linen" | "magenta" | "maroon" | "mediumaquamarine" | "mediumblue" | "mediumorchid" | "mediumpurple" | "mediumseagreen" | "mediumslateblue" | "mediumspringgreen" | "mediumturquoise" | "mediumvioletred" | "midnightblue" | "mintcream" | "mistyrose" | "moccasin" | "navajowhite" | "navy" | "oldlace" | "olive" | "olivedrab" | "orange" | "orangered" | "orchid" | "palegoldenrod" | "palegreen" | "paleturquoise" | "palevioletred" | "papayawhip" | "peachpuff" | "peru" | "pink" | "plum" | "powderblue" | "purple" | "rebeccapurple" | "red" | "rosybrown" | "royalblue" | "saddlebrown" | "salmon" | "sandybrown" | "seagreen" | "seashell" | "sienna" | "silver" | "skyblue" | "slateblue" | "slategray" | "slategrey" | "snow" | "springgreen" | "steelblue" | "tan" | "teal" | "thistle" | "tomato" | "turquoise" | "violet" | "wheat" | "white" | "whitesmoke" | "yellow" | "yellowgreen">>;
export declare function isHex(color: any): boolean;
export declare function isRgb(color: any): boolean;
export declare function isRgba(color: any): boolean;
/**
* 判断给定的字符串是否为一个合法颜色值
*
* @param value 原始字符串
*
* @returns 是否为合法颜色
*/
export declare function isColor(value: string): boolean;
/**
* 将给定的字符串转化为 {@link ObjectColor},无法转换时返回 null
*
* @param color 原始颜色字符串
*
* @returns 解析后的颜色对象
*/
export declare function parseStringColor(color: string): ObjectColor | null;
/**
* 将给定的 {@link Color} 解析为 {@link ColorMeta}
*
* @param color 原始颜色值
*
* @returns 解析后的颜色元数据
*/
export declare function parseColor(color: Color): ColorMeta;
/**
* 将给定的 {@link Color} 解析为 {@link RGBAColor}
*
* @param originColor 原始颜色值
*
* @returns 解析后的 RGB 颜色对象
*/
export declare function parseColorToRgba(originColor: Color): RGBAColor;
/**
* 将原始的 h、s、l 值标准化为 0 ~ 1 的值
*
* @param h 0 ~ 360
* @param s 0 ~ 1,0% ~ 100%
* @param l 0 ~ 1,0% ~ 100%
*
* @returns 标准化后的 HSL 对象
*/
export declare function normalizeHsl(h: number | string, s: number | string, l: number | string): {
h: number;
s: number;
l: number;
};
/**
* 将原始的 r、g、b 值标准化为 0 ~ 1 的值
*
* @param r 0 ~ 255
* @param g 0 ~ 255
* @param b 0 ~ 255
*
* @returns 标准化后 RGB 对象
*/
export declare function normalizeRgb(r: number | string, g: number | string, b: number | string): {
r: number;
g: number;
b: number;
};
/**
* 将原始的 h、s、v 值标准化为 0 ~ 1 的值
*
* @param h 0 ~ 360
* @param s 0 ~ 1,0% ~ 100%
* @param v 0 ~ 1,0% ~ 100%
*
* @returns 标准化的 HSV 对象
*/
export declare function normalizeHsv(h: number | string, s: number | string, v: number | string): {
h: number;
s: number;
v: number;
};
/**
* 将原始透明度值标准化为 0 ~ 1 的值
*
* @param a 0 ~ 1,0% ~ 100%
*
* @returns 标准化后的透明度
*/
export declare function normalizeAlpha(a: number | string): number;
/**
* 将 HSL 颜色转换为 RGB 颜色
*
* @param h 0 ~ 360
* @param s 0 ~ 1,0% ~ 100%
* @param l 0 ~ 1,0% ~ 100%
*
* @returns 转换后的 RGB 颜色
*/
export declare function hslToRgb(h: number | string, s: number | string, l: number | string): RGBColor;
/**
* 将 RGB 颜色转换为 HSL 颜色
*
* @param r 0 ~ 255
* @param g 0 ~ 255
* @param b 0 ~ 255
*
* @returns 转换后的 HSL 颜色
*/
export declare function rgbToHsl(r: number | string, g: number | string, b: number | string): HSLColor;
/**
* 将 HSL 颜色转换为 HSV 颜色
*
* @param h 0 ~ 360
* @param s 0 ~ 1,0% ~ 100%
* @param l 0 ~ 1,0% ~ 100%
*
* @returns 转换后的 HSV 颜色
*/
export declare function hslToHsv(h: number | string, s: number | string, l: number | string): HSVColor;
/**
* 将 HSV 颜色转换为 HSL 颜色
*
* @param h 0 ~ 360
* @param s 0 ~ 1,0% ~ 100%
* @param v 0 ~ 1,0% ~ 100%
*
* @returns 转换后的 HSL 颜色
*/
export declare function hsvToHsl(h: number | string, s: number | string, v: number | string): HSLColor;
/**
* 将 HSV 颜色转换为 RGB 颜色
*
* @param h 0 ~ 360
* @param s 0 ~ 1,0% ~ 100%
* @param v 0 ~ 1,0% ~ 100%
*
* @returns 转换后的 RGB 颜色
*/
export declare function hsvToRgb(h: number | string, s: number | string, v: number | string): RGBColor;
/**
* 将 RGB 颜色转换为 HSV 颜色
*
* @param r 0 ~ 255
* @param g 0 ~ 255
* @param b 0 ~ 255
*
* @returns 转换后的 HSV 颜色
*/
export declare function rgbToHsv(r: number | string, g: number | string, b: number | string): HSVColor;
/**
* 将 RGB 颜色转换为 HEX 颜色
*
* @param r 0 ~ 255
* @param g 0 ~ 255
* @param b 0 ~ 255
* @param allow3Char 是否允许 3 位的 HEX 值
*
* @returns 转换后的 HEX 颜色
*/
export declare function rgbToHex(r: number | string, g: number | string, b: number | string, allow3Char?: boolean): string;
/**
* 将 RGBA 颜色转换为 HEX 颜色
*
* @param r 0 ~ 255
* @param g 0 ~ 255
* @param b 0 ~ 255
* @param a 0 ~ 1,0% ~ 100%
* @param allow4Char 是否允许 4 位的 HEX 值
*
* @returns 转换后的 HEX 颜色
*/
export declare function rgbaToHex(r: number | string, g: number | string, b: number | string, a: number | string, allow4Char?: boolean): string;
/**
* 将两种颜色按照一定的比例混合
*
* @param color1 第一种颜色
* @param color2 第二种颜色
* @param weight 混合比例 0 ~ 1,越小则第一种颜色越少
*
* @returns 混合后的颜色
*/
export declare function mixColor(color1: Color, color2: Color, weight?: number): RGBAColor;
/**
* 调整给定颜色值的透明度
*
* @param color 需要调整的颜色
* @param alpha 调整后的透明度 0 ~ 1,0% ~ 100%
*
* @returns 调整后的颜色
*/
export declare function adjustAlpha(color: Color, alpha: number | string): RGBAColor;
/**
* 随机生成一个颜色值
*
* @param withAlpha 是否具有透明度
* @param type 颜色的类型
*
* @returns 生成的颜色字面值
*/
export declare function randomColor(withAlpha?: boolean, type?: ColorType): string;
/**
* 随机生成一个特定色调(冷暖色)的颜色值
*
* @param prefer 色调
* @param withAlpha 是否具有透明度
* @param type 颜色的类型
*
* @returns 生成的颜色字面值
*/
export declare function randomPreferColor(prefer: 'hard' | 'soft', withAlpha?: boolean, type?: ColorType): string;
/**
* 随机生成一个冷色调的颜色值
*
* @param withAlpha 是否具有透明度
* @param type 颜色的类型
*
* @returns 生成的颜色字面值
*/
export declare function randomHardColor(withAlpha?: boolean, type?: ColorType): string;
/**
* 随机生成一个暖色调的颜色值
*
* @param withAlpha 是否具有透明度
* @param type 颜色的类型
*
* @returns 生成的颜色字面值
*/
export declare function randomSoftColor(withAlpha?: boolean, type?: ColorType): string;
/**
* 获取给定颜色的灰度
*
* @param color 颜色字面值
*
* @returns 颜色的灰度
*/
export declare function toGrayScale(color: string): number;
export {};