@hairy/palette 0.3.12 → 1.5.0

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.
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019-PRESENT Hairyf<https://github.com/hairyf>
3
+ Copyright (c) 2025-PRESENT Hairyf <https://github.com/antfu>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -19,5 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
-
23
-
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # @hairy/palette
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![bundle][bundle-src]][bundle-href]
6
+ [![JSDocs][jsdocs-src]][jsdocs-href]
7
+ [![License][license-src]][license-href]
8
+
9
+ ## Install
10
+
11
+ ```
12
+ ni @hairy/palette
13
+ ```
14
+
15
+ ## CDN
16
+
17
+ ```html
18
+ <script src="https://unpkg.com/@hairy/palette"></script>
19
+ ```
20
+
21
+ ## License
22
+
23
+ [MIT](./LICENSE) License © [Hairyf](https://github.com/hairyf)
24
+
25
+ <!-- Badges -->
26
+
27
+ [npm-version-src]: https://img.shields.io/npm/v/@hairy/palette?style=flat&colorA=080f12&colorB=1fa669
28
+ [npm-version-href]: https://npmjs.com/package/@hairy/palette
29
+ [npm-downloads-src]: https://img.shields.io/npm/dm/@hairy/palette?style=flat&colorA=080f12&colorB=1fa669
30
+ [npm-downloads-href]: https://npmjs.com/package/@hairy/palette
31
+ [bundle-src]: https://img.shields.io/bundlephobia/minzip/@hairy/palette?style=flat&colorA=080f12&colorB=1fa669&label=minzip
32
+ [bundle-href]: https://bundlephobia.com/result?p=@hairy/palette
33
+ [license-src]: https://img.shields.io/github/license/hairyf/hairylib.svg?style=flat&colorA=080f12&colorB=1fa669
34
+ [license-href]: https://github.com/hairyf/hairylib/blob/main/LICENSE
35
+ [jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
36
+ [jsdocs-href]: https://www.jsdocs.io/package/@hairy/palette
package/dist/index.cjs CHANGED
@@ -28,8 +28,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
30
  // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
33
  blend: () => blend,
34
34
  brightness: () => brightness,
35
35
  changeAlpha: () => changeAlpha,
@@ -42,10 +42,10 @@ __export(src_exports, {
42
42
  rgbToHsv: () => rgbToHsv,
43
43
  textToRgb: () => textToRgb
44
44
  });
45
- module.exports = __toCommonJS(src_exports);
45
+ module.exports = __toCommonJS(index_exports);
46
46
  var import_colord = require("colord");
47
- var import_a11y = __toESM(require("colord/plugins/a11y"));
48
- var import_mix = __toESM(require("colord/plugins/mix"));
47
+ var import_a11y = __toESM(require("colord/plugins/a11y"), 1);
48
+ var import_mix = __toESM(require("colord/plugins/mix"), 1);
49
49
  (0, import_colord.extend)([import_a11y.default, import_mix.default]);
50
50
  function rgbToHex({ r, g, b, a }) {
51
51
  return (0, import_colord.colord)({ r, g, b, a }).toHex();
@@ -120,8 +120,7 @@ function saturation(hsv, i, isLight) {
120
120
  saturation2 = hsv.s - saturationStep * i;
121
121
  else if (i === darkColorCount)
122
122
  saturation2 = hsv.s + saturationStep;
123
- else
124
- saturation2 = hsv.s + saturationStep2 * i;
123
+ else saturation2 = hsv.s + saturationStep2 * i;
125
124
  if (saturation2 > 100)
126
125
  saturation2 = 100;
127
126
  if (isLight && i === lightColorCount && saturation2 > 10)
package/dist/index.d.ts CHANGED
@@ -1,111 +1,111 @@
1
1
  import * as colord from 'colord';
2
2
  import { RgbaColor, HsvaColor, AnyColor } from 'colord';
3
3
 
4
- type RGBA = Record<'r' | 'g' | 'b' | 'a', number>;
5
- type RGB = Record<'r' | 'g' | 'b', number> & {
6
- a?: number;
7
- };
8
- type HexColor = string;
9
- type HSVA = Record<'h' | 's' | 'v' | 'a', number>;
10
- type RGBA_TEXT = string;
11
- type HEX_TEXT = string;
12
- type HSVA_TEXT = string;
13
- type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT;
14
- type PALETTE_INDEXES = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
15
- /**
16
- * Converts a RGB/A color
17
- *
18
- * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
19
- *
20
- * to its HEX/A representation as a
21
- *
22
- * String (`#RRGGBB<AA>`).
23
- *
24
- * If Alpha channel is present in the original object it will be present also in the output.
25
- */
26
- declare function rgbToHex({ r, g, b, a }: RgbaColor): HexColor;
27
- /**
28
- * Converts a HEX/A color
29
- *
30
- * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
31
- *
32
- * to its RGB/A representation as an
33
- *
34
- * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
35
- *
36
- * If Alpha channel is present in the original object it will be present also in the output.
37
- */
38
- declare function hexToRgb(hex: HexColor): RgbaColor;
39
- /**
40
- * Converts a HEX/A color
41
- *
42
- * String (`#RRGGBB<AA>`)
43
- *
44
- * to its RGB/A representation as an
45
- *
46
- * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .
47
- *
48
- * If Alpha channel is present in the original object it will be present also in the output.
49
- */
50
- declare function hsvToRgb({ h, s, v, a }: HsvaColor): RgbaColor;
51
- /**
52
- * Converts a RGB/A color
53
- *
54
- * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
55
- *
56
- * to its HSV/A representation as an
57
- *
58
- * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-1]}`).
59
- *
60
- * If Alpha channel is present in the original object it will be present also in the output.
61
- */
62
- declare function rgbToHsv({ r, g, b, a }: RgbaColor): HsvaColor;
63
- /**
64
- * Converts a HEX/A color
65
- *
66
- * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
67
- *
68
- * to its RGB/A representation as an
69
- *
70
- * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
71
- *
72
- * If Alpha channel is present in the original object it will be present also in the output.
73
- */
74
- declare function textToRgb(str: AnyColor): RgbaColor;
75
- /**
76
- * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).
77
- *
78
- * Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 1 or -1 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.
79
- */
80
- declare function lighten(color: AnyColor, percent: number): string;
81
- /**
82
- * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.
83
- *
84
- * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
85
- */
86
- declare function luminance(color: AnyColor): number;
87
- /**
88
- * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.
89
- *
90
- * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
91
- */
92
- declare function brightness(color: AnyColor): number;
93
- /**
94
- * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.
95
- *
96
- * Accepts a HEX/A String or a RGB/A Object as `fgColor`/`bgColor`. If the alpha channel of the `fgColor` is completely opaque, then the result will be the `fgColor`. If the alpha channel of the `bgColor` is completely opaque, then the resulting blended color will also be opaque. Returns the same type as input for fgColor.
97
- */
98
- declare function blend(fgColor: AnyColor, bgColor: AnyColor): colord.Colord;
99
- /**
100
- * Change color transparency
101
- */
102
- declare function changeAlpha(color: COLOR, alpha: number): string;
103
- /**
104
- * 根据颜色获取调色板颜色(从左至右颜色从浅到深,6为主色号)
105
- * @param color - 颜色
106
- * @param index - 调色板的对应的色号(6为主色号)
107
- * @description 算法实现从ant-design调色板算法中借鉴 https://github.com/ant-design/ant-design/blob/master/components/style/color/colorPalette.less
108
- */
4
+ type RGBA = Record<'r' | 'g' | 'b' | 'a', number>;
5
+ type RGB = Record<'r' | 'g' | 'b', number> & {
6
+ a?: number;
7
+ };
8
+ type HexColor = string;
9
+ type HSVA = Record<'h' | 's' | 'v' | 'a', number>;
10
+ type RGBA_TEXT = string;
11
+ type HEX_TEXT = string;
12
+ type HSVA_TEXT = string;
13
+ type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT;
14
+ type PALETTE_INDEXES = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
15
+ /**
16
+ * Converts a RGB/A color
17
+ *
18
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
19
+ *
20
+ * to its HEX/A representation as a
21
+ *
22
+ * String (`#RRGGBB<AA>`).
23
+ *
24
+ * If Alpha channel is present in the original object it will be present also in the output.
25
+ */
26
+ declare function rgbToHex({ r, g, b, a }: RgbaColor): HexColor;
27
+ /**
28
+ * Converts a HEX/A color
29
+ *
30
+ * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
31
+ *
32
+ * to its RGB/A representation as an
33
+ *
34
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
35
+ *
36
+ * If Alpha channel is present in the original object it will be present also in the output.
37
+ */
38
+ declare function hexToRgb(hex: HexColor): RgbaColor;
39
+ /**
40
+ * Converts a HEX/A color
41
+ *
42
+ * String (`#RRGGBB<AA>`)
43
+ *
44
+ * to its RGB/A representation as an
45
+ *
46
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .
47
+ *
48
+ * If Alpha channel is present in the original object it will be present also in the output.
49
+ */
50
+ declare function hsvToRgb({ h, s, v, a }: HsvaColor): RgbaColor;
51
+ /**
52
+ * Converts a RGB/A color
53
+ *
54
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
55
+ *
56
+ * to its HSV/A representation as an
57
+ *
58
+ * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-1]}`).
59
+ *
60
+ * If Alpha channel is present in the original object it will be present also in the output.
61
+ */
62
+ declare function rgbToHsv({ r, g, b, a }: RgbaColor): HsvaColor;
63
+ /**
64
+ * Converts a HEX/A color
65
+ *
66
+ * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
67
+ *
68
+ * to its RGB/A representation as an
69
+ *
70
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
71
+ *
72
+ * If Alpha channel is present in the original object it will be present also in the output.
73
+ */
74
+ declare function textToRgb(str: AnyColor): RgbaColor;
75
+ /**
76
+ * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).
77
+ *
78
+ * Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 1 or -1 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.
79
+ */
80
+ declare function lighten(color: AnyColor, percent: number): string;
81
+ /**
82
+ * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.
83
+ *
84
+ * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
85
+ */
86
+ declare function luminance(color: AnyColor): number;
87
+ /**
88
+ * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.
89
+ *
90
+ * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
91
+ */
92
+ declare function brightness(color: AnyColor): number;
93
+ /**
94
+ * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.
95
+ *
96
+ * Accepts a HEX/A String or a RGB/A Object as `fgColor`/`bgColor`. If the alpha channel of the `fgColor` is completely opaque, then the result will be the `fgColor`. If the alpha channel of the `bgColor` is completely opaque, then the resulting blended color will also be opaque. Returns the same type as input for fgColor.
97
+ */
98
+ declare function blend(fgColor: AnyColor, bgColor: AnyColor): colord.Colord;
99
+ /**
100
+ * Change color transparency
101
+ */
102
+ declare function changeAlpha(color: COLOR, alpha: number): string;
103
+ /**
104
+ * Obtain palette colors based on color (from left to right, from light to dark, with 6 as the main color number)
105
+ * @param color - Color
106
+ * @param index - The corresponding color number of the color palette (6 as the main color number)
107
+ * @description algorithm implementation draws inspiration from ant design palette algorithm https://github.com/ant-design/ant-design/blob/master/components/style/color/colorPalette.less
108
+ */
109
109
  declare function colorPalette(color: string | RgbaColor, index: PALETTE_INDEXES): string;
110
110
 
111
- export { COLOR, HEX_TEXT, HSVA, HSVA_TEXT, HexColor, PALETTE_INDEXES, RGB, RGBA, RGBA_TEXT, blend, brightness, changeAlpha, colorPalette, hexToRgb, hsvToRgb, lighten, luminance, rgbToHex, rgbToHsv, textToRgb };
111
+ export { type COLOR, type HEX_TEXT, type HSVA, type HSVA_TEXT, type HexColor, type PALETTE_INDEXES, type RGB, type RGBA, type RGBA_TEXT, blend, brightness, changeAlpha, colorPalette, hexToRgb, hsvToRgb, lighten, luminance, rgbToHex, rgbToHsv, textToRgb };
@@ -0,0 +1,338 @@
1
+ "use strict";
2
+ (() => {
3
+ // ../../node_modules/.pnpm/colord@2.9.3/node_modules/colord/index.mjs
4
+ var r = { grad: 0.9, turn: 360, rad: 360 / (2 * Math.PI) };
5
+ var t = function(r3) {
6
+ return "string" == typeof r3 ? r3.length > 0 : "number" == typeof r3;
7
+ };
8
+ var n = function(r3, t4, n3) {
9
+ return void 0 === t4 && (t4 = 0), void 0 === n3 && (n3 = Math.pow(10, t4)), Math.round(n3 * r3) / n3 + 0;
10
+ };
11
+ var e = function(r3, t4, n3) {
12
+ return void 0 === t4 && (t4 = 0), void 0 === n3 && (n3 = 1), r3 > n3 ? n3 : r3 > t4 ? r3 : t4;
13
+ };
14
+ var u = function(r3) {
15
+ return (r3 = isFinite(r3) ? r3 % 360 : 0) > 0 ? r3 : r3 + 360;
16
+ };
17
+ var a = function(r3) {
18
+ return { r: e(r3.r, 0, 255), g: e(r3.g, 0, 255), b: e(r3.b, 0, 255), a: e(r3.a) };
19
+ };
20
+ var o = function(r3) {
21
+ return { r: n(r3.r), g: n(r3.g), b: n(r3.b), a: n(r3.a, 3) };
22
+ };
23
+ var i = /^#([0-9a-f]{3,8})$/i;
24
+ var s = function(r3) {
25
+ var t4 = r3.toString(16);
26
+ return t4.length < 2 ? "0" + t4 : t4;
27
+ };
28
+ var h = function(r3) {
29
+ var t4 = r3.r, n3 = r3.g, e3 = r3.b, u3 = r3.a, a3 = Math.max(t4, n3, e3), o4 = a3 - Math.min(t4, n3, e3), i3 = o4 ? a3 === t4 ? (n3 - e3) / o4 : a3 === n3 ? 2 + (e3 - t4) / o4 : 4 + (t4 - n3) / o4 : 0;
30
+ return { h: 60 * (i3 < 0 ? i3 + 6 : i3), s: a3 ? o4 / a3 * 100 : 0, v: a3 / 255 * 100, a: u3 };
31
+ };
32
+ var b = function(r3) {
33
+ var t4 = r3.h, n3 = r3.s, e3 = r3.v, u3 = r3.a;
34
+ t4 = t4 / 360 * 6, n3 /= 100, e3 /= 100;
35
+ var a3 = Math.floor(t4), o4 = e3 * (1 - n3), i3 = e3 * (1 - (t4 - a3) * n3), s2 = e3 * (1 - (1 - t4 + a3) * n3), h3 = a3 % 6;
36
+ return { r: 255 * [e3, i3, o4, o4, s2, e3][h3], g: 255 * [s2, e3, e3, i3, o4, o4][h3], b: 255 * [o4, o4, s2, e3, e3, i3][h3], a: u3 };
37
+ };
38
+ var g = function(r3) {
39
+ return { h: u(r3.h), s: e(r3.s, 0, 100), l: e(r3.l, 0, 100), a: e(r3.a) };
40
+ };
41
+ var d = function(r3) {
42
+ return { h: n(r3.h), s: n(r3.s), l: n(r3.l), a: n(r3.a, 3) };
43
+ };
44
+ var f = function(r3) {
45
+ return b((n3 = (t4 = r3).s, { h: t4.h, s: (n3 *= ((e3 = t4.l) < 50 ? e3 : 100 - e3) / 100) > 0 ? 2 * n3 / (e3 + n3) * 100 : 0, v: e3 + n3, a: t4.a }));
46
+ var t4, n3, e3;
47
+ };
48
+ var c = function(r3) {
49
+ return { h: (t4 = h(r3)).h, s: (u3 = (200 - (n3 = t4.s)) * (e3 = t4.v) / 100) > 0 && u3 < 200 ? n3 * e3 / 100 / (u3 <= 100 ? u3 : 200 - u3) * 100 : 0, l: u3 / 2, a: t4.a };
50
+ var t4, n3, e3, u3;
51
+ };
52
+ var l = /^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i;
53
+ var p = /^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i;
54
+ var v = /^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i;
55
+ var m = /^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i;
56
+ var y = { string: [[function(r3) {
57
+ var t4 = i.exec(r3);
58
+ return t4 ? (r3 = t4[1]).length <= 4 ? { r: parseInt(r3[0] + r3[0], 16), g: parseInt(r3[1] + r3[1], 16), b: parseInt(r3[2] + r3[2], 16), a: 4 === r3.length ? n(parseInt(r3[3] + r3[3], 16) / 255, 2) : 1 } : 6 === r3.length || 8 === r3.length ? { r: parseInt(r3.substr(0, 2), 16), g: parseInt(r3.substr(2, 2), 16), b: parseInt(r3.substr(4, 2), 16), a: 8 === r3.length ? n(parseInt(r3.substr(6, 2), 16) / 255, 2) : 1 } : null : null;
59
+ }, "hex"], [function(r3) {
60
+ var t4 = v.exec(r3) || m.exec(r3);
61
+ return t4 ? t4[2] !== t4[4] || t4[4] !== t4[6] ? null : a({ r: Number(t4[1]) / (t4[2] ? 100 / 255 : 1), g: Number(t4[3]) / (t4[4] ? 100 / 255 : 1), b: Number(t4[5]) / (t4[6] ? 100 / 255 : 1), a: void 0 === t4[7] ? 1 : Number(t4[7]) / (t4[8] ? 100 : 1) }) : null;
62
+ }, "rgb"], [function(t4) {
63
+ var n3 = l.exec(t4) || p.exec(t4);
64
+ if (!n3) return null;
65
+ var e3, u3, a3 = g({ h: (e3 = n3[1], u3 = n3[2], void 0 === u3 && (u3 = "deg"), Number(e3) * (r[u3] || 1)), s: Number(n3[3]), l: Number(n3[4]), a: void 0 === n3[5] ? 1 : Number(n3[5]) / (n3[6] ? 100 : 1) });
66
+ return f(a3);
67
+ }, "hsl"]], object: [[function(r3) {
68
+ var n3 = r3.r, e3 = r3.g, u3 = r3.b, o4 = r3.a, i3 = void 0 === o4 ? 1 : o4;
69
+ return t(n3) && t(e3) && t(u3) ? a({ r: Number(n3), g: Number(e3), b: Number(u3), a: Number(i3) }) : null;
70
+ }, "rgb"], [function(r3) {
71
+ var n3 = r3.h, e3 = r3.s, u3 = r3.l, a3 = r3.a, o4 = void 0 === a3 ? 1 : a3;
72
+ if (!t(n3) || !t(e3) || !t(u3)) return null;
73
+ var i3 = g({ h: Number(n3), s: Number(e3), l: Number(u3), a: Number(o4) });
74
+ return f(i3);
75
+ }, "hsl"], [function(r3) {
76
+ var n3 = r3.h, a3 = r3.s, o4 = r3.v, i3 = r3.a, s2 = void 0 === i3 ? 1 : i3;
77
+ if (!t(n3) || !t(a3) || !t(o4)) return null;
78
+ var h3 = function(r4) {
79
+ return { h: u(r4.h), s: e(r4.s, 0, 100), v: e(r4.v, 0, 100), a: e(r4.a) };
80
+ }({ h: Number(n3), s: Number(a3), v: Number(o4), a: Number(s2) });
81
+ return b(h3);
82
+ }, "hsv"]] };
83
+ var N = function(r3, t4) {
84
+ for (var n3 = 0; n3 < t4.length; n3++) {
85
+ var e3 = t4[n3][0](r3);
86
+ if (e3) return [e3, t4[n3][1]];
87
+ }
88
+ return [null, void 0];
89
+ };
90
+ var x = function(r3) {
91
+ return "string" == typeof r3 ? N(r3.trim(), y.string) : "object" == typeof r3 && null !== r3 ? N(r3, y.object) : [null, void 0];
92
+ };
93
+ var M = function(r3, t4) {
94
+ var n3 = c(r3);
95
+ return { h: n3.h, s: e(n3.s + 100 * t4, 0, 100), l: n3.l, a: n3.a };
96
+ };
97
+ var H = function(r3) {
98
+ return (299 * r3.r + 587 * r3.g + 114 * r3.b) / 1e3 / 255;
99
+ };
100
+ var $ = function(r3, t4) {
101
+ var n3 = c(r3);
102
+ return { h: n3.h, s: n3.s, l: e(n3.l + 100 * t4, 0, 100), a: n3.a };
103
+ };
104
+ var j = function() {
105
+ function r3(r4) {
106
+ this.parsed = x(r4)[0], this.rgba = this.parsed || { r: 0, g: 0, b: 0, a: 1 };
107
+ }
108
+ return r3.prototype.isValid = function() {
109
+ return null !== this.parsed;
110
+ }, r3.prototype.brightness = function() {
111
+ return n(H(this.rgba), 2);
112
+ }, r3.prototype.isDark = function() {
113
+ return H(this.rgba) < 0.5;
114
+ }, r3.prototype.isLight = function() {
115
+ return H(this.rgba) >= 0.5;
116
+ }, r3.prototype.toHex = function() {
117
+ return r4 = o(this.rgba), t4 = r4.r, e3 = r4.g, u3 = r4.b, i3 = (a3 = r4.a) < 1 ? s(n(255 * a3)) : "", "#" + s(t4) + s(e3) + s(u3) + i3;
118
+ var r4, t4, e3, u3, a3, i3;
119
+ }, r3.prototype.toRgb = function() {
120
+ return o(this.rgba);
121
+ }, r3.prototype.toRgbString = function() {
122
+ return r4 = o(this.rgba), t4 = r4.r, n3 = r4.g, e3 = r4.b, (u3 = r4.a) < 1 ? "rgba(" + t4 + ", " + n3 + ", " + e3 + ", " + u3 + ")" : "rgb(" + t4 + ", " + n3 + ", " + e3 + ")";
123
+ var r4, t4, n3, e3, u3;
124
+ }, r3.prototype.toHsl = function() {
125
+ return d(c(this.rgba));
126
+ }, r3.prototype.toHslString = function() {
127
+ return r4 = d(c(this.rgba)), t4 = r4.h, n3 = r4.s, e3 = r4.l, (u3 = r4.a) < 1 ? "hsla(" + t4 + ", " + n3 + "%, " + e3 + "%, " + u3 + ")" : "hsl(" + t4 + ", " + n3 + "%, " + e3 + "%)";
128
+ var r4, t4, n3, e3, u3;
129
+ }, r3.prototype.toHsv = function() {
130
+ return r4 = h(this.rgba), { h: n(r4.h), s: n(r4.s), v: n(r4.v), a: n(r4.a, 3) };
131
+ var r4;
132
+ }, r3.prototype.invert = function() {
133
+ return w({ r: 255 - (r4 = this.rgba).r, g: 255 - r4.g, b: 255 - r4.b, a: r4.a });
134
+ var r4;
135
+ }, r3.prototype.saturate = function(r4) {
136
+ return void 0 === r4 && (r4 = 0.1), w(M(this.rgba, r4));
137
+ }, r3.prototype.desaturate = function(r4) {
138
+ return void 0 === r4 && (r4 = 0.1), w(M(this.rgba, -r4));
139
+ }, r3.prototype.grayscale = function() {
140
+ return w(M(this.rgba, -1));
141
+ }, r3.prototype.lighten = function(r4) {
142
+ return void 0 === r4 && (r4 = 0.1), w($(this.rgba, r4));
143
+ }, r3.prototype.darken = function(r4) {
144
+ return void 0 === r4 && (r4 = 0.1), w($(this.rgba, -r4));
145
+ }, r3.prototype.rotate = function(r4) {
146
+ return void 0 === r4 && (r4 = 15), this.hue(this.hue() + r4);
147
+ }, r3.prototype.alpha = function(r4) {
148
+ return "number" == typeof r4 ? w({ r: (t4 = this.rgba).r, g: t4.g, b: t4.b, a: r4 }) : n(this.rgba.a, 3);
149
+ var t4;
150
+ }, r3.prototype.hue = function(r4) {
151
+ var t4 = c(this.rgba);
152
+ return "number" == typeof r4 ? w({ h: r4, s: t4.s, l: t4.l, a: t4.a }) : n(t4.h);
153
+ }, r3.prototype.isEqual = function(r4) {
154
+ return this.toHex() === w(r4).toHex();
155
+ }, r3;
156
+ }();
157
+ var w = function(r3) {
158
+ return r3 instanceof j ? r3 : new j(r3);
159
+ };
160
+ var S = [];
161
+ var k = function(r3) {
162
+ r3.forEach(function(r4) {
163
+ S.indexOf(r4) < 0 && (r4(j, y), S.push(r4));
164
+ });
165
+ };
166
+
167
+ // ../../node_modules/.pnpm/colord@2.9.3/node_modules/colord/plugins/a11y.mjs
168
+ var o2 = function(o4) {
169
+ var t4 = o4 / 255;
170
+ return t4 < 0.04045 ? t4 / 12.92 : Math.pow((t4 + 0.055) / 1.055, 2.4);
171
+ };
172
+ var t2 = function(t4) {
173
+ return 0.2126 * o2(t4.r) + 0.7152 * o2(t4.g) + 0.0722 * o2(t4.b);
174
+ };
175
+ function a11y_default(o4) {
176
+ o4.prototype.luminance = function() {
177
+ return o5 = t2(this.rgba), void 0 === (r3 = 2) && (r3 = 0), void 0 === n3 && (n3 = Math.pow(10, r3)), Math.round(n3 * o5) / n3 + 0;
178
+ var o5, r3, n3;
179
+ }, o4.prototype.contrast = function(r3) {
180
+ void 0 === r3 && (r3 = "#FFF");
181
+ var n3, a3, i3, e3, v2, u3, d2, c3 = r3 instanceof o4 ? r3 : new o4(r3);
182
+ return e3 = this.rgba, v2 = c3.toRgb(), u3 = t2(e3), d2 = t2(v2), n3 = u3 > d2 ? (u3 + 0.05) / (d2 + 0.05) : (d2 + 0.05) / (u3 + 0.05), void 0 === (a3 = 2) && (a3 = 0), void 0 === i3 && (i3 = Math.pow(10, a3)), Math.floor(i3 * n3) / i3 + 0;
183
+ }, o4.prototype.isReadable = function(o5, t4) {
184
+ return void 0 === o5 && (o5 = "#FFF"), void 0 === t4 && (t4 = {}), this.contrast(o5) >= (e3 = void 0 === (i3 = (r3 = t4).size) ? "normal" : i3, "AAA" === (a3 = void 0 === (n3 = r3.level) ? "AA" : n3) && "normal" === e3 ? 7 : "AA" === a3 && "large" === e3 ? 3 : 4.5);
185
+ var r3, n3, a3, i3, e3;
186
+ };
187
+ }
188
+
189
+ // ../../node_modules/.pnpm/colord@2.9.3/node_modules/colord/plugins/mix.mjs
190
+ var t3 = function(t4, a3, n3) {
191
+ return void 0 === a3 && (a3 = 0), void 0 === n3 && (n3 = 1), t4 > n3 ? n3 : t4 > a3 ? t4 : a3;
192
+ };
193
+ var a2 = function(t4) {
194
+ var a3 = t4 / 255;
195
+ return a3 < 0.04045 ? a3 / 12.92 : Math.pow((a3 + 0.055) / 1.055, 2.4);
196
+ };
197
+ var n2 = function(t4) {
198
+ return 255 * (t4 > 31308e-7 ? 1.055 * Math.pow(t4, 1 / 2.4) - 0.055 : 12.92 * t4);
199
+ };
200
+ var r2 = 96.422;
201
+ var o3 = 100;
202
+ var u2 = 82.521;
203
+ var e2 = function(a3) {
204
+ var r3, o4, u3 = { x: 0.9555766 * (r3 = a3).x + -0.0230393 * r3.y + 0.0631636 * r3.z, y: -0.0282895 * r3.x + 1.0099416 * r3.y + 0.0210077 * r3.z, z: 0.0122982 * r3.x + -0.020483 * r3.y + 1.3299098 * r3.z };
205
+ return o4 = { r: n2(0.032404542 * u3.x - 0.015371385 * u3.y - 4985314e-9 * u3.z), g: n2(-969266e-8 * u3.x + 0.018760108 * u3.y + 41556e-8 * u3.z), b: n2(556434e-9 * u3.x - 2040259e-9 * u3.y + 0.010572252 * u3.z), a: a3.a }, { r: t3(o4.r, 0, 255), g: t3(o4.g, 0, 255), b: t3(o4.b, 0, 255), a: t3(o4.a) };
206
+ };
207
+ var i2 = function(n3) {
208
+ var e3 = a2(n3.r), i3 = a2(n3.g), p3 = a2(n3.b);
209
+ return function(a3) {
210
+ return { x: t3(a3.x, 0, r2), y: t3(a3.y, 0, o3), z: t3(a3.z, 0, u2), a: t3(a3.a) };
211
+ }(function(t4) {
212
+ return { x: 1.0478112 * t4.x + 0.0228866 * t4.y + -0.050127 * t4.z, y: 0.0295424 * t4.x + 0.9904844 * t4.y + -0.0170491 * t4.z, z: -92345e-7 * t4.x + 0.0150436 * t4.y + 0.7521316 * t4.z, a: t4.a };
213
+ }({ x: 100 * (0.4124564 * e3 + 0.3575761 * i3 + 0.1804375 * p3), y: 100 * (0.2126729 * e3 + 0.7151522 * i3 + 0.072175 * p3), z: 100 * (0.0193339 * e3 + 0.119192 * i3 + 0.9503041 * p3), a: n3.a }));
214
+ };
215
+ var p2 = 216 / 24389;
216
+ var h2 = 24389 / 27;
217
+ var f2 = function(t4) {
218
+ var a3 = i2(t4), n3 = a3.x / r2, e3 = a3.y / o3, f3 = a3.z / u2;
219
+ return n3 = n3 > p2 ? Math.cbrt(n3) : (h2 * n3 + 16) / 116, { l: 116 * (e3 = e3 > p2 ? Math.cbrt(e3) : (h2 * e3 + 16) / 116) - 16, a: 500 * (n3 - e3), b: 200 * (e3 - (f3 = f3 > p2 ? Math.cbrt(f3) : (h2 * f3 + 16) / 116)), alpha: a3.a };
220
+ };
221
+ var c2 = function(a3, n3, i3) {
222
+ var c3, y2 = f2(a3), x2 = f2(n3);
223
+ return function(t4) {
224
+ var a4 = (t4.l + 16) / 116, n4 = t4.a / 500 + a4, i4 = a4 - t4.b / 200;
225
+ return e2({ x: (Math.pow(n4, 3) > p2 ? Math.pow(n4, 3) : (116 * n4 - 16) / h2) * r2, y: (t4.l > 8 ? Math.pow((t4.l + 16) / 116, 3) : t4.l / h2) * o3, z: (Math.pow(i4, 3) > p2 ? Math.pow(i4, 3) : (116 * i4 - 16) / h2) * u2, a: t4.alpha });
226
+ }({ l: t3((c3 = { l: y2.l * (1 - i3) + x2.l * i3, a: y2.a * (1 - i3) + x2.a * i3, b: y2.b * (1 - i3) + x2.b * i3, alpha: y2.alpha * (1 - i3) + x2.alpha * i3 }).l, 0, 400), a: c3.a, b: c3.b, alpha: t3(c3.alpha) });
227
+ };
228
+ function mix_default(t4) {
229
+ function a3(t5, a4, n3) {
230
+ void 0 === n3 && (n3 = 5);
231
+ for (var r3 = [], o4 = 1 / (n3 - 1), u3 = 0; u3 <= n3 - 1; u3++) r3.push(t5.mix(a4, o4 * u3));
232
+ return r3;
233
+ }
234
+ t4.prototype.mix = function(a4, n3) {
235
+ void 0 === n3 && (n3 = 0.5);
236
+ var r3 = a4 instanceof t4 ? a4 : new t4(a4), o4 = c2(this.toRgb(), r3.toRgb(), n3);
237
+ return new t4(o4);
238
+ }, t4.prototype.tints = function(t5) {
239
+ return a3(this, "#fff", t5);
240
+ }, t4.prototype.shades = function(t5) {
241
+ return a3(this, "#000", t5);
242
+ }, t4.prototype.tones = function(t5) {
243
+ return a3(this, "#808080", t5);
244
+ };
245
+ }
246
+
247
+ // src/index.ts
248
+ k([a11y_default, mix_default]);
249
+ function rgbToHex({ r: r3, g: g2, b: b2, a: a3 }) {
250
+ return w({ r: r3, g: g2, b: b2, a: a3 }).toHex();
251
+ }
252
+ function hexToRgb(hex) {
253
+ return w(hex).toRgb();
254
+ }
255
+ function hsvToRgb({ h: h3, s: s2, v: v2, a: a3 }) {
256
+ return w({ h: h3, s: s2, v: v2, a: a3 }).toRgb();
257
+ }
258
+ function rgbToHsv({ r: r3, g: g2, b: b2, a: a3 }) {
259
+ return w({ r: r3, b: b2, g: g2, a: a3 }).toHsv();
260
+ }
261
+ function textToRgb(str) {
262
+ return w(str).toRgb();
263
+ }
264
+ function lighten(color, percent) {
265
+ return w(color).lighten(percent).toHex();
266
+ }
267
+ function luminance(color) {
268
+ return w(color).luminance();
269
+ }
270
+ function brightness(color) {
271
+ return w(color).brightness();
272
+ }
273
+ function blend(fgColor, bgColor) {
274
+ return w(fgColor).mix(bgColor);
275
+ }
276
+ function changeAlpha(color, alpha) {
277
+ return w(color).alpha(alpha).toHex();
278
+ }
279
+ var hueStep = 2;
280
+ var saturationStep = 16;
281
+ var saturationStep2 = 5;
282
+ var brightnessStep1 = 5;
283
+ var brightnessStep2 = 15;
284
+ var lightColorCount = 5;
285
+ var darkColorCount = 4;
286
+ function colorPalette(color, index) {
287
+ if (typeof color !== "string" && (!color || color.r === void 0))
288
+ throw new TypeError("Expected a string or a {r, g, b} object as color");
289
+ const rgb = typeof color === "string" ? textToRgb(color) : color;
290
+ const oldHsv = w(rgb).toHsv();
291
+ if (index === 6)
292
+ return rgbToHex(rgb);
293
+ const light = index < 6;
294
+ const i3 = light ? lightColorCount + 1 - index : index - lightColorCount - 1;
295
+ const newHsv = {
296
+ h: hue(oldHsv, i3, light),
297
+ s: saturation(oldHsv, i3, light),
298
+ v: value(oldHsv, i3, light),
299
+ a: oldHsv.a
300
+ };
301
+ return w(newHsv).toHex();
302
+ }
303
+ function hue(hsv, i3, isLight) {
304
+ let hue2;
305
+ if (hsv.h >= 60 && hsv.h <= 240) {
306
+ hue2 = isLight ? hsv.h - hueStep * i3 : hsv.h + hueStep * i3;
307
+ } else {
308
+ hue2 = isLight ? hsv.h + hueStep * i3 : hsv.h - hueStep * i3;
309
+ }
310
+ if (hue2 < 0)
311
+ hue2 += 360;
312
+ else if (hue2 >= 360)
313
+ hue2 -= 360;
314
+ return hue2;
315
+ }
316
+ function saturation(hsv, i3, isLight) {
317
+ let saturation2;
318
+ if (isLight)
319
+ saturation2 = hsv.s - saturationStep * i3;
320
+ else if (i3 === darkColorCount)
321
+ saturation2 = hsv.s + saturationStep;
322
+ else saturation2 = hsv.s + saturationStep2 * i3;
323
+ if (saturation2 > 100)
324
+ saturation2 = 100;
325
+ if (isLight && i3 === lightColorCount && saturation2 > 10)
326
+ saturation2 = 10;
327
+ if (saturation2 < 6)
328
+ saturation2 = 6;
329
+ return saturation2;
330
+ }
331
+ function value(hsv, i3, isLight) {
332
+ let value2;
333
+ value2 = isLight ? hsv.v + brightnessStep1 * i3 : hsv.v - brightnessStep2 * i3;
334
+ if (value2 > 100)
335
+ value2 = 100;
336
+ return value2;
337
+ }
338
+ })();
@@ -76,8 +76,7 @@ function saturation(hsv, i, isLight) {
76
76
  saturation2 = hsv.s - saturationStep * i;
77
77
  else if (i === darkColorCount)
78
78
  saturation2 = hsv.s + saturationStep;
79
- else
80
- saturation2 = hsv.s + saturationStep2 * i;
79
+ else saturation2 = hsv.s + saturationStep2 * i;
81
80
  if (saturation2 > 100)
82
81
  saturation2 = 100;
83
82
  if (isLight && i === lightColorCount && saturation2 > 10)
package/package.json CHANGED
@@ -1,10 +1,22 @@
1
1
  {
2
2
  "name": "@hairy/palette",
3
- "version": "0.3.12",
3
+ "type": "module",
4
+ "version": "1.5.0",
5
+ "description": "Library for color palette",
6
+ "author": "Hairyf <wwu710632@gmail.com>",
4
7
  "license": "MIT",
5
- "main": "./dist/index.cjs",
8
+ "funding": "https://github.com/sponsors/hairyf",
9
+ "homepage": "https://github.com/hairyf/hairylib#readme",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/hairyf/hairylib.git"
13
+ },
14
+ "bugs": "https://github.com/hairyf/hairylib/issues",
15
+ "keywords": [],
16
+ "sideEffects": false,
17
+ "main": "./dist/index.js",
6
18
  "publishConfig": {
7
- "jsdelivr": "./dist/index.iife.min.js"
19
+ "jsdelivr": "./dist/index.global.js"
8
20
  },
9
21
  "files": [
10
22
  "dist"
@@ -13,14 +25,16 @@
13
25
  "colord": "^2.9.3"
14
26
  },
15
27
  "scripts": {
16
- "build": "ptsup src/index.ts --dts"
28
+ "build": "tsup",
29
+ "dev": "tsup --watch",
30
+ "start": "tsx src/index.ts"
17
31
  },
18
- "module": "./dist/index.mjs",
32
+ "module": "./dist/index.js",
19
33
  "types": "./dist/index.d.ts",
20
- "unpkg": "./dist/index.iife.min.js",
34
+ "unpkg": "./dist/index.global.js",
21
35
  "exports": {
22
36
  ".": {
23
- "import": "./dist/index.mjs",
37
+ "import": "./dist/index.js",
24
38
  "require": "./dist/index.cjs",
25
39
  "types": "./dist/index.d.ts"
26
40
  },