@hairy/palette 0.3.2 → 0.3.3

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/index.cjs.js CHANGED
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,205 +16,58 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
17
20
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
21
 
19
22
  // index.ts
20
23
  var util_color_exports = {};
21
24
  __export(util_color_exports, {
22
25
  blend: () => blend,
23
- blendAlpha: () => blendAlpha,
24
26
  brightness: () => brightness,
25
27
  changeAlpha: () => changeAlpha,
26
28
  colorPalette: () => colorPalette,
27
29
  hexToRgb: () => hexToRgb,
28
30
  hsvToRgb: () => hsvToRgb,
29
31
  lighten: () => lighten,
30
- luminosity: () => luminosity,
32
+ luminance: () => luminance,
31
33
  rgbToHex: () => rgbToHex,
32
34
  rgbToHsv: () => rgbToHsv,
33
35
  textToRgb: () => textToRgb
34
36
  });
35
37
  module.exports = __toCommonJS(util_color_exports);
36
- var RE_RGBA = /^rgb(a)?\((\d{1,3}),(\d{1,3}),(\d{1,3}),?([01]?\.?\d*?)?\)$/;
38
+ var import_colord = require("colord");
39
+ var import_a11y = __toESM(require("colord/plugins/a11y"));
40
+ var import_mix = __toESM(require("colord/plugins/mix"));
41
+ (0, import_colord.extend)([import_a11y.default, import_mix.default]);
37
42
  function rgbToHex({ r, g, b, a }) {
38
- const alpha = a !== void 0;
39
- r = Math.round(r);
40
- g = Math.round(g);
41
- b = Math.round(b);
42
- if (r > 255 || g > 255 || b > 255 || alpha && a > 100) {
43
- throw new TypeError("Expected 3 numbers below 256 (and optionally one below 100)");
44
- }
45
- if (alpha) {
46
- a = Number((Math.round(255 * a / 100) | 1 << 8).toString(16).slice(1));
47
- }
48
- return "#" + (b | g << 8 | r << 16 | 1 << 24).toString(16).slice(1) + a;
43
+ return (0, import_colord.colord)({ r, g, b, a }).toHex();
49
44
  }
50
45
  function hexToRgb(hex) {
51
- if (typeof hex !== "string") {
52
- throw new TypeError("Expected a string");
53
- }
54
- hex = hex.replace(/^#/, "");
55
- if (hex.length === 3) {
56
- hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
57
- } else if (hex.length === 4) {
58
- hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
59
- }
60
- const num = parseInt(hex, 16);
61
- return hex.length > 6 ? { r: num >> 24 & 255, g: num >> 16 & 255, b: num >> 8 & 255, a: Math.round((num & 255) / 2.55) } : { r: num >> 16, g: num >> 8 & 255, b: num & 255 };
46
+ return (0, import_colord.colord)(hex).toRgb();
62
47
  }
63
48
  function hsvToRgb({ h, s, v, a }) {
64
- let r, g, b;
65
- s = s / 100;
66
- v = v / 100;
67
- h = h / 360;
68
- const i = Math.floor(h * 6), f = h * 6 - i, p = v * (1 - s), q = v * (1 - f * s), t = v * (1 - (1 - f) * s);
69
- switch (i % 6) {
70
- case 0:
71
- r = v;
72
- g = t;
73
- b = p;
74
- break;
75
- case 1:
76
- r = q;
77
- g = v;
78
- b = p;
79
- break;
80
- case 2:
81
- r = p;
82
- g = v;
83
- b = t;
84
- break;
85
- case 3:
86
- r = p;
87
- g = q;
88
- b = v;
89
- break;
90
- case 4:
91
- r = t;
92
- g = p;
93
- b = v;
94
- break;
95
- case 5:
96
- r = v;
97
- g = p;
98
- b = q;
99
- break;
100
- }
101
- return {
102
- r: Math.round(r * 255),
103
- g: Math.round(g * 255),
104
- b: Math.round(b * 255),
105
- a
106
- };
49
+ return (0, import_colord.colord)({ h, s, v, a }).toRgb();
107
50
  }
108
51
  function rgbToHsv({ r, g, b, a }) {
109
- const max = Math.max(r, g, b), min = Math.min(r, g, b), d = max - min, s = max === 0 ? 0 : d / max, v = max / 255;
110
- let h;
111
- switch (max) {
112
- case min:
113
- h = 0;
114
- break;
115
- case r:
116
- h = g - b + d * (g < b ? 6 : 0);
117
- h /= 6 * d;
118
- break;
119
- case g:
120
- h = b - r + d * 2;
121
- h /= 6 * d;
122
- break;
123
- case b:
124
- h = r - g + d * 4;
125
- h /= 6 * d;
126
- break;
127
- }
128
- return {
129
- h: Math.round(h * 360),
130
- s: Math.round(s * 100),
131
- v: Math.round(v * 100),
132
- a: a || 1
133
- };
52
+ return (0, import_colord.colord)({ r, b, g, a }).toHsv();
134
53
  }
135
54
  function textToRgb(str) {
136
- if (typeof str !== "string") {
137
- throw new TypeError("Expected a string");
138
- }
139
- const color = str.replace(/ /g, "");
140
- const m = RE_RGBA.exec(color);
141
- if (m === null) {
142
- return hexToRgb(color);
143
- }
144
- const rgb = {
145
- r: Math.min(255, parseInt(m[2], 10)),
146
- g: Math.min(255, parseInt(m[3], 10)),
147
- b: Math.min(255, parseInt(m[4], 10))
148
- };
149
- if (m[1]) {
150
- const alpha = parseFloat(m[5]);
151
- rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100;
152
- }
153
- return rgb;
55
+ return (0, import_colord.colord)(str).toRgb();
154
56
  }
155
57
  function lighten(color, percent) {
156
- if (typeof color !== "string") {
157
- throw new TypeError("Expected a string as color");
158
- }
159
- if (typeof percent !== "number") {
160
- throw new TypeError("Expected a numeric percent");
161
- }
162
- const rgb = textToRgb(color), t = percent < 0 ? 0 : 255, p = Math.abs(percent) / 100, R = rgb.r, G = rgb.g, B = rgb.b;
163
- return "#" + (16777216 + (Math.round((t - R) * p) + R) * 65536 + (Math.round((t - G) * p) + G) * 256 + (Math.round((t - B) * p) + B)).toString(16).slice(1);
58
+ return (0, import_colord.colord)(color).lighten(percent).toHex();
164
59
  }
165
- function luminosity(color) {
166
- if (typeof color !== "string" && (!color || color.r === void 0)) {
167
- throw new TypeError("Expected a string or a {r, g, b} object as color");
168
- }
169
- const rgb = typeof color === "string" ? textToRgb(color) : color, r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255, R = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4), G = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4), B = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
170
- return 0.2126 * R + 0.7152 * G + 0.0722 * B;
60
+ function luminance(color) {
61
+ return (0, import_colord.colord)(color).luminance();
171
62
  }
172
63
  function brightness(color) {
173
- if (typeof color !== "string" && (!color || color.r === void 0)) {
174
- throw new TypeError("Expected a string or a {r, g, b} object as color");
175
- }
176
- const rgb = typeof color === "string" ? textToRgb(color) : color;
177
- return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1e3;
64
+ return (0, import_colord.colord)(color).brightness();
178
65
  }
179
66
  function blend(fgColor, bgColor) {
180
- if (typeof fgColor !== "string" && (!fgColor || fgColor.r === void 0)) {
181
- throw new TypeError("Expected a string or a {r, g, b[, a]} object as fgColor");
182
- }
183
- if (typeof bgColor !== "string" && (!bgColor || bgColor.r === void 0)) {
184
- throw new TypeError("Expected a string or a {r, g, b[, a]} object as bgColor");
185
- }
186
- const rgb1 = typeof fgColor === "string" ? textToRgb(fgColor) : fgColor, r1 = rgb1.r / 255, g1 = rgb1.g / 255, b1 = rgb1.b / 255, a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1, rgb2 = typeof bgColor === "string" ? textToRgb(bgColor) : bgColor, r2 = rgb2.r / 255, g2 = rgb2.g / 255, b2 = rgb2.b / 255, a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1, a = a1 + a2 * (1 - a1), r = Math.round((r1 * a1 + r2 * a2 * (1 - a1)) / a * 255), g = Math.round((g1 * a1 + g2 * a2 * (1 - a1)) / a * 255), b = Math.round((b1 * a1 + b2 * a2 * (1 - a1)) / a * 255);
187
- const ret = { r, g, b, a: Math.round(a * 100) };
188
- return typeof fgColor === "string" ? rgbToHex(ret) : ret;
189
- }
190
- function blendAlpha(color, offset) {
191
- if (typeof color !== "string") {
192
- throw new TypeError("Expected a string as color");
193
- }
194
- if (offset === void 0 || offset < -1 || offset > 1) {
195
- throw new TypeError("Expected offset to be between -1 and 1");
196
- }
197
- const { r, g, b, a } = textToRgb(color);
198
- const alpha = a !== void 0 ? a / 100 : 0;
199
- return rgbToHex({
200
- r,
201
- g,
202
- b,
203
- a: Math.round(Math.min(1, Math.max(0, alpha + offset)) * 100)
204
- });
67
+ return (0, import_colord.colord)(fgColor).mix(bgColor);
205
68
  }
206
69
  function changeAlpha(color, alpha) {
207
- if (typeof color !== "string") {
208
- throw new TypeError("Expected a string as color");
209
- }
210
- if (alpha === void 0 || alpha < 0 || alpha > 100) {
211
- throw new TypeError("Expected alpha to be between 1 and 100");
212
- }
213
- const rgba = textToRgb(color);
214
- rgba.a = alpha;
215
- return rgbToHex(rgba);
70
+ return (0, import_colord.colord)(color).alpha(alpha).toHex();
216
71
  }
217
72
  var hueStep = 2;
218
73
  var saturationStep = 16;
@@ -226,7 +81,7 @@ function colorPalette(color, index) {
226
81
  throw new TypeError("Expected a string or a {r, g, b} object as color");
227
82
  }
228
83
  const rgb = typeof color === "string" ? textToRgb(color) : color;
229
- const oldHsv = rgbToHsv(rgb);
84
+ const oldHsv = (0, import_colord.colord)(rgb).toHsv();
230
85
  if (index === 6)
231
86
  return rgbToHex(rgb);
232
87
  const light = index < 6;
@@ -237,7 +92,7 @@ function colorPalette(color, index) {
237
92
  v: value(oldHsv, i, light),
238
93
  a: oldHsv.a
239
94
  };
240
- return rgbToHex(hsvToRgb(newHsv));
95
+ return (0, import_colord.colord)(newHsv).toHex();
241
96
  }
242
97
  function hue(hsv, i, isLight) {
243
98
  let hue2;
@@ -278,14 +133,13 @@ function value(hsv, i, isLight) {
278
133
  // Annotate the CommonJS export names for ESM import in node:
279
134
  0 && (module.exports = {
280
135
  blend,
281
- blendAlpha,
282
136
  brightness,
283
137
  changeAlpha,
284
138
  colorPalette,
285
139
  hexToRgb,
286
140
  hsvToRgb,
287
141
  lighten,
288
- luminosity,
142
+ luminance,
289
143
  rgbToHex,
290
144
  rgbToHsv,
291
145
  textToRgb
package/index.cjs.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../index.ts"],
4
- "sourcesContent": ["export type RGBA = Record<'r' | 'g' | 'b' | 'a', number>\r\nexport type RGB = Record<'r' | 'g' | 'b', number> & { a?: number }\r\nexport type HEX = string\r\nexport type HSVA = Record<'h' | 's' | 'v' | 'a', number>\r\n\r\nexport type RGBA_TEXT = string\r\nexport type HEX_TEXT = string\r\nexport type HSVA_TEXT = string\r\nexport type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT\r\nexport type PALETTE_INDEXES = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10\r\n\r\nconst RE_RGBA = /^rgb(a)?\\((\\d{1,3}),(\\d{1,3}),(\\d{1,3}),?([01]?\\.?\\d*?)?\\)$/\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HEX/A representation as a\r\n *\r\n * String (`#RRGGBB<AA>`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHex({ r, g, b, a }: RGB): HEX {\r\n const alpha = a !== void 0\r\n\r\n r = Math.round(r)\r\n g = Math.round(g)\r\n b = Math.round(b)\r\n\r\n if (r > 255 || g > 255 || b > 255 || (alpha && a! > 100)) {\r\n throw new TypeError('Expected 3 numbers below 256 (and optionally one below 100)')\r\n }\r\n\r\n if (alpha) {\r\n a = Number((Math.round((255 * a!) / 100) | (1 << 8)).toString(16).slice(1))\r\n }\r\n\r\n return '#' + (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1) + a\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hexToRgb(hex: HEX): RGB {\r\n if (typeof hex !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n hex = hex.replace(/^#/, '')\r\n\r\n if (hex.length === 3) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]\r\n } else if (hex.length === 4) {\r\n hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3]\r\n }\r\n\r\n const num = parseInt(hex, 16)\r\n\r\n return hex.length > 6\r\n ? { r: (num >> 24) & 255, g: (num >> 16) & 255, b: (num >> 8) & 255, a: Math.round((num & 255) / 2.55) }\r\n : { r: num >> 16, g: (num >> 8) & 255, b: num & 255 }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hsvToRgb({ h, s, v, a }: HSVA): RGBA {\r\n let r: any, g: any, b: any\r\n s = s / 100\r\n v = v / 100\r\n\r\n h = h / 360\r\n const i = Math.floor(h * 6),\r\n f = h * 6 - i,\r\n p = v * (1 - s),\r\n q = v * (1 - f * s),\r\n t = v * (1 - (1 - f) * s)\r\n\r\n switch (i % 6) {\r\n case 0:\r\n r = v\r\n g = t\r\n b = p\r\n break\r\n case 1:\r\n r = q\r\n g = v\r\n b = p\r\n break\r\n case 2:\r\n r = p\r\n g = v\r\n b = t\r\n break\r\n case 3:\r\n r = p\r\n g = q\r\n b = v\r\n break\r\n case 4:\r\n r = t\r\n g = p\r\n b = v\r\n break\r\n case 5:\r\n r = v\r\n g = p\r\n b = q\r\n break\r\n }\r\n\r\n return {\r\n r: Math.round(r * 255),\r\n g: Math.round(g * 255),\r\n b: Math.round(b * 255),\r\n a\r\n }\r\n}\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HSV/A representation as an\r\n *\r\n * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-100]}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHsv({ r, g, b, a }: RGB): HSVA {\r\n const max = Math.max(r, g, b),\r\n min = Math.min(r, g, b),\r\n d = max - min,\r\n s = max === 0 ? 0 : d / max,\r\n v = max / 255\r\n let h: any\r\n\r\n switch (max) {\r\n case min:\r\n h = 0\r\n break\r\n case r:\r\n h = g - b + d * (g < b ? 6 : 0)\r\n h /= 6 * d\r\n break\r\n case g:\r\n h = b - r + d * 2\r\n h /= 6 * d\r\n break\r\n case b:\r\n h = r - g + d * 4\r\n h /= 6 * d\r\n break\r\n }\r\n\r\n return {\r\n h: Math.round(h * 360),\r\n s: Math.round(s * 100),\r\n v: Math.round(v * 100),\r\n a: a || 1\r\n }\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function textToRgb(str: COLOR): RGB {\r\n if (typeof str !== 'string') {\r\n throw new TypeError('Expected a string')\r\n }\r\n\r\n const color = str.replace(/ /g, '')\r\n\r\n const m = RE_RGBA.exec(color)\r\n\r\n if (m === null) {\r\n return hexToRgb(color)\r\n }\r\n\r\n const rgb: RGB = {\r\n r: Math.min(255, parseInt(m[2], 10)),\r\n g: Math.min(255, parseInt(m[3], 10)),\r\n b: Math.min(255, parseInt(m[4], 10))\r\n }\r\n\r\n if (m[1]) {\r\n const alpha = parseFloat(m[5])\r\n rgb.a = Math.min(1, isNaN(alpha) === true ? 1 : alpha) * 100\r\n }\r\n\r\n return rgb\r\n}\r\n\r\n/**\r\n * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).\r\n *\r\n * Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 100 or -100 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.\r\n */\r\nexport function lighten(color: COLOR, percent: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n if (typeof percent !== 'number') {\r\n throw new TypeError('Expected a numeric percent')\r\n }\r\n\r\n const rgb = textToRgb(color),\r\n t = percent < 0 ? 0 : 255,\r\n p = Math.abs(percent) / 100,\r\n R = rgb.r,\r\n G = rgb.g,\r\n B = rgb.b\r\n\r\n return (\r\n '#' +\r\n (\r\n 0x1_00_00_00 +\r\n (Math.round((t - R) * p) + R) * 0x1_00_00 +\r\n (Math.round((t - G) * p) + G) * 0x1_00 +\r\n (Math.round((t - B) * p) + B)\r\n )\r\n .toString(16)\r\n .slice(1)\r\n )\r\n}\r\n/**\r\n * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function luminosity(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color,\r\n r = rgb.r / 255,\r\n g = rgb.g / 255,\r\n b = rgb.b / 255,\r\n R = r <= 0.039_28 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4),\r\n G = g <= 0.039_28 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4),\r\n B = b <= 0.039_28 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4)\r\n return 0.2126 * R + 0.7152 * G + 0.0722 * B\r\n}\r\n/**\r\n * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function brightness(color: COLOR | RGB) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color\r\n\r\n return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000\r\n}\r\n\r\n/**\r\n * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.\r\n *\r\n * 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.\r\n */\r\nexport function blend(fgColor: COLOR | RGB, bgColor: COLOR | RGB) {\r\n if (typeof fgColor !== 'string' && (!fgColor || fgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as fgColor')\r\n }\r\n\r\n if (typeof bgColor !== 'string' && (!bgColor || bgColor.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b[, a]} object as bgColor')\r\n }\r\n\r\n const rgb1 = typeof fgColor === 'string' ? textToRgb(fgColor) : fgColor,\r\n r1 = rgb1.r / 255,\r\n g1 = rgb1.g / 255,\r\n b1 = rgb1.b / 255,\r\n a1 = rgb1.a !== void 0 ? rgb1.a / 100 : 1,\r\n rgb2 = typeof bgColor === 'string' ? textToRgb(bgColor) : bgColor,\r\n r2 = rgb2.r / 255,\r\n g2 = rgb2.g / 255,\r\n b2 = rgb2.b / 255,\r\n a2 = rgb2.a !== void 0 ? rgb2.a / 100 : 1,\r\n a = a1 + a2 * (1 - a1),\r\n r = Math.round(((r1 * a1 + r2 * a2 * (1 - a1)) / a) * 255),\r\n g = Math.round(((g1 * a1 + g2 * a2 * (1 - a1)) / a) * 255),\r\n b = Math.round(((b1 * a1 + b2 * a2 * (1 - a1)) / a) * 255)\r\n\r\n const ret = { r, g, b, a: Math.round(a * 100) }\r\n return typeof fgColor === 'string' ? rgbToHex(ret) : ret\r\n}\r\n\r\n/**\r\n * Increments or decrements the alpha of a string color.\r\n *\r\n * Accepts a HEX/A String as color and a number between -1 and 1 (including edges) as offset. Use a negative value to decrement and a positive number to increment (ex: blendAlpha('#ff0000', -0.1) to decrement alpha by 10%). Returns HEX/A String.\r\n */\r\nexport function blendAlpha(color: COLOR, offset: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n\r\n if (offset === void 0 || offset < -1 || offset > 1) {\r\n throw new TypeError('Expected offset to be between -1 and 1')\r\n }\r\n\r\n const { r, g, b, a } = textToRgb(color)\r\n const alpha = a !== void 0 ? a / 100 : 0\r\n\r\n return rgbToHex({\r\n r,\r\n g,\r\n b,\r\n a: Math.round(Math.min(1, Math.max(0, alpha + offset)) * 100)\r\n })\r\n}\r\n\r\n/**\r\n * Change color transparency\r\n */\r\nexport function changeAlpha(color: COLOR, alpha: number) {\r\n if (typeof color !== 'string') {\r\n throw new TypeError('Expected a string as color')\r\n }\r\n if (alpha === void 0 || alpha < 0 || alpha > 100) {\r\n throw new TypeError('Expected alpha to be between 1 and 100')\r\n }\r\n const rgba = textToRgb(color)\r\n rgba.a = alpha\r\n\r\n return rgbToHex(rgba)\r\n}\r\n\r\nconst hueStep = 2\r\nconst saturationStep = 16\r\nconst saturationStep2 = 5\r\nconst brightnessStep1 = 5\r\nconst brightnessStep2 = 15\r\nconst lightColorCount = 5\r\nconst darkColorCount = 4\r\n\r\n/**\r\n * \u6839\u636E\u989C\u8272\u83B7\u53D6\u8C03\u8272\u677F\u989C\u8272(\u4ECE\u5DE6\u81F3\u53F3\u989C\u8272\u4ECE\u6D45\u5230\u6DF1\uFF0C6\u4E3A\u4E3B\u8272\u53F7)\r\n * @param color - \u989C\u8272\r\n * @param index - \u8C03\u8272\u677F\u7684\u5BF9\u5E94\u7684\u8272\u53F7(6\u4E3A\u4E3B\u8272\u53F7)\r\n * @description \u7B97\u6CD5\u5B9E\u73B0\u4ECEant-design\u8C03\u8272\u677F\u7B97\u6CD5\u4E2D\u501F\u9274 https://github.com/ant-design/ant-design/blob/master/components/style/color/colorPalette.less\r\n */\r\nexport function colorPalette(color: COLOR | RGB, index: PALETTE_INDEXES) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color\r\n const oldHsv = rgbToHsv(rgb)\r\n\r\n if (index === 6) return rgbToHex(rgb)\r\n\r\n const light = index < 6\r\n const i = light ? lightColorCount + 1 - index : index - lightColorCount - 1\r\n const newHsv: HSVA = {\r\n h: hue(oldHsv, i, light),\r\n s: saturation(oldHsv, i, light),\r\n v: value(oldHsv, i, light),\r\n a: oldHsv.a\r\n }\r\n\r\n return rgbToHex(hsvToRgb(newHsv))\r\n}\r\n\r\n/**\r\n * \u83B7\u53D6\u8272\u76F8\u6E10\u53D8\r\n * @param hsv - hsv\u683C\u5F0F\u989C\u8272\u503C\r\n * @param i - \u4E0E6\u7684\u76F8\u5BF9\u8DDD\u79BB\r\n * @param isLight - \u662F\u5426\u662F\u4EAE\u989C\u8272\r\n */\r\nfunction hue(hsv: HSVA, i: number, isLight: boolean) {\r\n let hue: number\r\n if (hsv.h >= 60 && hsv.h <= 240) {\r\n // \u51B7\u8272\u8C03\r\n // \u51CF\u6DE1\u53D8\u4EAE \u8272\u76F8\u987A\u65F6\u9488\u65CB\u8F6C \u66F4\u6696\r\n // \u52A0\u6DF1\u53D8\u6697 \u8272\u76F8\u9006\u65F6\u9488\u65CB\u8F6C \u66F4\u51B7\r\n hue = isLight ? hsv.h - hueStep * i : hsv.h + hueStep * i\r\n } else {\r\n // \u6696\u8272\u8C03\r\n // \u51CF\u6DE1\u53D8\u4EAE \u8272\u76F8\u9006\u65F6\u9488\u65CB\u8F6C \u66F4\u6696\r\n // \u52A0\u6DF1\u53D8\u6697 \u8272\u76F8\u987A\u65F6\u9488\u65CB\u8F6C \u66F4\u51B7\r\n hue = isLight ? hsv.h + hueStep * i : hsv.h - hueStep * i\r\n }\r\n if (hue < 0) hue += 360\r\n else if (hue >= 360) hue -= 360\r\n\r\n return hue\r\n}\r\n\r\n/**\r\n * \u83B7\u53D6\u9971\u548C\u5EA6\u6E10\u53D8\r\n * @param hsv - hsv\u683C\u5F0F\u989C\u8272\u503C\r\n * @param i - \u4E0E6\u7684\u76F8\u5BF9\u8DDD\u79BB\r\n * @param isLight - \u662F\u5426\u662F\u4EAE\u989C\u8272\r\n */\r\nfunction saturation(hsv: HSVA, i: number, isLight: boolean) {\r\n let saturation: number\r\n if (isLight) saturation = hsv.s - saturationStep * i\r\n else if (i === darkColorCount) saturation = hsv.s + saturationStep\r\n else saturation = hsv.s + saturationStep2 * i\r\n\r\n if (saturation > 100) saturation = 100\r\n\r\n if (isLight && i === lightColorCount && saturation > 10) saturation = 10\r\n\r\n if (saturation < 6) saturation = 6\r\n\r\n return saturation\r\n}\r\n\r\n/**\r\n * \u83B7\u53D6\u660E\u5EA6\u6E10\u53D8\r\n * @param hsv - hsv\u683C\u5F0F\u989C\u8272\u503C\r\n * @param i - \u4E0E6\u7684\u76F8\u5BF9\u8DDD\u79BB\r\n * @param isLight - \u662F\u5426\u662F\u4EAE\u989C\u8272\r\n */\r\nfunction value(hsv: HSVA, i: number, isLight: boolean) {\r\n let value: number\r\n value = isLight ? hsv.v + brightnessStep1 * i : hsv.v - brightnessStep2 * i\r\n\r\n if (value > 100) value = 100\r\n\r\n return value\r\n}\r\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,IAAM,UAAU;AAaT,kBAAkB,EAAE,GAAG,GAAG,GAAG,KAAe;AACjD,QAAM,QAAQ,MAAM;AAEpB,MAAI,KAAK,MAAM,CAAC;AAChB,MAAI,KAAK,MAAM,CAAC;AAChB,MAAI,KAAK,MAAM,CAAC;AAEhB,MAAI,IAAI,OAAO,IAAI,OAAO,IAAI,OAAQ,SAAS,IAAK,KAAM;AACxD,UAAM,IAAI,UAAU,6DAA6D;AAAA,EACnF;AAEA,MAAI,OAAO;AACT,QAAI,OAAQ,MAAK,MAAO,MAAM,IAAM,GAAG,IAAK,KAAK,GAAI,SAAS,EAAE,EAAE,MAAM,CAAC,CAAC;AAAA,EAC5E;AAEA,SAAO,MAAO,KAAK,KAAK,IAAM,KAAK,KAAO,KAAK,IAAK,SAAS,EAAE,EAAE,MAAM,CAAC,IAAI;AAC9E;AAaO,kBAAkB,KAAe;AACtC,MAAI,OAAO,QAAQ,UAAU;AAC3B,UAAM,IAAI,UAAU,mBAAmB;AAAA,EACzC;AAEA,QAAM,IAAI,QAAQ,MAAM,EAAE;AAE1B,MAAI,IAAI,WAAW,GAAG;AACpB,UAAM,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI;AAAA,EACzD,WAAW,IAAI,WAAW,GAAG;AAC3B,UAAM,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK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GAAW,SAAkB;AACnD,MAAI;AACJ,MAAI,IAAI,KAAK,MAAM,IAAI,KAAK,KAAK;AAI/B,WAAM,UAAU,IAAI,IAAI,UAAU,IAAI,IAAI,IAAI,UAAU;AAAA,EAC1D,OAAO;AAIL,WAAM,UAAU,IAAI,IAAI,UAAU,IAAI,IAAI,IAAI,UAAU;AAAA,EAC1D;AACA,MAAI,OAAM;AAAG,YAAO;AAAA,WACX,QAAO;AAAK,YAAO;AAE5B,SAAO;AACT;AAQA,oBAAoB,KAAW,GAAW,SAAkB;AAC1D,MAAI;AACJ,MAAI;AAAS,kBAAa,IAAI,IAAI,iBAAiB;AAAA,WAC1C,MAAM;AAAgB,kBAAa,IAAI,IAAI;AAAA;AAC/C,kBAAa,IAAI,IAAI,kBAAkB;AAE5C,MAAI,cAAa;AAAK,kBAAa;AAEnC,MAAI,WAAW,MAAM,mBAAmB,cAAa;AAAI,kBAAa;AAEtE,MAAI,cAAa;AAAG,kBAAa;AAEjC,SAAO;AACT;AAQA,eAAe,KAAW,GAAW,SAAkB;AACrD,MAAI;AACJ,WAAQ,UAAU,IAAI,IAAI,kBAAkB,IAAI,IAAI,IAAI,kBAAkB;AAE1E,MAAI,SAAQ;AAAK,aAAQ;AAEzB,SAAO;AACT;",
4
+ "sourcesContent": ["import { colord, extend, AnyColor, RgbaColor, HsvaColor } from 'colord'\r\nimport a11yPlugin from 'colord/plugins/a11y'\r\nimport mixPlugin from 'colord/plugins/mix'\r\n\r\nextend([a11yPlugin, mixPlugin])\r\nexport type RGBA = Record<'r' | 'g' | 'b' | 'a', number>\r\nexport type RGB = Record<'r' | 'g' | 'b', number> & { a?: number }\r\nexport type HexColor = string\r\nexport type HSVA = Record<'h' | 's' | 'v' | 'a', number>\r\n\r\nexport type RGBA_TEXT = string\r\nexport type HEX_TEXT = string\r\nexport type HSVA_TEXT = string\r\nexport type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT\r\nexport type PALETTE_INDEXES = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HEX/A representation as a\r\n *\r\n * String (`#RRGGBB<AA>`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHex({ r, g, b, a }: RgbaColor): HexColor {\r\n return colord({ r, g, b, a }).toHex()\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hexToRgb(hex: HexColor): RgbaColor {\r\n return colord(hex).toRgb()\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function hsvToRgb({ h, s, v, a }: HsvaColor): RgbaColor {\r\n return colord({ h, s, v, a }).toRgb()\r\n}\r\n\r\n/**\r\n * Converts a RGB/A color\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)\r\n *\r\n * to its HSV/A representation as an\r\n *\r\n * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-1]}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function rgbToHsv({ r, g, b, a }: RgbaColor): HsvaColor {\r\n return colord({ r, b, g, a }).toHsv()\r\n}\r\n\r\n/**\r\n * Converts a HEX/A color\r\n *\r\n * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)\r\n *\r\n * to its RGB/A representation as an\r\n *\r\n * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).\r\n *\r\n * If Alpha channel is present in the original object it will be present also in the output.\r\n */\r\nexport function textToRgb(str: AnyColor): RgbaColor {\r\n return colord(str).toRgb()\r\n}\r\n\r\n/**\r\n * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).\r\n *\r\n * 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`.\r\n */\r\nexport function lighten(color: AnyColor, percent: number) {\r\n return colord(color).lighten(percent).toHex()\r\n}\r\n/**\r\n * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function luminance(color: AnyColor) {\r\n return colord(color).luminance()\r\n}\r\n/**\r\n * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.\r\n *\r\n * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.\r\n */\r\nexport function brightness(color: AnyColor) {\r\n return colord(color).brightness()\r\n}\r\n\r\n/**\r\n * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.\r\n *\r\n * 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.\r\n */\r\nexport function blend(fgColor: AnyColor, bgColor: AnyColor) {\r\n return colord(fgColor).mix(bgColor)\r\n}\r\n\r\n/**\r\n * Change color transparency\r\n */\r\nexport function changeAlpha(color: COLOR, alpha: number) {\r\n return colord(color).alpha(alpha).toHex()\r\n}\r\n\r\nconst hueStep = 2\r\nconst saturationStep = 16\r\nconst saturationStep2 = 5\r\nconst brightnessStep1 = 5\r\nconst brightnessStep2 = 15\r\nconst lightColorCount = 5\r\nconst darkColorCount = 4\r\n\r\n/**\r\n * \u6839\u636E\u989C\u8272\u83B7\u53D6\u8C03\u8272\u677F\u989C\u8272(\u4ECE\u5DE6\u81F3\u53F3\u989C\u8272\u4ECE\u6D45\u5230\u6DF1\uFF0C6\u4E3A\u4E3B\u8272\u53F7)\r\n * @param color - \u989C\u8272\r\n * @param index - \u8C03\u8272\u677F\u7684\u5BF9\u5E94\u7684\u8272\u53F7(6\u4E3A\u4E3B\u8272\u53F7)\r\n * @description \u7B97\u6CD5\u5B9E\u73B0\u4ECEant-design\u8C03\u8272\u677F\u7B97\u6CD5\u4E2D\u501F\u9274 https://github.com/ant-design/ant-design/blob/master/components/style/color/colorPalette.less\r\n */\r\nexport function colorPalette(color: string | RgbaColor, index: PALETTE_INDEXES) {\r\n if (typeof color !== 'string' && (!color || color.r === void 0)) {\r\n throw new TypeError('Expected a string or a {r, g, b} object as color')\r\n }\r\n const rgb = typeof color === 'string' ? textToRgb(color) : color\r\n const oldHsv = colord(rgb).toHsv()\r\n\r\n if (index === 6) return rgbToHex(rgb)\r\n\r\n const light = index < 6\r\n const i = light ? lightColorCount + 1 - index : index - lightColorCount - 1\r\n const newHsv = {\r\n h: hue(oldHsv, i, light),\r\n s: saturation(oldHsv, i, light),\r\n v: value(oldHsv, i, light),\r\n a: oldHsv.a\r\n }\r\n return colord(newHsv).toHex()\r\n}\r\n\r\n/**\r\n * \u83B7\u53D6\u8272\u76F8\u6E10\u53D8\r\n * @param hsv - hsv\u683C\u5F0F\u989C\u8272\u503C\r\n * @param i - \u4E0E6\u7684\u76F8\u5BF9\u8DDD\u79BB\r\n * @param isLight - \u662F\u5426\u662F\u4EAE\u989C\u8272\r\n */\r\nfunction hue(hsv: HsvaColor, i: number, isLight: boolean) {\r\n let hue: number\r\n if (hsv.h >= 60 && hsv.h <= 240) {\r\n // \u51B7\u8272\u8C03\r\n // \u51CF\u6DE1\u53D8\u4EAE \u8272\u76F8\u987A\u65F6\u9488\u65CB\u8F6C \u66F4\u6696\r\n // \u52A0\u6DF1\u53D8\u6697 \u8272\u76F8\u9006\u65F6\u9488\u65CB\u8F6C \u66F4\u51B7\r\n hue = isLight ? hsv.h - hueStep * i : hsv.h + hueStep * i\r\n } else {\r\n // \u6696\u8272\u8C03\r\n // \u51CF\u6DE1\u53D8\u4EAE \u8272\u76F8\u9006\u65F6\u9488\u65CB\u8F6C \u66F4\u6696\r\n // \u52A0\u6DF1\u53D8\u6697 \u8272\u76F8\u987A\u65F6\u9488\u65CB\u8F6C \u66F4\u51B7\r\n hue = isLight ? hsv.h + hueStep * i : hsv.h - hueStep * i\r\n }\r\n if (hue < 0) hue += 360\r\n else if (hue >= 360) hue -= 360\r\n\r\n return hue\r\n}\r\n\r\n/**\r\n * \u83B7\u53D6\u9971\u548C\u5EA6\u6E10\u53D8\r\n * @param hsv - hsv\u683C\u5F0F\u989C\u8272\u503C\r\n * @param i - \u4E0E6\u7684\u76F8\u5BF9\u8DDD\u79BB\r\n * @param isLight - \u662F\u5426\u662F\u4EAE\u989C\u8272\r\n */\r\nfunction saturation(hsv: HsvaColor, i: number, isLight: boolean) {\r\n let saturation: number\r\n if (isLight) saturation = hsv.s - saturationStep * i\r\n else if (i === darkColorCount) saturation = hsv.s + saturationStep\r\n else saturation = hsv.s + saturationStep2 * i\r\n\r\n if (saturation > 100) saturation = 100\r\n\r\n if (isLight && i === lightColorCount && saturation > 10) saturation = 10\r\n\r\n if (saturation < 6) saturation = 6\r\n\r\n return saturation\r\n}\r\n\r\n/**\r\n * \u83B7\u53D6\u660E\u5EA6\u6E10\u53D8\r\n * @param hsv - hsv\u683C\u5F0F\u989C\u8272\u503C\r\n * @param i - \u4E0E6\u7684\u76F8\u5BF9\u8DDD\u79BB\r\n * @param isLight - \u662F\u5426\u662F\u4EAE\u989C\u8272\r\n */\r\nfunction value(hsv: HsvaColor, i: number, isLight: boolean) {\r\n let value: number\r\n value = isLight ? hsv.v + brightnessStep1 * i : hsv.v - brightnessStep2 * i\r\n\r\n if (value > 100) value = 100\r\n\r\n return value\r\n}\r\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+D;AAC/D,kBAAuB;AACvB,iBAAsB;AAEtB,0BAAO,CAAC,qBAAY,kBAAS,CAAC;AAuBvB,kBAAkB,EAAE,GAAG,GAAG,GAAG,KAA0B;AAC5D,SAAO,0BAAO,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC,EAAE,MAAM;AACtC;AAaO,kBAAkB,KAA0B;AACjD,SAAO,0BAAO,GAAG,EAAE,MAAM;AAC3B;AAaO,kBAAkB,EAAE,GAAG,GAAG,GAAG,KAA2B;AAC7D,SAAO,0BAAO,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC,EAAE,MAAM;AACtC;AAaO,kBAAkB,EAAE,GAAG,GAAG,GAAG,KAA2B;AAC7D,SAAO,0BAAO,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC,EAAE,MAAM;AACtC;AAaO,mBAAmB,KAA0B;AAClD,SAAO,0BAAO,GAAG,EAAE,MAAM;AAC3B;AAOO,iBAAiB,OAAiB,SAAiB;AACxD,SAAO,0BAAO,KAAK,EAAE,QAAQ,OAAO,EAAE,MAAM;AAC9C;AAMO,mBAAmB,OAAiB;AACzC,SAAO,0BAAO,KAAK,EAAE,UAAU;AACjC;AAMO,oBAAoB,OAAiB;AAC1C,SAAO,0BAAO,KAAK,EAAE,WAAW;AAClC;AAOO,eAAe,SAAmB,SAAmB;AAC1D,SAAO,0BAAO,OAAO,EAAE,IAAI,OAAO;AACpC;AAKO,qBAAqB,OAAc,OAAe;AACvD,SAAO,0BAAO,KAAK,EAAE,MAAM,KAAK,EAAE,MAAM;AAC1C;AAEA,IAAM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E5C,MAAI,cAAa;AAAK,kBAAa;AAEnC,MAAI,WAAW,MAAM,mBAAmB,cAAa;AAAI,kBAAa;AAEtE,MAAI,cAAa;AAAG,kBAAa;AAEjC,SAAO;AACT;AAQA,eAAe,KAAgB,GAAW,SAAkB;AAC1D,MAAI;AACJ,WAAQ,UAAU,IAAI,IAAI,kBAAkB,IAAI,IAAI,IAAI,kBAAkB;AAE1E,MAAI,SAAQ;AAAK,aAAQ;AAEzB,SAAO;AACT;",
6
6
  "names": []
7
7
  }
package/index.d.ts CHANGED
@@ -1,8 +1,11 @@
1
+ import * as colord from 'colord';
2
+ import { RgbaColor, HsvaColor, AnyColor } from 'colord';
3
+
1
4
  declare type RGBA = Record<'r' | 'g' | 'b' | 'a', number>;
2
5
  declare type RGB = Record<'r' | 'g' | 'b', number> & {
3
6
  a?: number;
4
7
  };
5
- declare type HEX = string;
8
+ declare type HexColor = string;
6
9
  declare type HSVA = Record<'h' | 's' | 'v' | 'a', number>;
7
10
  declare type RGBA_TEXT = string;
8
11
  declare type HEX_TEXT = string;
@@ -20,7 +23,7 @@ declare type PALETTE_INDEXES = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
20
23
  *
21
24
  * If Alpha channel is present in the original object it will be present also in the output.
22
25
  */
23
- declare function rgbToHex({ r, g, b, a }: RGB): HEX;
26
+ declare function rgbToHex({ r, g, b, a }: RgbaColor): HexColor;
24
27
  /**
25
28
  * Converts a HEX/A color
26
29
  *
@@ -32,7 +35,7 @@ declare function rgbToHex({ r, g, b, a }: RGB): HEX;
32
35
  *
33
36
  * If Alpha channel is present in the original object it will be present also in the output.
34
37
  */
35
- declare function hexToRgb(hex: HEX): RGB;
38
+ declare function hexToRgb(hex: HexColor): RgbaColor;
36
39
  /**
37
40
  * Converts a HEX/A color
38
41
  *
@@ -44,7 +47,7 @@ declare function hexToRgb(hex: HEX): RGB;
44
47
  *
45
48
  * If Alpha channel is present in the original object it will be present also in the output.
46
49
  */
47
- declare function hsvToRgb({ h, s, v, a }: HSVA): RGBA;
50
+ declare function hsvToRgb({ h, s, v, a }: HsvaColor): RgbaColor;
48
51
  /**
49
52
  * Converts a RGB/A color
50
53
  *
@@ -52,11 +55,11 @@ declare function hsvToRgb({ h, s, v, a }: HSVA): RGBA;
52
55
  *
53
56
  * to its HSV/A representation as an
54
57
  *
55
- * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-100]}`).
58
+ * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-1]}`).
56
59
  *
57
60
  * If Alpha channel is present in the original object it will be present also in the output.
58
61
  */
59
- declare function rgbToHsv({ r, g, b, a }: RGB): HSVA;
62
+ declare function rgbToHsv({ r, g, b, a }: RgbaColor): HsvaColor;
60
63
  /**
61
64
  * Converts a HEX/A color
62
65
  *
@@ -68,42 +71,31 @@ declare function rgbToHsv({ r, g, b, a }: RGB): HSVA;
68
71
  *
69
72
  * If Alpha channel is present in the original object it will be present also in the output.
70
73
  */
71
- declare function textToRgb(str: COLOR): RGB;
74
+ declare function textToRgb(str: AnyColor): RgbaColor;
72
75
  /**
73
76
  * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).
74
77
  *
75
- * Accepts a HEX/A String or a RGB/A String as color and a percent (0 to 100 or -100 to 0) of lighten/darken to be applied to the `color`. Returns a HEX String representation of the calculated `color`.
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`.
76
79
  */
77
- declare function lighten(color: COLOR, percent: number): string;
80
+ declare function lighten(color: AnyColor, percent: number): string;
78
81
  /**
79
82
  * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.
80
83
  *
81
84
  * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
82
85
  */
83
- declare function luminosity(color: COLOR | RGB): number;
86
+ declare function luminance(color: AnyColor): number;
84
87
  /**
85
88
  * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.
86
89
  *
87
90
  * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
88
91
  */
89
- declare function brightness(color: COLOR | RGB): number;
92
+ declare function brightness(color: AnyColor): number;
90
93
  /**
91
94
  * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.
92
95
  *
93
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.
94
97
  */
95
- declare function blend(fgColor: COLOR | RGB, bgColor: COLOR | RGB): string | {
96
- r: number;
97
- g: number;
98
- b: number;
99
- a: number;
100
- };
101
- /**
102
- * Increments or decrements the alpha of a string color.
103
- *
104
- * Accepts a HEX/A String as color and a number between -1 and 1 (including edges) as offset. Use a negative value to decrement and a positive number to increment (ex: blendAlpha('#ff0000', -0.1) to decrement alpha by 10%). Returns HEX/A String.
105
- */
106
- declare function blendAlpha(color: COLOR, offset: number): string;
98
+ declare function blend(fgColor: AnyColor, bgColor: AnyColor): colord.Colord;
107
99
  /**
108
100
  * Change color transparency
109
101
  */
@@ -114,6 +106,6 @@ declare function changeAlpha(color: COLOR, alpha: number): string;
114
106
  * @param index - 调色板的对应的色号(6为主色号)
115
107
  * @description 算法实现从ant-design调色板算法中借鉴 https://github.com/ant-design/ant-design/blob/master/components/style/color/colorPalette.less
116
108
  */
117
- declare function colorPalette(color: COLOR | RGB, index: PALETTE_INDEXES): string;
109
+ declare function colorPalette(color: string | RgbaColor, index: PALETTE_INDEXES): string;
118
110
 
119
- export { COLOR, HEX, HEX_TEXT, HSVA, HSVA_TEXT, PALETTE_INDEXES, RGB, RGBA, RGBA_TEXT, blend, blendAlpha, brightness, changeAlpha, colorPalette, hexToRgb, hsvToRgb, lighten, luminosity, rgbToHex, rgbToHsv, textToRgb };
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 };