@cloudcome/utils-core 0.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/LICENSE +21 -0
- package/package.json +277 -6
- package/src/array.ts +312 -0
- package/src/async.ts +379 -0
- package/src/base64.ts +20 -0
- package/src/cache.ts +146 -0
- package/src/color/contrast.ts +20 -0
- package/src/color/distance.ts +28 -0
- package/src/color/helpers.ts +23 -0
- package/src/color/hex-hsl.ts +11 -0
- package/src/color/hex-hsv.ts +28 -0
- package/src/color/hex-hwb.ts +31 -0
- package/src/color/hex-rgb.ts +39 -0
- package/src/color/hsl-lighten.ts +15 -0
- package/src/color/hsv-brighten.ts +17 -0
- package/src/color/luminance.ts +17 -0
- package/src/color/mix.ts +26 -0
- package/src/color/rgb-hsl.ts +53 -0
- package/src/color/rgb-hsv.ts +52 -0
- package/src/color/rgb-hwb.ts +56 -0
- package/src/color/rgb-lab.ts +33 -0
- package/src/color/rgb-whiter.ts +22 -0
- package/src/color/rgb-xyz.ts +62 -0
- package/src/color/types.ts +65 -0
- package/src/color/xyz-lab.ts +54 -0
- package/src/color.ts +19 -0
- package/src/crypto/md5.mjs +357 -0
- package/src/crypto/sha1.mjs +300 -0
- package/src/crypto/sha256.mjs +310 -0
- package/src/crypto/sha512.mjs +459 -0
- package/src/crypto.ts +60 -0
- package/src/date/const.ts +6 -0
- package/src/date/core.ts +162 -0
- package/src/date/days.ts +51 -0
- package/src/date/is.ts +186 -0
- package/src/date/relative.ts +92 -0
- package/src/date/start-end.ts +246 -0
- package/src/date/timezone.ts +220 -0
- package/src/date/weeks.ts +100 -0
- package/src/date.ts +8 -0
- package/src/dict.ts +1 -0
- package/src/dts/global.d.ts +27 -0
- package/src/easing.ts +166 -0
- package/src/emitter.ts +117 -0
- package/src/enum.ts +171 -0
- package/src/env.ts +62 -0
- package/src/error.ts +31 -0
- package/src/exception.ts +68 -0
- package/src/fn.ts +197 -0
- package/src/index.ts +1 -0
- package/src/number.ts +236 -0
- package/src/object/each.ts +56 -0
- package/src/object/get-set.ts +273 -0
- package/src/object/is.ts +128 -0
- package/src/object/merge.ts +180 -0
- package/src/object/process.ts +80 -0
- package/src/object.ts +5 -0
- package/src/path.ts +188 -0
- package/src/promise.ts +111 -0
- package/src/qs.ts +119 -0
- package/src/regexp.ts +156 -0
- package/src/string.ts +146 -0
- package/src/time/from.ts +57 -0
- package/src/time/to.ts +106 -0
- package/src/time.ts +2 -0
- package/src/timer.ts +226 -0
- package/src/tree.ts +394 -0
- package/src/type.ts +197 -0
- package/src/types.ts +78 -0
- package/src/unique.ts +77 -0
- package/src/url.ts +93 -0
- package/src/version.ts +71 -0
- package/test/array.test.ts +332 -0
- package/test/async-real.test.ts +39 -0
- package/test/async.test.ts +375 -0
- package/test/base64.test.ts +32 -0
- package/test/cache.test.ts +83 -0
- package/test/color.test.ts +163 -0
- package/test/crypto.test.ts +34 -0
- package/test/date-tz.test.ts +206 -0
- package/test/date.test.ts +353 -0
- package/test/easing.test.ts +33 -0
- package/test/emitter.test.ts +71 -0
- package/test/enum.test.ts +113 -0
- package/test/env.test.ts +69 -0
- package/test/error.test.ts +58 -0
- package/test/exception.test.ts +43 -0
- package/test/fn.test.ts +263 -0
- package/test/helpers.ts +23 -0
- package/test/index.test.ts +6 -0
- package/test/number.test.ts +213 -0
- package/test/object.test.ts +309 -0
- package/test/path.test.ts +156 -0
- package/test/promise.test.ts +199 -0
- package/test/qs.test.ts +79 -0
- package/test/regexp.test.ts +97 -0
- package/test/string.test.ts +150 -0
- package/test/time.test.ts +214 -0
- package/test/timer.test.ts +114 -0
- package/test/tree.test.ts +348 -0
- package/test/type.test.ts +226 -0
- package/test/unique.test.ts +71 -0
- package/test/url.test.ts +136 -0
- package/test/version.test.ts +52 -0
- package/tsconfig.json +31 -0
- package/vite.config.mts +114 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { HEX, RGB } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 将HEX颜色字符串转换为RGB颜色对象
|
|
5
|
+
* @param hex HEX颜色字符串,支持3位或6位格式(例如#f00或#ff0000)
|
|
6
|
+
* @returns {RGB} 对应的RGB颜色对象
|
|
7
|
+
* @throws {SyntaxError} 当颜色格式不符合规范时抛出
|
|
8
|
+
* @example
|
|
9
|
+
* hexToRgb('#f00') // returns {r: 255, g: 0, b: 0}
|
|
10
|
+
*/
|
|
11
|
+
export function hexToRgb(hex: HEX): RGB {
|
|
12
|
+
const reg = hex.length === 4 ? /^#(.)(.)(.)$/ : /^#(.{2})(.{2})(.{2})$/;
|
|
13
|
+
const result = reg.exec(hex);
|
|
14
|
+
|
|
15
|
+
if (!result) throw new SyntaxError(`颜色(${hex})表达式有误`);
|
|
16
|
+
|
|
17
|
+
const [_, r, g, b] = result;
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
r: Number.parseInt(r.padEnd(2, r), 16),
|
|
21
|
+
g: Number.parseInt(g.padEnd(2, g), 16),
|
|
22
|
+
b: Number.parseInt(b.padEnd(2, b), 16),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function to16(n: number) {
|
|
27
|
+
return Math.round(n).toString(16).padStart(2, '0');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 将RGB颜色对象转换为HEX颜色字符串
|
|
32
|
+
* @param rgb RGB颜色对象
|
|
33
|
+
* @returns {HEX} 6位HEX颜色字符串(带#前缀)
|
|
34
|
+
* @example
|
|
35
|
+
* rgbToHex({r: 255, g: 0, b: 0}) // returns '#ff0000'
|
|
36
|
+
*/
|
|
37
|
+
export function rgbToHex(rgb: RGB): HEX {
|
|
38
|
+
return `#${to16(rgb.r)}${to16(rgb.g)}${to16(rgb.b)}`;
|
|
39
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { HSL } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 通过HSL颜色空间调整颜色亮度
|
|
5
|
+
* @param hsl 原始 HSL 颜色
|
|
6
|
+
* @param value 亮度调整系数(0-1之间,0.1表示增加10%亮度)
|
|
7
|
+
* @returns {HSL} 调整后的 HSL 颜色
|
|
8
|
+
* @example
|
|
9
|
+
* hslLighten({h: 300, s: 33, l: 44}, 0.2) // 返回亮度增加20%后的颜色
|
|
10
|
+
*/
|
|
11
|
+
export function hslLighten(hsl: HSL, value: number): HSL {
|
|
12
|
+
const hslFinal = { ...hsl };
|
|
13
|
+
hslFinal.l = hslFinal.l * (1 + value);
|
|
14
|
+
return hslFinal;
|
|
15
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { HSV } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 通过HSV颜色空间调整颜色明度
|
|
5
|
+
* @param hsv 原始HSV颜色对象
|
|
6
|
+
* @param value 明度调整系数(-1到1之间):
|
|
7
|
+
* - 正值增加明度(如0.2表示+20%)
|
|
8
|
+
* - 负值降低明度(如-0.1表示-10%)
|
|
9
|
+
* @returns {HSV} 调整后的HSV颜色对象(v值范围0-100%)
|
|
10
|
+
* @example
|
|
11
|
+
* hsvBrighten({h: 0, s: 100, v: 50}, 0.3) // 返回{h: 0, s: 100, v: 65}
|
|
12
|
+
*/
|
|
13
|
+
export function hsvBrighten(hsv: HSV, value: number): HSV {
|
|
14
|
+
const hsvFinal = { ...hsv };
|
|
15
|
+
hsvFinal.v = hsvFinal.v * (1 + value);
|
|
16
|
+
return hsvFinal;
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RGB } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 计算RGB颜色的相对亮度(符合WCAG 2.1标准)
|
|
5
|
+
* @param rgb RGB颜色对象(分量范围0-255)
|
|
6
|
+
* @returns {number} 相对亮度值(0-1之间)
|
|
7
|
+
* @see https://www.w3.org/TR/WCAG21/#dfn-relative-luminance
|
|
8
|
+
* @example
|
|
9
|
+
* luminance({r: 255, g: 255, b: 255}) // 返回1
|
|
10
|
+
*/
|
|
11
|
+
export function luminance({ r, g, b }: RGB) {
|
|
12
|
+
const a = [r, g, b].map((v) => {
|
|
13
|
+
const vFinal = v / 255;
|
|
14
|
+
return vFinal <= 0.03928 ? vFinal / 12.92 : ((vFinal + 0.055) / 1.055) ** 2.4;
|
|
15
|
+
});
|
|
16
|
+
return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
|
|
17
|
+
}
|
package/src/color/mix.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { HSL, HSV, RGB } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 颜色混合函数(支持RGB/HSL/HSV色彩模型)
|
|
5
|
+
* @template T 颜色类型,支持RGB、HSL或HSV对象
|
|
6
|
+
* @param {T} a 第一个颜色对象
|
|
7
|
+
* @param {T} b 第二个颜色对象
|
|
8
|
+
* @param {number} [weight=0.5] 混合权重(0-1之间):
|
|
9
|
+
* - 0 表示完全使用第一个颜色
|
|
10
|
+
* - 1 表示完全使用第二个颜色
|
|
11
|
+
* @returns {T} 线性混合后的新颜色对象
|
|
12
|
+
* @example
|
|
13
|
+
* // RGB混合示例
|
|
14
|
+
* mix({r: 255, g: 0, b: 0}, {r: 0, g: 0, b: 255}, 0.5) // 返回紫色
|
|
15
|
+
*
|
|
16
|
+
* // HSL混合示例
|
|
17
|
+
* mix({h: 0, s: 100, l: 50}, {h: 120, s: 100, l: 50}, 0.3)
|
|
18
|
+
*/
|
|
19
|
+
export function mix<T extends RGB | HSV | HSL>(a: T, b: T, weight = 0.5): T {
|
|
20
|
+
return Object.keys(a).reduce((acc, key) => {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
acc[key] = (b[key] - a[key]) * weight + a[key];
|
|
24
|
+
return acc;
|
|
25
|
+
}, {} as T);
|
|
26
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// @ref https://www.w3schools.com/lib/w3color.js
|
|
2
|
+
import { rgbToHue } from './helpers';
|
|
3
|
+
import type { HSL, RGB } from './types';
|
|
4
|
+
|
|
5
|
+
const { abs, min, max, round } = Math;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 将RGB颜色转换为HSL颜色空间
|
|
9
|
+
* @param rgb RGB颜色对象(分量范围0-255)
|
|
10
|
+
* @returns {HSL} HSL颜色对象,其中:
|
|
11
|
+
* h: 色相(0-360度)
|
|
12
|
+
* s: 饱和度(0-100%)
|
|
13
|
+
* l: 亮度(0-100%)
|
|
14
|
+
* @example
|
|
15
|
+
* rgbToHsl({r: 255, g: 0, b: 0}) // returns {h: 0, s: 100, l: 50}
|
|
16
|
+
*/
|
|
17
|
+
export function rgbToHsl(rgb: RGB): HSL {
|
|
18
|
+
const [hue, max, min, diff] = rgbToHue(rgb);
|
|
19
|
+
const l = (2 * max - diff) / 2;
|
|
20
|
+
const s = min === max ? 0 : l < 0.5 ? (max - min) / (max + min) : (max - min) / (2 - max - min);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
h: hue,
|
|
24
|
+
s: s * 100,
|
|
25
|
+
l: l * 100,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// @ref https://www.30secondsofcode.org/js/s/hsl-to-rgb/
|
|
30
|
+
/**
|
|
31
|
+
* 将HSL颜色转换回RGB颜色空间
|
|
32
|
+
* @param hsl HSL颜色对象
|
|
33
|
+
* @param hsl.h 色相(0-360度)
|
|
34
|
+
* @param hsl.s 饱和度(0-100%)
|
|
35
|
+
* @param hsl.l 亮度(0-100%)
|
|
36
|
+
* @returns {RGB} RGB颜色对象(分量范围0-255)
|
|
37
|
+
* @example
|
|
38
|
+
* hslToRgb({h: 0, s: 100, l: 50}) // returns {r: 255, g: 0, b: 0}
|
|
39
|
+
*/
|
|
40
|
+
export function hslToRgb({ h, s, l }: HSL): RGB {
|
|
41
|
+
s /= 100;
|
|
42
|
+
l /= 100;
|
|
43
|
+
|
|
44
|
+
const a = s * min(l, 1 - l);
|
|
45
|
+
const k = (n: number) => (n + h / 30) % 12;
|
|
46
|
+
const f = (n: number) => l - a * max(-1, min(k(n) - 3, min(9 - k(n), 1)));
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
r: 255 * f(0),
|
|
50
|
+
g: 255 * f(8),
|
|
51
|
+
b: 255 * f(4),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { rgbToHue } from './helpers';
|
|
2
|
+
import type { HSV, RGB } from './types';
|
|
3
|
+
|
|
4
|
+
const { abs, min, max, round } = Math;
|
|
5
|
+
|
|
6
|
+
// @ref https://www.30secondsofcode.org/js/s/rgb-to-hsb/
|
|
7
|
+
/**
|
|
8
|
+
* 将RGB颜色转换为HSV颜色空间
|
|
9
|
+
* @param rgb RGB颜色对象(分量范围0-255)
|
|
10
|
+
* @returns {HSV} HSV颜色对象:
|
|
11
|
+
* h: 色相(0-360度)
|
|
12
|
+
* s: 饱和度(0-100%)
|
|
13
|
+
* v: 明度(0-100%)
|
|
14
|
+
* @see https://en.wikipedia.org/wiki/HSL_and_HSV
|
|
15
|
+
* @example
|
|
16
|
+
* rgbToHsv({r: 255, g: 0, b: 0}) // {h: 0, s: 100, v: 100}
|
|
17
|
+
*/
|
|
18
|
+
export function rgbToHsv(rgb: RGB): HSV {
|
|
19
|
+
const [hue, max, min, diff] = rgbToHue(rgb);
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
h: hue,
|
|
23
|
+
s: max && (diff / max) * 100,
|
|
24
|
+
v: max * 100,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// @ref https://www.30secondsofcode.org/js/s/hsb-to-rgb/
|
|
29
|
+
/**
|
|
30
|
+
* 将HSV颜色转换回RGB颜色空间
|
|
31
|
+
* @param hsv HSV颜色对象
|
|
32
|
+
* @param hsv.h 色相(0-360度)
|
|
33
|
+
* @param hsv.s 饱和度(0-100%)
|
|
34
|
+
* @param hsv.v 明度(0-100%)
|
|
35
|
+
* @returns {RGB} RGB颜色对象(分量范围0-255)
|
|
36
|
+
* @see https://www.rapidtables.com/convert/color/hsv-to-rgb.html
|
|
37
|
+
* @example
|
|
38
|
+
* hsvToRgb({h: 0, s: 100, v: 100}) // {r: 255, g: 0, b: 0}
|
|
39
|
+
*/
|
|
40
|
+
export function hsvToRgb({ h, s, v }: HSV): RGB {
|
|
41
|
+
s /= 100;
|
|
42
|
+
v /= 100;
|
|
43
|
+
|
|
44
|
+
const k = (n: number) => (n + h / 60) % 6;
|
|
45
|
+
const f = (n: number) => v * (1 - s * max(0, min(k(n), 4 - k(n), 1)));
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
r: 255 * f(5),
|
|
49
|
+
g: 255 * f(3),
|
|
50
|
+
b: 255 * f(1),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { rgbToHue } from './helpers';
|
|
2
|
+
import { hslToRgb } from './rgb-hsl';
|
|
3
|
+
import type { HWB, RGB } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 将RGB颜色转换为HWB颜色空间
|
|
7
|
+
* @param rgb RGB颜色对象(分量范围0-255)
|
|
8
|
+
* @returns {HWB} HWB颜色对象:
|
|
9
|
+
* h: 色相(0-360度)
|
|
10
|
+
* w: 白度(0-100%)
|
|
11
|
+
* b: 黑度(0-100%)
|
|
12
|
+
* @see https://www.w3.org/TR/css-color-4/#hwb-to-rgb
|
|
13
|
+
* @example
|
|
14
|
+
* rgbToHwb({r: 255, g: 0, b: 0}) // {h: 0, w: 0, b: 0}
|
|
15
|
+
*/
|
|
16
|
+
export function rgbToHwb(rgb: RGB): HWB {
|
|
17
|
+
const [hue, max, min, diff] = rgbToHue(rgb);
|
|
18
|
+
return {
|
|
19
|
+
h: hue,
|
|
20
|
+
w: min * 100,
|
|
21
|
+
b: (1 - max) * 100,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// @ref https://www.w3schools.com/lib/w3color.js
|
|
26
|
+
/**
|
|
27
|
+
* 将HWB颜色转换回RGB颜色空间
|
|
28
|
+
* @param hwb HWB颜色对象
|
|
29
|
+
* @param hwb.h 色相(0-360度)
|
|
30
|
+
* @param hwb.w 白度(0-100%)
|
|
31
|
+
* @param hwb.b 黑度(0-100%)
|
|
32
|
+
* @returns {RGB} RGB颜色对象(分量范围0-255)
|
|
33
|
+
* @see https://en.wikipedia.org/wiki/HWB_color_model
|
|
34
|
+
* @example
|
|
35
|
+
* hwbToRgb({h: 0, w: 0, b: 0}) // {r: 255, g: 0, b: 0}
|
|
36
|
+
*/
|
|
37
|
+
export function hwbToRgb({ h, w: white, b: black }: HWB) {
|
|
38
|
+
white /= 100;
|
|
39
|
+
black /= 100;
|
|
40
|
+
|
|
41
|
+
const { r, g, b } = hslToRgb({ h, s: 100, l: 50 });
|
|
42
|
+
const tot = white + black;
|
|
43
|
+
|
|
44
|
+
if (tot > 1) {
|
|
45
|
+
white = white / tot;
|
|
46
|
+
black = black / tot;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const f = (n: number) => ((n / 255) * (1 - white - black) + white) * 255;
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
r: f(r),
|
|
53
|
+
g: f(g),
|
|
54
|
+
b: f(b),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { rgbToXyz, xyzToRgb } from './rgb-xyz';
|
|
2
|
+
import type { LAB, RGB } from './types';
|
|
3
|
+
import { labToXyz, xyzToLab } from './xyz-lab';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 将RGB颜色转换为Lab颜色空间(CIE 1976标准)
|
|
7
|
+
* @param rgb RGB颜色对象(分量范围0-255)
|
|
8
|
+
* @returns {LAB} Lab颜色对象:
|
|
9
|
+
* l: 明度(0-100)
|
|
10
|
+
* a: 绿-红分量(-128到127)
|
|
11
|
+
* b: 蓝-黄分量(-128到127)
|
|
12
|
+
* @see https://en.wikipedia.org/wiki/CIELAB_color_space
|
|
13
|
+
* @example
|
|
14
|
+
* rgbToLab({r: 255, g: 0, b: 0}) // {l: 53.24, a: 80.09, b: 67.20}
|
|
15
|
+
*/
|
|
16
|
+
export function rgbToLab(rgb: RGB): LAB {
|
|
17
|
+
return xyzToLab(rgbToXyz(rgb));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 将Lab颜色转换回RGB颜色空间
|
|
22
|
+
* @param lab Lab颜色对象
|
|
23
|
+
* @param lab.l 明度(0-100)
|
|
24
|
+
* @param lab.a 绿-红分量(-128到127)
|
|
25
|
+
* @param lab.b 蓝-黄分量(-128到127)
|
|
26
|
+
* @returns {RGB} RGB颜色对象(分量可能超出0-255范围)
|
|
27
|
+
* @see https://www.easyrgb.com/en/math.php
|
|
28
|
+
* @example
|
|
29
|
+
* labToRgb({l: 53.24, a: 80.09, b: 67.20}) // {r: 255, g: 0, b: 0}
|
|
30
|
+
*/
|
|
31
|
+
export function labToRgb(lab: LAB): RGB {
|
|
32
|
+
return xyzToRgb(labToXyz(lab));
|
|
33
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { mix } from './mix';
|
|
2
|
+
import type { RGB } from './types';
|
|
3
|
+
|
|
4
|
+
const { abs, min, max, round } = Math;
|
|
5
|
+
|
|
6
|
+
const whiteRGB: RGB = { r: 0, g: 0, b: 0 };
|
|
7
|
+
const blackRGB: RGB = { r: 255, g: 255, b: 255 };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 通过混合颜色调整明暗度
|
|
11
|
+
* @param rgb 原始RGB颜色对象
|
|
12
|
+
* @param value 调整强度(-1到1之间):
|
|
13
|
+
* - 正值时与黑色混合(增加暗度)
|
|
14
|
+
* - 负值时与白色混合(增加亮度)
|
|
15
|
+
* @returns {RGB} 调整后的RGB颜色对象
|
|
16
|
+
* @example
|
|
17
|
+
* rgbDarken({r: 100, g: 150, b: 200}, 0.2) // 变暗20%
|
|
18
|
+
*/
|
|
19
|
+
export function rgbWhiter(rgb: RGB, value: number): RGB {
|
|
20
|
+
const rgb2: RGB = value > 0 ? whiteRGB : blackRGB;
|
|
21
|
+
return mix(rgb, rgb2, abs(value));
|
|
22
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// https://stackoverflow.com/a/73998199
|
|
2
|
+
|
|
3
|
+
import type { RGB, XYZ } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 将RGB颜色转换为CIE 1931 XYZ颜色空间(D65白点,2°观察者)
|
|
7
|
+
* @param rgb RGB颜色对象(分量范围0-255)
|
|
8
|
+
* @returns {XYZ} XYZ颜色对象:
|
|
9
|
+
* x: 约0-95.047
|
|
10
|
+
* y: 约0-100.0
|
|
11
|
+
* z: 约0-108.883
|
|
12
|
+
* @see https://en.wikipedia.org/wiki/CIE_1931_color_space
|
|
13
|
+
* @example
|
|
14
|
+
* rgbToXyz({r: 255, g: 255, b: 255}) // {x: 95.047, y: 100.0, z: 108.883}
|
|
15
|
+
*/
|
|
16
|
+
export function rgbToXyz(rgb: RGB): XYZ {
|
|
17
|
+
const { r, g, b } = rgb;
|
|
18
|
+
const [var_R, var_G, var_B] = [r, g, b]
|
|
19
|
+
.map((x) => x / 255)
|
|
20
|
+
.map((x) => (x > 0.04045 ? ((x + 0.055) / 1.055) ** 2.4 : x / 12.92))
|
|
21
|
+
.map((x) => x * 100);
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
// Observer. = 2°, Illuminant = D65
|
|
25
|
+
x: var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805,
|
|
26
|
+
y: var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722,
|
|
27
|
+
z: var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 将XYZ颜色转换回RGB颜色空间(可能超出常规范围)
|
|
33
|
+
* @param xyz XYZ颜色对象
|
|
34
|
+
* @returns {RGB} RGB颜色对象(分量可能超出0-255范围)
|
|
35
|
+
* @see https://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html
|
|
36
|
+
* @example
|
|
37
|
+
* xyzToRgb({x: 95.047, y: 100.0, z: 108.883}) // {r: 255, g: 255, b: 255}
|
|
38
|
+
*/
|
|
39
|
+
export function xyzToRgb(xyz: XYZ): RGB {
|
|
40
|
+
const { x, y, z } = xyz;
|
|
41
|
+
|
|
42
|
+
//X, Y and Z input refer to a D65/2° standard illuminant.
|
|
43
|
+
//sR, sG and sB (standard RGB) output range = 0 ÷ 255
|
|
44
|
+
|
|
45
|
+
const var_X = x / 100;
|
|
46
|
+
const var_Y = y / 100;
|
|
47
|
+
const var_Z = z / 100;
|
|
48
|
+
|
|
49
|
+
const var_R = var_X * 3.2406 + var_Y * -1.5372 + var_Z * -0.4986;
|
|
50
|
+
const var_G = var_X * -0.9689 + var_Y * 1.8758 + var_Z * 0.0415;
|
|
51
|
+
const var_B = var_X * 0.0557 + var_Y * -0.204 + var_Z * 1.057;
|
|
52
|
+
|
|
53
|
+
const [r, g, b] = [var_R, var_G, var_B]
|
|
54
|
+
.map((n) => (n > 0.0031308 ? 1.055 * n ** (1 / 2.4) - 0.055 : 12.92 * n))
|
|
55
|
+
.map((n) => n * 255);
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
r,
|
|
59
|
+
g,
|
|
60
|
+
b,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HEX颜色字符串类型(带#前缀)
|
|
3
|
+
* @example "#ff0000" // 红色
|
|
4
|
+
* @example "#0f0" // 绿色简写
|
|
5
|
+
*/
|
|
6
|
+
export type HEX = `#${string}`;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* RGB颜色空间类型
|
|
10
|
+
* @property r 红色分量(0-255)
|
|
11
|
+
* @property g 绿色分量(0-255)
|
|
12
|
+
* @property b 蓝色分量(0-255)
|
|
13
|
+
* @example {r: 255, g: 0, b: 0} // 纯红色
|
|
14
|
+
*/
|
|
15
|
+
export type RGB = { r: number; g: number; b: number };
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* HSV/HSB颜色空间类型
|
|
19
|
+
* @property h 色相(0-360度)
|
|
20
|
+
* @property s 饱和度(0-100%)
|
|
21
|
+
* @property v 明度(0-100%)
|
|
22
|
+
* @see https://zh.wikipedia.org/wiki/HSL%E5%92%8CHSV%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4
|
|
23
|
+
* @example {h: 0, s: 100, v: 100} // 纯红色
|
|
24
|
+
*/
|
|
25
|
+
export type HSV = { h: number; s: number; v: number };
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* HSL颜色空间类型
|
|
29
|
+
* @property h 色相(0-360度)
|
|
30
|
+
* @property s 饱和度(0-100%)
|
|
31
|
+
* @property l 亮度(0-100%)
|
|
32
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl
|
|
33
|
+
* @example {h: 120, s: 100, l: 50} // 纯绿色
|
|
34
|
+
*/
|
|
35
|
+
export type HSL = { h: number; s: number; l: number };
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* HWB颜色空间类型(W3C标准)
|
|
39
|
+
* @property h 色相(0-360度)
|
|
40
|
+
* @property w 白度(0-100%)
|
|
41
|
+
* @property b 黑度(0-100%)
|
|
42
|
+
* @see https://zh.wikipedia.org/zh-hans/HWB%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4
|
|
43
|
+
* @example {h: 0, w: 0, b: 0} // 纯红色
|
|
44
|
+
*/
|
|
45
|
+
export type HWB = { h: number; w: number; b: number };
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* CIE LAB颜色空间类型(L*a*b*)
|
|
49
|
+
* @property l 明度(0-100)
|
|
50
|
+
* @property a 绿-红分量(典型范围-128到127)
|
|
51
|
+
* @property b 蓝-黄分量(典型范围-128到127)
|
|
52
|
+
* @see https://zh.m.wikipedia.org/wiki/CIELAB%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4
|
|
53
|
+
* @example {l: 53.24, a: 80.09, b: 67.20} // 亮红色
|
|
54
|
+
*/
|
|
55
|
+
export type LAB = { l: number; a: number; b: number };
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* CIE 1931 XYZ颜色空间类型
|
|
59
|
+
* @property x XYZ三刺激值X分量
|
|
60
|
+
* @property y XYZ三刺激值Y分量(亮度基准)
|
|
61
|
+
* @property z XYZ三刺激值Z分量
|
|
62
|
+
* @see // https://zh.m.wikipedia.org/wiki/CIE_1931%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4
|
|
63
|
+
* @example {x: 95.047, y: 100.0, z: 108.883} // D65标准白点
|
|
64
|
+
*/
|
|
65
|
+
export type XYZ = { x: number; y: number; z: number };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// https://stackoverflow.com/a/73998199
|
|
2
|
+
|
|
3
|
+
import type { LAB, XYZ } from './types';
|
|
4
|
+
|
|
5
|
+
const ref_X = 95.047;
|
|
6
|
+
const ref_Y = 100.0;
|
|
7
|
+
const ref_Z = 108.883;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 将XYZ颜色转换为Lab颜色空间(CIE 1976 L*a*b*,D65白点)
|
|
11
|
+
* @param xyz XYZ颜色对象(参考值:D65白点X=95.047,Y=100,Z=108.883)
|
|
12
|
+
* @returns {LAB} Lab颜色对象:
|
|
13
|
+
* l: 明度(0-100)
|
|
14
|
+
* a: 绿-红分量(典型范围-128到127)
|
|
15
|
+
* b: 蓝-黄分量(典型范围-128到127)
|
|
16
|
+
* @see https://en.wikipedia.org/wiki/CIELAB_color_space
|
|
17
|
+
* @example
|
|
18
|
+
* xyzToLab({x: 95.047, y: 100.0, z: 108.883}) // {l: 100, a: 0, b: 0}
|
|
19
|
+
*/
|
|
20
|
+
export function xyzToLab(xyz: XYZ): LAB {
|
|
21
|
+
const { x, y, z } = xyz;
|
|
22
|
+
const [var_X, var_Y, var_Z] = [x / ref_X, y / ref_Y, z / ref_Z].map((a) =>
|
|
23
|
+
a > 0.008856 ? a ** (1 / 3) : 7.787 * a + 16 / 116,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const l = 116 * var_Y - 16;
|
|
27
|
+
const a = 500 * (var_X - var_Y);
|
|
28
|
+
const b = 200 * (var_Y - var_Z);
|
|
29
|
+
|
|
30
|
+
return { l, a, b };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 将Lab颜色转换回XYZ颜色空间
|
|
35
|
+
* @param lab Lab颜色对象
|
|
36
|
+
* @param lab.l 明度(0-100)
|
|
37
|
+
* @param lab.a 绿-红分量(典型范围-128到127)
|
|
38
|
+
* @param lab.b 蓝-黄分量(典型范围-128到127)
|
|
39
|
+
* @returns {XYZ} XYZ颜色对象(基于D65白点)
|
|
40
|
+
* @see https://www.easyrgb.com/en/math.php
|
|
41
|
+
* @example
|
|
42
|
+
* labToXyz({l: 100, a: 0, b: 0}) // {x: 95.047, y: 100.0, z: 108.883}
|
|
43
|
+
*/
|
|
44
|
+
export function labToXyz(lab: LAB): XYZ {
|
|
45
|
+
const { l, a, b } = lab;
|
|
46
|
+
|
|
47
|
+
const var_Y = (l + 16) / 116;
|
|
48
|
+
const var_X = a / 500 + var_Y;
|
|
49
|
+
const var_Z = var_Y - b / 200;
|
|
50
|
+
|
|
51
|
+
const [X, Y, Z] = [var_X, var_Y, var_Z].map((n) => (n ** 3 > 0.008856 ? n ** 3 : (n - 16 / 116) / 7.787));
|
|
52
|
+
|
|
53
|
+
return { x: X * ref_X, y: Y * ref_Y, z: Z * ref_Z };
|
|
54
|
+
}
|
package/src/color.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export * from './color/contrast';
|
|
2
|
+
export * from './color/distance';
|
|
3
|
+
export * from './color/hex-hsl';
|
|
4
|
+
export * from './color/hex-hsv';
|
|
5
|
+
export * from './color/hex-hwb';
|
|
6
|
+
export * from './color/hex-rgb';
|
|
7
|
+
export * from './color/hsl-lighten';
|
|
8
|
+
export * from './color/hsv-brighten';
|
|
9
|
+
export * from './color/luminance';
|
|
10
|
+
export * from './color/mix';
|
|
11
|
+
export * from './color/rgb-hsl';
|
|
12
|
+
export * from './color/rgb-hsv';
|
|
13
|
+
export * from './color/rgb-hwb';
|
|
14
|
+
export * from './color/rgb-lab';
|
|
15
|
+
export * from './color/rgb-xyz';
|
|
16
|
+
export * from './color/xyz-lab';
|
|
17
|
+
export * from './color/rgb-whiter';
|
|
18
|
+
export * from './color/types';
|
|
19
|
+
export { rgbToHue } from './color/helpers';
|