@hairy/palette 1.49.0 → 1.50.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) 2025-PRESENT Hairyf <https://github.com/antfu>
3
+ Copyright (c) 2025-PRESENT Hairyf <https://github.com/hairyf>
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
package/README.md CHANGED
@@ -1,36 +1,448 @@
1
1
  # @hairy/palette
2
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]
3
+ <!-- automd:badges name="@hairy/palette" github="hairyf/hairylib" license bundlephobia -->
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@hairy/palette)](https://npmjs.com/package/@hairy/palette)
6
+ [![npm downloads](https://img.shields.io/npm/dm/@hairy/palette)](https://npm.chart.dev/@hairy/palette)
7
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@hairy/palette)](https://bundlephobia.com/package/@hairy/palette)
8
+ [![license](https://img.shields.io/github/license/hairyf/hairylib)](https://github.com/hairyf/hairylib/blob/main/LICENSE)
9
+
10
+ <!-- /automd -->
11
+
12
+ Color palette utilities for generating and working with theme colors.
8
13
 
9
14
  ## Install
10
15
 
16
+ <!-- automd:pm-install name="@hairy/palette" -->
17
+
18
+ ```sh
19
+ # ✨ Auto-detect
20
+ npx nypm install @hairy/palette
21
+
22
+ # npm
23
+ npm install @hairy/palette
24
+
25
+ # yarn
26
+ yarn add @hairy/palette
27
+
28
+ # pnpm
29
+ pnpm add @hairy/palette
30
+
31
+ # bun
32
+ bun install @hairy/palette
33
+
34
+ # deno
35
+ deno install npm:@hairy/palette
36
+ ```
37
+
38
+ <!-- /automd -->
39
+
40
+ ## API
41
+
42
+ <!-- automd:jsdocs src="./src/index.ts" -->
43
+
44
+ ### `blend(fgColor, bgColor)`
45
+
46
+ Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.
47
+
48
+ 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.
49
+
50
+ ### `brightness(color)`
51
+
52
+ Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.
53
+
54
+ Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
55
+
56
+ ### `changeAlpha(color, alpha)`
57
+
58
+ Change color transparency
59
+
60
+ ### `colorPalette(color, index)`
61
+
62
+ Obtain palette colors based on color (from left to right, from light to dark, with 6 as the main color number)
63
+
64
+ ### `hexToRgb(hex)`
65
+
66
+ Converts a HEX/A color
67
+
68
+ String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
69
+
70
+ to its RGB/A representation as an
71
+
72
+ Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
73
+
74
+ If Alpha channel is present in the original object it will be present also in the output.
75
+
76
+ ### `hsvToRgb()`
77
+
78
+ Converts a HEX/A color
79
+
80
+ String (`#RRGGBB<AA>`)
81
+
82
+ to its RGB/A representation as an
83
+
84
+ Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .
85
+
86
+ If Alpha channel is present in the original object it will be present also in the output.
87
+
88
+ ### `lighten(color, percent)`
89
+
90
+ Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).
91
+
92
+ 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`.
93
+
94
+ ### `luminance(color)`
95
+
96
+ Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.
97
+
98
+ Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
99
+
100
+ ### `rgbToHex()`
101
+
102
+ Converts a RGB/A color
103
+
104
+ Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
105
+
106
+ to its HEX/A representation as a
107
+
108
+ String (`#RRGGBB<AA>`).
109
+
110
+ If Alpha channel is present in the original object it will be present also in the output.
111
+
112
+ ### `rgbToHsv()`
113
+
114
+ Converts a RGB/A color
115
+
116
+ Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
117
+
118
+ to its HSV/A representation as an
119
+
120
+ Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-1]}`).
121
+
122
+ If Alpha channel is present in the original object it will be present also in the output.
123
+
124
+ ### `textToRgb(str)`
125
+
126
+ Converts a HEX/A color
127
+
128
+ String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
129
+
130
+ to its RGB/A representation as an
131
+
132
+ Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
133
+
134
+ If Alpha channel is present in the original object it will be present also in the output.
135
+
136
+ <!-- /automd -->
137
+
138
+ ## Directory structure
139
+
140
+ <!-- automd:dir-tree imports=""maxDepth=2 -->
141
+
11
142
  ```
12
- ni @hairy/palette
143
+ ├── src/
144
+ │ └── index.ts
145
+ ├── test/
146
+ │ └── index.test.ts
147
+ ├── package.json
148
+ ├── README.md
149
+ └── tsdown.config.ts
13
150
  ```
14
151
 
15
- ## CDN
152
+ <!-- /automd -->
153
+
154
+ ## Source
155
+
156
+ <!-- automd:file src="./src/index.ts" code lang="ts" -->
157
+
158
+ ```ts [index.ts]
159
+ import type { AnyColor, HsvaColor, RgbaColor } from 'colord'
160
+ import { colord, extend } from 'colord'
161
+ import a11yPlugin from 'colord/plugins/a11y'
162
+ import mixPlugin from 'colord/plugins/mix'
163
+
164
+ extend([a11yPlugin, mixPlugin])
165
+
166
+ export type RGBA = Record<'r' | 'g' | 'b' | 'a', number>
167
+ export type RGB = Record<'r' | 'g' | 'b', number> & { a?: number }
168
+ export type HexColor = string
169
+ export type HSVA = Record<'h' | 's' | 'v' | 'a', number>
170
+
171
+ export type RGBA_TEXT = string
172
+ export type HEX_TEXT = string
173
+ export type HSVA_TEXT = string
174
+ export type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT
175
+ export type PALETTE_INDEXES = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
176
+
177
+ /**
178
+ * Converts a RGB/A color
179
+ *
180
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
181
+ *
182
+ * to its HEX/A representation as a
183
+ *
184
+ * String (`#RRGGBB<AA>`).
185
+ *
186
+ * If Alpha channel is present in the original object it will be present also in the output.
187
+ */
188
+ export function rgbToHex({ r, g, b, a }: RgbaColor): HexColor {
189
+ return colord({ r, g, b, a }).toHex()
190
+ }
191
+
192
+ /**
193
+ * Converts a HEX/A color
194
+ *
195
+ * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
196
+ *
197
+ * to its RGB/A representation as an
198
+ *
199
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
200
+ *
201
+ * If Alpha channel is present in the original object it will be present also in the output.
202
+ */
203
+ export function hexToRgb(hex: HexColor): RgbaColor {
204
+ return colord(hex).toRgb()
205
+ }
206
+
207
+ /**
208
+ * Converts a HEX/A color
209
+ *
210
+ * String (`#RRGGBB<AA>`)
211
+ *
212
+ * to its RGB/A representation as an
213
+ *
214
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .
215
+ *
216
+ * If Alpha channel is present in the original object it will be present also in the output.
217
+ */
218
+ export function hsvToRgb({ h, s, v, a }: HsvaColor): RgbaColor {
219
+ return colord({ h, s, v, a }).toRgb()
220
+ }
221
+
222
+ /**
223
+ * Converts a RGB/A color
224
+ *
225
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
226
+ *
227
+ * to its HSV/A representation as an
228
+ *
229
+ * Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-1]}`).
230
+ *
231
+ * If Alpha channel is present in the original object it will be present also in the output.
232
+ */
233
+ export function rgbToHsv({ r, g, b, a }: RgbaColor): HsvaColor {
234
+ return colord({ r, b, g, a }).toHsv()
235
+ }
236
+
237
+ /**
238
+ * Converts a HEX/A color
239
+ *
240
+ * String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
241
+ *
242
+ * to its RGB/A representation as an
243
+ *
244
+ * Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
245
+ *
246
+ * If Alpha channel is present in the original object it will be present also in the output.
247
+ */
248
+ export function textToRgb(str: AnyColor): RgbaColor {
249
+ return colord(str).toRgb()
250
+ }
251
+
252
+ /**
253
+ * Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).
254
+ *
255
+ * 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`.
256
+ */
257
+ export function lighten(color: AnyColor, percent: number) {
258
+ return colord(color).lighten(percent).toHex()
259
+ }
260
+ /**
261
+ * Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.
262
+ *
263
+ * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
264
+ */
265
+ export function luminance(color: AnyColor) {
266
+ return colord(color).luminance()
267
+ }
268
+ /**
269
+ * Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.
270
+ *
271
+ * Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
272
+ */
273
+ export function brightness(color: AnyColor) {
274
+ return colord(color).brightness()
275
+ }
276
+
277
+ /**
278
+ * Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.
279
+ *
280
+ * 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.
281
+ */
282
+ export function blend(fgColor: AnyColor, bgColor: AnyColor) {
283
+ return colord(fgColor).mix(bgColor)
284
+ }
285
+
286
+ /**
287
+ * Change color transparency
288
+ */
289
+ export function changeAlpha(color: COLOR, alpha: number) {
290
+ return colord(color).alpha(alpha).toHex()
291
+ }
292
+
293
+ const hueStep = 2
294
+ const saturationStep = 16
295
+ const saturationStep2 = 5
296
+ const brightnessStep1 = 5
297
+ const brightnessStep2 = 15
298
+ const lightColorCount = 5
299
+ const darkColorCount = 4
16
300
 
17
- ```html
18
- <script src="https://unpkg.com/@hairy/palette"></script>
301
+ /**
302
+ * Obtain palette colors based on color (from left to right, from light to dark, with 6 as the main color number)
303
+ * @param color - Color
304
+ * @param index - The corresponding color number of the color palette (6 as the main color number)
305
+ * @description algorithm implementation draws inspiration from ant design palette algorithm https://github.com/ant-design/ant-design/blob/master/components/style/color/colorPalette.less
306
+ */
307
+ export function colorPalette(color: string | RgbaColor, index: PALETTE_INDEXES) {
308
+ if (typeof color !== 'string' && (!color || color.r === void 0))
309
+ throw new TypeError('Expected a string or a {r, g, b} object as color')
310
+
311
+ const rgb = typeof color === 'string' ? textToRgb(color) : color
312
+ const oldHsv = colord(rgb).toHsv()
313
+
314
+ if (index === 6)
315
+ return rgbToHex(rgb)
316
+
317
+ const light = index < 6
318
+ const i = light ? lightColorCount + 1 - index : index - lightColorCount - 1
319
+ const newHsv = {
320
+ h: hue(oldHsv, i, light),
321
+ s: saturation(oldHsv, i, light),
322
+ v: value(oldHsv, i, light),
323
+ a: oldHsv.a,
324
+ }
325
+ return colord(newHsv).toHex()
326
+ }
327
+
328
+ /**
329
+ * Obtain color gradient
330
+ * @param hsv - Color values in HSV format
331
+ * @param i - relative distance to 6
332
+ * @param isLight - Is it a bright color
333
+ */
334
+ function hue(hsv: HsvaColor, i: number, isLight: boolean) {
335
+ let hue: number
336
+ if (hsv.h >= 60 && hsv.h <= 240) {
337
+ // Cool color tone
338
+ // Dimming and brightening colors, clockwise rotation brings warmth
339
+ // Deepen and darken the color, rotate counterclockwise to make it colder
340
+ hue = isLight ? hsv.h - hueStep * i : hsv.h + hueStep * i
341
+ }
342
+ else {
343
+ // Warm color tone
344
+ // Dimming and brightening colors are warmer when rotated counterclockwise
345
+ // Deepen and darken, rotate clockwise to make it cooler
346
+ hue = isLight ? hsv.h + hueStep * i : hsv.h - hueStep * i
347
+ }
348
+ if (hue < 0)
349
+ hue += 360
350
+ else if (hue >= 360)
351
+ hue -= 360
352
+
353
+ return hue
354
+ }
355
+
356
+ /**
357
+ * Obtain saturation gradient
358
+ * @param hsv - Color values in HSV format
359
+ * @param i - relative distance to 6
360
+ * @param isLight - Is it a bright color
361
+ */
362
+ function saturation(hsv: HsvaColor, i: number, isLight: boolean) {
363
+ let saturation: number
364
+ if (isLight)
365
+ saturation = hsv.s - saturationStep * i
366
+ else if (i === darkColorCount)
367
+ saturation = hsv.s + saturationStep
368
+ else saturation = hsv.s + saturationStep2 * i
369
+
370
+ if (saturation > 100)
371
+ saturation = 100
372
+
373
+ if (isLight && i === lightColorCount && saturation > 10)
374
+ saturation = 10
375
+
376
+ if (saturation < 6)
377
+ saturation = 6
378
+
379
+ return saturation
380
+ }
381
+
382
+ /**
383
+ * Obtain brightness gradient
384
+ * @param hsv - Color values in HSV format
385
+ * @param i - relative distance to 6
386
+ * @param isLight - Is it a bright color
387
+ */
388
+ function value(hsv: HsvaColor, i: number, isLight: boolean) {
389
+ let value: number
390
+ value = isLight ? hsv.v + brightnessStep1 * i : hsv.v - brightnessStep2 * i
391
+
392
+ if (value > 100)
393
+ value = 100
394
+
395
+ return value
396
+ }
19
397
  ```
20
398
 
399
+ <!-- /automd -->
400
+
401
+ ## Contributors
402
+
403
+ <!-- automd:contributors github="hairyf/hairylib" author="Hairyf" license="MIT" -->
404
+
405
+ Published under the [MIT](https://github.com/hairyf/hairylib/blob/main/LICENSE) license.
406
+ Made by [@Hairyf](https://github.com/Hairyf) and [community](https://github.com/hairyf/hairylib/graphs/contributors) 💛
407
+ <br><br>
408
+ <a href="https://github.com/hairyf/hairylib/graphs/contributors">
409
+ <img src="https://contrib.rocks/image?repo=hairyf/hairylib" />
410
+ </a>
411
+
412
+ <!-- /automd -->
413
+
21
414
  ## License
22
415
 
23
- [MIT](./LICENSE) License © [Hairyf](https://github.com/hairyf)
416
+ <!-- automd:fetch url="gh:hairyf/hairylib/main/LICENSE" -->
417
+
418
+ MIT License
419
+
420
+ Copyright (c) 2025-PRESENT Hairyf <https://github.com/hairyf>
421
+
422
+ Permission is hereby granted, free of charge, to any person obtaining a copy
423
+ of this software and associated documentation files (the "Software"), to deal
424
+ in the Software without restriction, including without limitation the rights
425
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
426
+ copies of the Software, and to permit persons to whom the Software is
427
+ furnished to do so, subject to the following conditions:
428
+
429
+ The above copyright notice and this permission notice shall be included in all
430
+ copies or substantial portions of the Software.
431
+
432
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
433
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
434
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
435
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
436
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
437
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
438
+ SOFTWARE.
439
+
440
+ <!-- /automd -->
441
+
442
+ <!-- automd:with-automd -->
443
+
444
+ ---
24
445
 
25
- <!-- Badges -->
446
+ _🤖 auto updated with [automd](https://automd.unjs.io)_
26
447
 
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
448
+ <!-- /automd -->
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/palette",
3
3
  "type": "module",
4
- "version": "1.49.0",
4
+ "version": "1.50.0",
5
5
  "description": "Library for color palette",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",