@cloudcome/utils-core 1.1.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 +279 -1
- 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/{dist/color/contrast.d.ts → src/color/contrast.ts} +12 -2
- package/src/color/distance.ts +28 -0
- package/src/color/helpers.ts +23 -0
- package/src/color/hex-hsl.ts +11 -0
- package/{dist/color/hex-hsv.d.ts → src/color/hex-hsv.ts} +11 -3
- package/{dist/color/hex-hwb.d.ts → src/color/hex-hwb.ts} +11 -3
- package/src/color/hex-rgb.ts +39 -0
- package/{dist/color/hsl-lighten.d.ts → src/color/hsl-lighten.ts} +7 -2
- package/{dist/color/hsv-brighten.d.ts → src/color/hsv-brighten.ts} +7 -2
- package/{dist/color/luminance.d.ts → src/color/luminance.ts} +9 -2
- package/{dist/color/mix.d.ts → src/color/mix.ts} +10 -2
- package/src/color/rgb-hsl.ts +53 -0
- package/{dist/color/rgb-hsv.d.ts → src/color/rgb-hsv.ts} +30 -3
- package/src/color/rgb-hwb.ts +56 -0
- package/{dist/color/rgb-lab.d.ts → src/color/rgb-lab.ts} +11 -3
- package/src/color/rgb-whiter.ts +22 -0
- package/src/color/rgb-xyz.ts +62 -0
- package/{dist/color/types.d.ts → src/color/types.ts} +12 -30
- package/{dist/color/xyz-lab.d.ts → src/color/xyz-lab.ts} +32 -3
- 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/{dist/crypto.d.ts → src/crypto.ts} +20 -4
- package/src/date/const.ts +6 -0
- package/src/date/core.ts +162 -0
- package/src/date/days.ts +51 -0
- package/{dist/date/is.d.ts → src/date/is.ts} +102 -8
- 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/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/{dist/env.d.ts → src/env.ts} +30 -6
- package/{dist/error.d.ts → src/error.ts} +12 -3
- 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/{dist/object/each.d.ts → src/object/each.ts} +23 -3
- package/src/object/get-set.ts +273 -0
- package/src/object/is.ts +128 -0
- package/src/object/merge.ts +180 -0
- package/{dist/object/process.d.ts → src/object/process.ts} +38 -4
- package/src/path.ts +188 -0
- package/{dist/promise.d.ts → src/promise.ts} +67 -6
- package/{dist/qs.d.ts → src/qs.ts} +60 -3
- 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/{dist/timer.mjs → src/timer.ts} +124 -17
- package/{dist/tree.d.ts → src/tree.ts} +225 -41
- package/{dist/type.d.ts → src/type.ts} +96 -20
- package/{dist/types.d.ts → src/types.ts} +33 -12
- 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
- package/dist/array.cjs +0 -129
- package/dist/array.cjs.map +0 -1
- package/dist/array.d.ts +0 -171
- package/dist/array.mjs +0 -129
- package/dist/array.mjs.map +0 -1
- package/dist/async.cjs +0 -219
- package/dist/async.cjs.map +0 -1
- package/dist/async.d.ts +0 -137
- package/dist/async.mjs +0 -219
- package/dist/async.mjs.map +0 -1
- package/dist/base64.cjs +0 -16
- package/dist/base64.cjs.map +0 -1
- package/dist/base64.d.ts +0 -7
- package/dist/base64.mjs +0 -16
- package/dist/base64.mjs.map +0 -1
- package/dist/cache.cjs +0 -79
- package/dist/cache.cjs.map +0 -1
- package/dist/cache.d.ts +0 -90
- package/dist/cache.mjs +0 -79
- package/dist/cache.mjs.map +0 -1
- package/dist/color/distance.d.ts +0 -8
- package/dist/color/helpers.d.ts +0 -2
- package/dist/color/hex-hsl.d.ts +0 -3
- package/dist/color/hex-rgb.d.ts +0 -18
- package/dist/color/rgb-hsl.d.ts +0 -23
- package/dist/color/rgb-hwb.d.ts +0 -29
- package/dist/color/rgb-whiter.d.ts +0 -12
- package/dist/color/rgb-xyz.d.ts +0 -22
- package/dist/color.cjs +0 -250
- package/dist/color.cjs.map +0 -1
- package/dist/color.mjs +0 -250
- package/dist/color.mjs.map +0 -1
- package/dist/const.cjs +0 -14
- package/dist/const.cjs.map +0 -1
- package/dist/const.mjs +0 -15
- package/dist/const.mjs.map +0 -1
- package/dist/core.cjs +0 -250
- package/dist/core.cjs.map +0 -1
- package/dist/core.mjs +0 -251
- package/dist/core.mjs.map +0 -1
- package/dist/crypto/md5.d.mts +0 -1
- package/dist/crypto/sha1.d.mts +0 -1
- package/dist/crypto/sha256.d.mts +0 -1
- package/dist/crypto/sha512.d.mts +0 -1
- package/dist/crypto.cjs +0 -812
- package/dist/crypto.cjs.map +0 -1
- package/dist/crypto.mjs +0 -812
- package/dist/crypto.mjs.map +0 -1
- package/dist/date/const.d.ts +0 -6
- package/dist/date/core.d.ts +0 -52
- package/dist/date/days.d.ts +0 -23
- package/dist/date/relative.d.ts +0 -44
- package/dist/date/start-end.d.ts +0 -73
- package/dist/date/timezone.d.ts +0 -67
- package/dist/date/weeks.d.ts +0 -72
- package/dist/date.cjs +0 -239
- package/dist/date.cjs.map +0 -1
- package/dist/date.mjs +0 -241
- package/dist/date.mjs.map +0 -1
- package/dist/dict.cjs +0 -2
- package/dist/dict.cjs.map +0 -1
- package/dist/dict.mjs +0 -2
- package/dist/dict.mjs.map +0 -1
- package/dist/each.cjs +0 -18
- package/dist/each.cjs.map +0 -1
- package/dist/each.mjs +0 -19
- package/dist/each.mjs.map +0 -1
- package/dist/easing.cjs +0 -151
- package/dist/easing.cjs.map +0 -1
- package/dist/easing.d.ts +0 -46
- package/dist/easing.mjs +0 -151
- package/dist/easing.mjs.map +0 -1
- package/dist/emitter.cjs +0 -94
- package/dist/emitter.cjs.map +0 -1
- package/dist/emitter.d.ts +0 -68
- package/dist/emitter.mjs +0 -94
- package/dist/emitter.mjs.map +0 -1
- package/dist/enum.cjs +0 -58
- package/dist/enum.cjs.map +0 -1
- package/dist/enum.d.ts +0 -68
- package/dist/enum.mjs +0 -58
- package/dist/enum.mjs.map +0 -1
- package/dist/env.cjs +0 -28
- package/dist/env.cjs.map +0 -1
- package/dist/env.mjs +0 -28
- package/dist/env.mjs.map +0 -1
- package/dist/error.cjs +0 -12
- package/dist/error.cjs.map +0 -1
- package/dist/error.mjs +0 -12
- package/dist/error.mjs.map +0 -1
- package/dist/exception.cjs +0 -22
- package/dist/exception.cjs.map +0 -1
- package/dist/exception.d.ts +0 -31
- package/dist/exception.mjs +0 -22
- package/dist/exception.mjs.map +0 -1
- package/dist/fn.cjs +0 -76
- package/dist/fn.cjs.map +0 -1
- package/dist/fn.d.ts +0 -102
- package/dist/fn.mjs +0 -76
- package/dist/fn.mjs.map +0 -1
- package/dist/index.cjs +0 -5
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.mjs +0 -5
- package/dist/index.mjs.map +0 -1
- package/dist/merge.cjs +0 -87
- package/dist/merge.cjs.map +0 -1
- package/dist/merge.mjs +0 -88
- package/dist/merge.mjs.map +0 -1
- package/dist/number.cjs +0 -11
- package/dist/number.cjs.map +0 -1
- package/dist/number.d.ts +0 -137
- package/dist/number.mjs +0 -11
- package/dist/number.mjs.map +0 -1
- package/dist/object/get-set.d.ts +0 -111
- package/dist/object/is.d.ts +0 -32
- package/dist/object/merge.d.ts +0 -72
- package/dist/object.cjs +0 -130
- package/dist/object.cjs.map +0 -1
- package/dist/object.mjs +0 -130
- package/dist/object.mjs.map +0 -1
- package/dist/path.cjs +0 -77
- package/dist/path.cjs.map +0 -1
- package/dist/path.d.ts +0 -82
- package/dist/path.mjs +0 -77
- package/dist/path.mjs.map +0 -1
- package/dist/promise.cjs +0 -62
- package/dist/promise.cjs.map +0 -1
- package/dist/promise.mjs +0 -62
- package/dist/promise.mjs.map +0 -1
- package/dist/qs.cjs +0 -47
- package/dist/qs.cjs.map +0 -1
- package/dist/qs.mjs +0 -47
- package/dist/qs.mjs.map +0 -1
- package/dist/regexp.cjs +0 -66
- package/dist/regexp.cjs.map +0 -1
- package/dist/regexp.d.ts +0 -65
- package/dist/regexp.mjs +0 -66
- package/dist/regexp.mjs.map +0 -1
- package/dist/string.cjs +0 -16
- package/dist/string.cjs.map +0 -1
- package/dist/string.d.ts +0 -80
- package/dist/string.mjs +0 -16
- package/dist/string.mjs.map +0 -1
- package/dist/string2.cjs +0 -147
- package/dist/string2.cjs.map +0 -1
- package/dist/string2.mjs +0 -148
- package/dist/string2.mjs.map +0 -1
- package/dist/time/from.d.ts +0 -14
- package/dist/time/to.d.ts +0 -38
- package/dist/time.cjs +0 -82
- package/dist/time.cjs.map +0 -1
- package/dist/time.mjs +0 -82
- package/dist/time.mjs.map +0 -1
- package/dist/timer.cjs +0 -119
- package/dist/timer.cjs.map +0 -1
- package/dist/timer.d.ts +0 -96
- package/dist/timer.mjs.map +0 -1
- package/dist/tree.cjs +0 -125
- package/dist/tree.cjs.map +0 -1
- package/dist/tree.mjs +0 -125
- package/dist/tree.mjs.map +0 -1
- package/dist/type.cjs +0 -78
- package/dist/type.cjs.map +0 -1
- package/dist/type.mjs +0 -78
- package/dist/type.mjs.map +0 -1
- package/dist/types.cjs +0 -2
- package/dist/types.cjs.map +0 -1
- package/dist/types.mjs +0 -2
- package/dist/types.mjs.map +0 -1
- package/dist/unique.cjs +0 -46
- package/dist/unique.cjs.map +0 -1
- package/dist/unique.d.ts +0 -22
- package/dist/unique.mjs +0 -46
- package/dist/unique.mjs.map +0 -1
- package/dist/url.cjs +0 -37
- package/dist/url.cjs.map +0 -1
- package/dist/url.d.ts +0 -53
- package/dist/url.mjs +0 -37
- package/dist/url.mjs.map +0 -1
- package/dist/version.cjs +0 -33
- package/dist/version.cjs.map +0 -1
- package/dist/version.d.ts +0 -32
- package/dist/version.mjs +0 -33
- package/dist/version.mjs.map +0 -1
- /package/{dist/color.d.ts → src/color.ts} +0 -0
- /package/{dist/date.d.ts → src/date.ts} +0 -0
- /package/{dist/dict.d.ts → src/dict.ts} +0 -0
- /package/{dist/object.d.ts → src/object.ts} +0 -0
- /package/{dist/time.d.ts → src/time.ts} +0 -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
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { HSL } from './types';
|
|
1
|
+
import type { HSL } from './types';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* 通过HSL颜色空间调整颜色亮度
|
|
4
5
|
* @param hsl 原始 HSL 颜色
|
|
@@ -7,4 +8,8 @@ import { HSL } from './types';
|
|
|
7
8
|
* @example
|
|
8
9
|
* hslLighten({h: 300, s: 33, l: 44}, 0.2) // 返回亮度增加20%后的颜色
|
|
9
10
|
*/
|
|
10
|
-
export
|
|
11
|
+
export function hslLighten(hsl: HSL, value: number): HSL {
|
|
12
|
+
const hslFinal = { ...hsl };
|
|
13
|
+
hslFinal.l = hslFinal.l * (1 + value);
|
|
14
|
+
return hslFinal;
|
|
15
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { HSV } from './types';
|
|
1
|
+
import type { HSV } from './types';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* 通过HSV颜色空间调整颜色明度
|
|
4
5
|
* @param hsv 原始HSV颜色对象
|
|
@@ -9,4 +10,8 @@ import { HSV } from './types';
|
|
|
9
10
|
* @example
|
|
10
11
|
* hsvBrighten({h: 0, s: 100, v: 50}, 0.3) // 返回{h: 0, s: 100, v: 65}
|
|
11
12
|
*/
|
|
12
|
-
export
|
|
13
|
+
export function hsvBrighten(hsv: HSV, value: number): HSV {
|
|
14
|
+
const hsvFinal = { ...hsv };
|
|
15
|
+
hsvFinal.v = hsvFinal.v * (1 + value);
|
|
16
|
+
return hsvFinal;
|
|
17
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { RGB } from './types';
|
|
1
|
+
import type { RGB } from './types';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* 计算RGB颜色的相对亮度(符合WCAG 2.1标准)
|
|
4
5
|
* @param rgb RGB颜色对象(分量范围0-255)
|
|
@@ -7,4 +8,10 @@ import { RGB } from './types';
|
|
|
7
8
|
* @example
|
|
8
9
|
* luminance({r: 255, g: 255, b: 255}) // 返回1
|
|
9
10
|
*/
|
|
10
|
-
export
|
|
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
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { HSL, HSV, RGB } from './types';
|
|
1
|
+
import type { HSL, HSV, RGB } from './types';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* 颜色混合函数(支持RGB/HSL/HSV色彩模型)
|
|
4
5
|
* @template T 颜色类型,支持RGB、HSL或HSV对象
|
|
@@ -15,4 +16,11 @@ import { HSL, HSV, RGB } from './types';
|
|
|
15
16
|
* // HSL混合示例
|
|
16
17
|
* mix({h: 0, s: 100, l: 50}, {h: 120, s: 100, l: 50}, 0.3)
|
|
17
18
|
*/
|
|
18
|
-
export
|
|
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
|
+
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
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/
|
|
2
7
|
/**
|
|
3
8
|
* 将RGB颜色转换为HSV颜色空间
|
|
4
9
|
* @param rgb RGB颜色对象(分量范围0-255)
|
|
@@ -10,7 +15,17 @@ import { HSV, RGB } from './types';
|
|
|
10
15
|
* @example
|
|
11
16
|
* rgbToHsv({r: 255, g: 0, b: 0}) // {h: 0, s: 100, v: 100}
|
|
12
17
|
*/
|
|
13
|
-
export
|
|
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/
|
|
14
29
|
/**
|
|
15
30
|
* 将HSV颜色转换回RGB颜色空间
|
|
16
31
|
* @param hsv HSV颜色对象
|
|
@@ -22,4 +37,16 @@ export declare function rgbToHsv(rgb: RGB): HSV;
|
|
|
22
37
|
* @example
|
|
23
38
|
* hsvToRgb({h: 0, s: 100, v: 100}) // {r: 255, g: 0, b: 0}
|
|
24
39
|
*/
|
|
25
|
-
export
|
|
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
|
+
}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rgbToXyz, xyzToRgb } from './rgb-xyz';
|
|
2
|
+
import type { LAB, RGB } from './types';
|
|
3
|
+
import { labToXyz, xyzToLab } from './xyz-lab';
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* 将RGB颜色转换为Lab颜色空间(CIE 1976标准)
|
|
4
7
|
* @param rgb RGB颜色对象(分量范围0-255)
|
|
@@ -10,7 +13,10 @@ import { LAB, RGB } from './types';
|
|
|
10
13
|
* @example
|
|
11
14
|
* rgbToLab({r: 255, g: 0, b: 0}) // {l: 53.24, a: 80.09, b: 67.20}
|
|
12
15
|
*/
|
|
13
|
-
export
|
|
16
|
+
export function rgbToLab(rgb: RGB): LAB {
|
|
17
|
+
return xyzToLab(rgbToXyz(rgb));
|
|
18
|
+
}
|
|
19
|
+
|
|
14
20
|
/**
|
|
15
21
|
* 将Lab颜色转换回RGB颜色空间
|
|
16
22
|
* @param lab Lab颜色对象
|
|
@@ -22,4 +28,6 @@ export declare function rgbToLab(rgb: RGB): LAB;
|
|
|
22
28
|
* @example
|
|
23
29
|
* labToRgb({l: 53.24, a: 80.09, b: 67.20}) // {r: 255, g: 0, b: 0}
|
|
24
30
|
*/
|
|
25
|
-
export
|
|
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
|
+
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @example "#0f0" // 绿色简写
|
|
5
5
|
*/
|
|
6
6
|
export type HEX = `#${string}`;
|
|
7
|
+
|
|
7
8
|
/**
|
|
8
9
|
* RGB颜色空间类型
|
|
9
10
|
* @property r 红色分量(0-255)
|
|
@@ -11,11 +12,8 @@ export type HEX = `#${string}`;
|
|
|
11
12
|
* @property b 蓝色分量(0-255)
|
|
12
13
|
* @example {r: 255, g: 0, b: 0} // 纯红色
|
|
13
14
|
*/
|
|
14
|
-
export type RGB = {
|
|
15
|
-
|
|
16
|
-
g: number;
|
|
17
|
-
b: number;
|
|
18
|
-
};
|
|
15
|
+
export type RGB = { r: number; g: number; b: number };
|
|
16
|
+
|
|
19
17
|
/**
|
|
20
18
|
* HSV/HSB颜色空间类型
|
|
21
19
|
* @property h 色相(0-360度)
|
|
@@ -24,11 +22,8 @@ export type RGB = {
|
|
|
24
22
|
* @see https://zh.wikipedia.org/wiki/HSL%E5%92%8CHSV%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4
|
|
25
23
|
* @example {h: 0, s: 100, v: 100} // 纯红色
|
|
26
24
|
*/
|
|
27
|
-
export type HSV = {
|
|
28
|
-
|
|
29
|
-
s: number;
|
|
30
|
-
v: number;
|
|
31
|
-
};
|
|
25
|
+
export type HSV = { h: number; s: number; v: number };
|
|
26
|
+
|
|
32
27
|
/**
|
|
33
28
|
* HSL颜色空间类型
|
|
34
29
|
* @property h 色相(0-360度)
|
|
@@ -37,11 +32,8 @@ export type HSV = {
|
|
|
37
32
|
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl
|
|
38
33
|
* @example {h: 120, s: 100, l: 50} // 纯绿色
|
|
39
34
|
*/
|
|
40
|
-
export type HSL = {
|
|
41
|
-
|
|
42
|
-
s: number;
|
|
43
|
-
l: number;
|
|
44
|
-
};
|
|
35
|
+
export type HSL = { h: number; s: number; l: number };
|
|
36
|
+
|
|
45
37
|
/**
|
|
46
38
|
* HWB颜色空间类型(W3C标准)
|
|
47
39
|
* @property h 色相(0-360度)
|
|
@@ -50,11 +42,8 @@ export type HSL = {
|
|
|
50
42
|
* @see https://zh.wikipedia.org/zh-hans/HWB%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4
|
|
51
43
|
* @example {h: 0, w: 0, b: 0} // 纯红色
|
|
52
44
|
*/
|
|
53
|
-
export type HWB = {
|
|
54
|
-
|
|
55
|
-
w: number;
|
|
56
|
-
b: number;
|
|
57
|
-
};
|
|
45
|
+
export type HWB = { h: number; w: number; b: number };
|
|
46
|
+
|
|
58
47
|
/**
|
|
59
48
|
* CIE LAB颜色空间类型(L*a*b*)
|
|
60
49
|
* @property l 明度(0-100)
|
|
@@ -63,11 +52,8 @@ export type HWB = {
|
|
|
63
52
|
* @see https://zh.m.wikipedia.org/wiki/CIELAB%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4
|
|
64
53
|
* @example {l: 53.24, a: 80.09, b: 67.20} // 亮红色
|
|
65
54
|
*/
|
|
66
|
-
export type LAB = {
|
|
67
|
-
|
|
68
|
-
a: number;
|
|
69
|
-
b: number;
|
|
70
|
-
};
|
|
55
|
+
export type LAB = { l: number; a: number; b: number };
|
|
56
|
+
|
|
71
57
|
/**
|
|
72
58
|
* CIE 1931 XYZ颜色空间类型
|
|
73
59
|
* @property x XYZ三刺激值X分量
|
|
@@ -76,8 +62,4 @@ export type LAB = {
|
|
|
76
62
|
* @see // https://zh.m.wikipedia.org/wiki/CIE_1931%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4
|
|
77
63
|
* @example {x: 95.047, y: 100.0, z: 108.883} // D65标准白点
|
|
78
64
|
*/
|
|
79
|
-
export type XYZ = {
|
|
80
|
-
x: number;
|
|
81
|
-
y: number;
|
|
82
|
-
z: number;
|
|
83
|
-
};
|
|
65
|
+
export type XYZ = { x: number; y: number; z: number };
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
|
|
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
|
+
|
|
2
9
|
/**
|
|
3
10
|
* 将XYZ颜色转换为Lab颜色空间(CIE 1976 L*a*b*,D65白点)
|
|
4
11
|
* @param xyz XYZ颜色对象(参考值:D65白点X=95.047,Y=100,Z=108.883)
|
|
@@ -10,7 +17,19 @@ import { LAB, XYZ } from './types';
|
|
|
10
17
|
* @example
|
|
11
18
|
* xyzToLab({x: 95.047, y: 100.0, z: 108.883}) // {l: 100, a: 0, b: 0}
|
|
12
19
|
*/
|
|
13
|
-
export
|
|
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
|
+
|
|
14
33
|
/**
|
|
15
34
|
* 将Lab颜色转换回XYZ颜色空间
|
|
16
35
|
* @param lab Lab颜色对象
|
|
@@ -22,4 +41,14 @@ export declare function xyzToLab(xyz: XYZ): LAB;
|
|
|
22
41
|
* @example
|
|
23
42
|
* labToXyz({l: 100, a: 0, b: 0}) // {x: 95.047, y: 100.0, z: 108.883}
|
|
24
43
|
*/
|
|
25
|
-
export
|
|
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
|
+
}
|