@hairy/palette 0.3.9 → 0.3.10
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/package.json +1 -1
- package/src/index.ts +0 -238
package/package.json
CHANGED
package/src/index.ts
DELETED
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-void */
|
|
2
|
-
import type { AnyColor, HsvaColor, RgbaColor } from 'colord'
|
|
3
|
-
import { colord, extend } from 'colord'
|
|
4
|
-
import a11yPlugin from 'colord/plugins/a11y'
|
|
5
|
-
import mixPlugin from 'colord/plugins/mix'
|
|
6
|
-
|
|
7
|
-
extend([a11yPlugin, mixPlugin])
|
|
8
|
-
export type RGBA = Record<'r' | 'g' | 'b' | 'a', number>
|
|
9
|
-
export type RGB = Record<'r' | 'g' | 'b', number> & { a?: number }
|
|
10
|
-
export type HexColor = string
|
|
11
|
-
export type HSVA = Record<'h' | 's' | 'v' | 'a', number>
|
|
12
|
-
|
|
13
|
-
export type RGBA_TEXT = string
|
|
14
|
-
export type HEX_TEXT = string
|
|
15
|
-
export type HSVA_TEXT = string
|
|
16
|
-
export type COLOR = RGBA_TEXT | HEX_TEXT | HSVA_TEXT
|
|
17
|
-
export type PALETTE_INDEXES = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Converts a RGB/A color
|
|
21
|
-
*
|
|
22
|
-
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
|
|
23
|
-
*
|
|
24
|
-
* to its HEX/A representation as a
|
|
25
|
-
*
|
|
26
|
-
* String (`#RRGGBB<AA>`).
|
|
27
|
-
*
|
|
28
|
-
* If Alpha channel is present in the original object it will be present also in the output.
|
|
29
|
-
*/
|
|
30
|
-
export function rgbToHex({ r, g, b, a }: RgbaColor): HexColor {
|
|
31
|
-
return colord({ r, g, b, a }).toHex()
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Converts a HEX/A color
|
|
36
|
-
*
|
|
37
|
-
* String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
|
|
38
|
-
*
|
|
39
|
-
* to its RGB/A representation as an
|
|
40
|
-
*
|
|
41
|
-
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
|
|
42
|
-
*
|
|
43
|
-
* If Alpha channel is present in the original object it will be present also in the output.
|
|
44
|
-
*/
|
|
45
|
-
export function hexToRgb(hex: HexColor): RgbaColor {
|
|
46
|
-
return colord(hex).toRgb()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Converts a HEX/A color
|
|
51
|
-
*
|
|
52
|
-
* String (`#RRGGBB<AA>`)
|
|
53
|
-
*
|
|
54
|
-
* to its RGB/A representation as an
|
|
55
|
-
*
|
|
56
|
-
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`) .
|
|
57
|
-
*
|
|
58
|
-
* If Alpha channel is present in the original object it will be present also in the output.
|
|
59
|
-
*/
|
|
60
|
-
export function hsvToRgb({ h, s, v, a }: HsvaColor): RgbaColor {
|
|
61
|
-
return colord({ h, s, v, a }).toRgb()
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Converts a RGB/A color
|
|
66
|
-
*
|
|
67
|
-
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`)
|
|
68
|
-
*
|
|
69
|
-
* to its HSV/A representation as an
|
|
70
|
-
*
|
|
71
|
-
* Object (`{ h: [0-360], s: [0-100], v: [0-100}, a: [0-1]}`).
|
|
72
|
-
*
|
|
73
|
-
* If Alpha channel is present in the original object it will be present also in the output.
|
|
74
|
-
*/
|
|
75
|
-
export function rgbToHsv({ r, g, b, a }: RgbaColor): HsvaColor {
|
|
76
|
-
return colord({ r, b, g, a }).toHsv()
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Converts a HEX/A color
|
|
81
|
-
*
|
|
82
|
-
* String (`#RRGGBB<AA>`) or a RGB/A color String(`rgb(R, G, B<, A>)`)
|
|
83
|
-
*
|
|
84
|
-
* to its RGB/A representation as an
|
|
85
|
-
*
|
|
86
|
-
* Object (`{ r: [0-255], g: [0-255], b: [0-255}<, a: [0-100]>}`).
|
|
87
|
-
*
|
|
88
|
-
* If Alpha channel is present in the original object it will be present also in the output.
|
|
89
|
-
*/
|
|
90
|
-
export function textToRgb(str: AnyColor): RgbaColor {
|
|
91
|
-
return colord(str).toRgb()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Lighten the `color` (if `percent` is positive) or darken it (if `percent` is negative).
|
|
96
|
-
*
|
|
97
|
-
* 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`.
|
|
98
|
-
*/
|
|
99
|
-
export function lighten(color: AnyColor, percent: number) {
|
|
100
|
-
return colord(color).lighten(percent).toHex()
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Calculates the [relative luminance](https://www.w3.org/TR/WCAG20/#relativeluminancedef) of the `color`.
|
|
104
|
-
*
|
|
105
|
-
* Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
|
|
106
|
-
*/
|
|
107
|
-
export function luminance(color: AnyColor) {
|
|
108
|
-
return colord(color).luminance()
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Calculates the [color contrast](https://www.w3.org/TR/AERT/#color-contrast) of the `color`.
|
|
112
|
-
*
|
|
113
|
-
* Accepts a HEX/A String, a RGB/A String or a RGB/A Object as `color`. Returns a value between 0 and 1.
|
|
114
|
-
*/
|
|
115
|
-
export function brightness(color: AnyColor) {
|
|
116
|
-
return colord(color).brightness()
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Calculates the [blend](https://www.w3.org/TR/compositing-1/#simplealphacompositing) of two colors.
|
|
121
|
-
*
|
|
122
|
-
* 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.
|
|
123
|
-
*/
|
|
124
|
-
export function blend(fgColor: AnyColor, bgColor: AnyColor) {
|
|
125
|
-
return colord(fgColor).mix(bgColor)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Change color transparency
|
|
130
|
-
*/
|
|
131
|
-
export function changeAlpha(color: COLOR, alpha: number) {
|
|
132
|
-
return colord(color).alpha(alpha).toHex()
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const hueStep = 2
|
|
136
|
-
const saturationStep = 16
|
|
137
|
-
const saturationStep2 = 5
|
|
138
|
-
const brightnessStep1 = 5
|
|
139
|
-
const brightnessStep2 = 15
|
|
140
|
-
const lightColorCount = 5
|
|
141
|
-
const darkColorCount = 4
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* 根据颜色获取调色板颜色(从左至右颜色从浅到深,6为主色号)
|
|
145
|
-
* @param color - 颜色
|
|
146
|
-
* @param index - 调色板的对应的色号(6为主色号)
|
|
147
|
-
* @description 算法实现从ant-design调色板算法中借鉴 https://github.com/ant-design/ant-design/blob/master/components/style/color/colorPalette.less
|
|
148
|
-
*/
|
|
149
|
-
export function colorPalette(color: string | RgbaColor, index: PALETTE_INDEXES) {
|
|
150
|
-
if (typeof color !== 'string' && (!color || color.r === void 0))
|
|
151
|
-
throw new TypeError('Expected a string or a {r, g, b} object as color')
|
|
152
|
-
|
|
153
|
-
const rgb = typeof color === 'string' ? textToRgb(color) : color
|
|
154
|
-
const oldHsv = colord(rgb).toHsv()
|
|
155
|
-
|
|
156
|
-
if (index === 6)
|
|
157
|
-
return rgbToHex(rgb)
|
|
158
|
-
|
|
159
|
-
const light = index < 6
|
|
160
|
-
const i = light ? lightColorCount + 1 - index : index - lightColorCount - 1
|
|
161
|
-
const newHsv = {
|
|
162
|
-
h: hue(oldHsv, i, light),
|
|
163
|
-
s: saturation(oldHsv, i, light),
|
|
164
|
-
v: value(oldHsv, i, light),
|
|
165
|
-
a: oldHsv.a,
|
|
166
|
-
}
|
|
167
|
-
return colord(newHsv).toHex()
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 获取色相渐变
|
|
172
|
-
* @param hsv - hsv格式颜色值
|
|
173
|
-
* @param i - 与6的相对距离
|
|
174
|
-
* @param isLight - 是否是亮颜色
|
|
175
|
-
*/
|
|
176
|
-
function hue(hsv: HsvaColor, i: number, isLight: boolean) {
|
|
177
|
-
let hue: number
|
|
178
|
-
if (hsv.h >= 60 && hsv.h <= 240) {
|
|
179
|
-
// 冷色调
|
|
180
|
-
// 减淡变亮 色相顺时针旋转 更暖
|
|
181
|
-
// 加深变暗 色相逆时针旋转 更冷
|
|
182
|
-
hue = isLight ? hsv.h - hueStep * i : hsv.h + hueStep * i
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
// 暖色调
|
|
186
|
-
// 减淡变亮 色相逆时针旋转 更暖
|
|
187
|
-
// 加深变暗 色相顺时针旋转 更冷
|
|
188
|
-
hue = isLight ? hsv.h + hueStep * i : hsv.h - hueStep * i
|
|
189
|
-
}
|
|
190
|
-
if (hue < 0)
|
|
191
|
-
hue += 360
|
|
192
|
-
else if (hue >= 360)
|
|
193
|
-
hue -= 360
|
|
194
|
-
|
|
195
|
-
return hue
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* 获取饱和度渐变
|
|
200
|
-
* @param hsv - hsv格式颜色值
|
|
201
|
-
* @param i - 与6的相对距离
|
|
202
|
-
* @param isLight - 是否是亮颜色
|
|
203
|
-
*/
|
|
204
|
-
function saturation(hsv: HsvaColor, i: number, isLight: boolean) {
|
|
205
|
-
let saturation: number
|
|
206
|
-
if (isLight)
|
|
207
|
-
saturation = hsv.s - saturationStep * i
|
|
208
|
-
else if (i === darkColorCount)
|
|
209
|
-
saturation = hsv.s + saturationStep
|
|
210
|
-
else saturation = hsv.s + saturationStep2 * i
|
|
211
|
-
|
|
212
|
-
if (saturation > 100)
|
|
213
|
-
saturation = 100
|
|
214
|
-
|
|
215
|
-
if (isLight && i === lightColorCount && saturation > 10)
|
|
216
|
-
saturation = 10
|
|
217
|
-
|
|
218
|
-
if (saturation < 6)
|
|
219
|
-
saturation = 6
|
|
220
|
-
|
|
221
|
-
return saturation
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* 获取明度渐变
|
|
226
|
-
* @param hsv - hsv格式颜色值
|
|
227
|
-
* @param i - 与6的相对距离
|
|
228
|
-
* @param isLight - 是否是亮颜色
|
|
229
|
-
*/
|
|
230
|
-
function value(hsv: HsvaColor, i: number, isLight: boolean) {
|
|
231
|
-
let value: number
|
|
232
|
-
value = isLight ? hsv.v + brightnessStep1 * i : hsv.v - brightnessStep2 * i
|
|
233
|
-
|
|
234
|
-
if (value > 100)
|
|
235
|
-
value = 100
|
|
236
|
-
|
|
237
|
-
return value
|
|
238
|
-
}
|