@hairy/palette 0.3.7 → 0.3.9
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/dist/index.cjs +153 -0
- package/dist/index.d.ts +111 -0
- package/dist/index.mjs +108 -0
- package/package.json +6 -6
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
blend: () => blend,
|
|
34
|
+
brightness: () => brightness,
|
|
35
|
+
changeAlpha: () => changeAlpha,
|
|
36
|
+
colorPalette: () => colorPalette,
|
|
37
|
+
hexToRgb: () => hexToRgb,
|
|
38
|
+
hsvToRgb: () => hsvToRgb,
|
|
39
|
+
lighten: () => lighten,
|
|
40
|
+
luminance: () => luminance,
|
|
41
|
+
rgbToHex: () => rgbToHex,
|
|
42
|
+
rgbToHsv: () => rgbToHsv,
|
|
43
|
+
textToRgb: () => textToRgb
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(src_exports);
|
|
46
|
+
var import_colord = require("colord");
|
|
47
|
+
var import_a11y = __toESM(require("colord/plugins/a11y"));
|
|
48
|
+
var import_mix = __toESM(require("colord/plugins/mix"));
|
|
49
|
+
(0, import_colord.extend)([import_a11y.default, import_mix.default]);
|
|
50
|
+
function rgbToHex({ r, g, b, a }) {
|
|
51
|
+
return (0, import_colord.colord)({ r, g, b, a }).toHex();
|
|
52
|
+
}
|
|
53
|
+
function hexToRgb(hex) {
|
|
54
|
+
return (0, import_colord.colord)(hex).toRgb();
|
|
55
|
+
}
|
|
56
|
+
function hsvToRgb({ h, s, v, a }) {
|
|
57
|
+
return (0, import_colord.colord)({ h, s, v, a }).toRgb();
|
|
58
|
+
}
|
|
59
|
+
function rgbToHsv({ r, g, b, a }) {
|
|
60
|
+
return (0, import_colord.colord)({ r, b, g, a }).toHsv();
|
|
61
|
+
}
|
|
62
|
+
function textToRgb(str) {
|
|
63
|
+
return (0, import_colord.colord)(str).toRgb();
|
|
64
|
+
}
|
|
65
|
+
function lighten(color, percent) {
|
|
66
|
+
return (0, import_colord.colord)(color).lighten(percent).toHex();
|
|
67
|
+
}
|
|
68
|
+
function luminance(color) {
|
|
69
|
+
return (0, import_colord.colord)(color).luminance();
|
|
70
|
+
}
|
|
71
|
+
function brightness(color) {
|
|
72
|
+
return (0, import_colord.colord)(color).brightness();
|
|
73
|
+
}
|
|
74
|
+
function blend(fgColor, bgColor) {
|
|
75
|
+
return (0, import_colord.colord)(fgColor).mix(bgColor);
|
|
76
|
+
}
|
|
77
|
+
function changeAlpha(color, alpha) {
|
|
78
|
+
return (0, import_colord.colord)(color).alpha(alpha).toHex();
|
|
79
|
+
}
|
|
80
|
+
var hueStep = 2;
|
|
81
|
+
var saturationStep = 16;
|
|
82
|
+
var saturationStep2 = 5;
|
|
83
|
+
var brightnessStep1 = 5;
|
|
84
|
+
var brightnessStep2 = 15;
|
|
85
|
+
var lightColorCount = 5;
|
|
86
|
+
var darkColorCount = 4;
|
|
87
|
+
function colorPalette(color, index) {
|
|
88
|
+
if (typeof color !== "string" && (!color || color.r === void 0))
|
|
89
|
+
throw new TypeError("Expected a string or a {r, g, b} object as color");
|
|
90
|
+
const rgb = typeof color === "string" ? textToRgb(color) : color;
|
|
91
|
+
const oldHsv = (0, import_colord.colord)(rgb).toHsv();
|
|
92
|
+
if (index === 6)
|
|
93
|
+
return rgbToHex(rgb);
|
|
94
|
+
const light = index < 6;
|
|
95
|
+
const i = light ? lightColorCount + 1 - index : index - lightColorCount - 1;
|
|
96
|
+
const newHsv = {
|
|
97
|
+
h: hue(oldHsv, i, light),
|
|
98
|
+
s: saturation(oldHsv, i, light),
|
|
99
|
+
v: value(oldHsv, i, light),
|
|
100
|
+
a: oldHsv.a
|
|
101
|
+
};
|
|
102
|
+
return (0, import_colord.colord)(newHsv).toHex();
|
|
103
|
+
}
|
|
104
|
+
function hue(hsv, i, isLight) {
|
|
105
|
+
let hue2;
|
|
106
|
+
if (hsv.h >= 60 && hsv.h <= 240) {
|
|
107
|
+
hue2 = isLight ? hsv.h - hueStep * i : hsv.h + hueStep * i;
|
|
108
|
+
} else {
|
|
109
|
+
hue2 = isLight ? hsv.h + hueStep * i : hsv.h - hueStep * i;
|
|
110
|
+
}
|
|
111
|
+
if (hue2 < 0)
|
|
112
|
+
hue2 += 360;
|
|
113
|
+
else if (hue2 >= 360)
|
|
114
|
+
hue2 -= 360;
|
|
115
|
+
return hue2;
|
|
116
|
+
}
|
|
117
|
+
function saturation(hsv, i, isLight) {
|
|
118
|
+
let saturation2;
|
|
119
|
+
if (isLight)
|
|
120
|
+
saturation2 = hsv.s - saturationStep * i;
|
|
121
|
+
else if (i === darkColorCount)
|
|
122
|
+
saturation2 = hsv.s + saturationStep;
|
|
123
|
+
else
|
|
124
|
+
saturation2 = hsv.s + saturationStep2 * i;
|
|
125
|
+
if (saturation2 > 100)
|
|
126
|
+
saturation2 = 100;
|
|
127
|
+
if (isLight && i === lightColorCount && saturation2 > 10)
|
|
128
|
+
saturation2 = 10;
|
|
129
|
+
if (saturation2 < 6)
|
|
130
|
+
saturation2 = 6;
|
|
131
|
+
return saturation2;
|
|
132
|
+
}
|
|
133
|
+
function value(hsv, i, isLight) {
|
|
134
|
+
let value2;
|
|
135
|
+
value2 = isLight ? hsv.v + brightnessStep1 * i : hsv.v - brightnessStep2 * i;
|
|
136
|
+
if (value2 > 100)
|
|
137
|
+
value2 = 100;
|
|
138
|
+
return value2;
|
|
139
|
+
}
|
|
140
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
141
|
+
0 && (module.exports = {
|
|
142
|
+
blend,
|
|
143
|
+
brightness,
|
|
144
|
+
changeAlpha,
|
|
145
|
+
colorPalette,
|
|
146
|
+
hexToRgb,
|
|
147
|
+
hsvToRgb,
|
|
148
|
+
lighten,
|
|
149
|
+
luminance,
|
|
150
|
+
rgbToHex,
|
|
151
|
+
rgbToHsv,
|
|
152
|
+
textToRgb
|
|
153
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import * as colord from 'colord';
|
|
2
|
+
import { RgbaColor, HsvaColor, AnyColor } from 'colord';
|
|
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
|
+
*/
|
|
109
|
+
declare function colorPalette(color: string | RgbaColor, index: PALETTE_INDEXES): string;
|
|
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 };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { colord, extend } from "colord";
|
|
3
|
+
import a11yPlugin from "colord/plugins/a11y";
|
|
4
|
+
import mixPlugin from "colord/plugins/mix";
|
|
5
|
+
extend([a11yPlugin, mixPlugin]);
|
|
6
|
+
function rgbToHex({ r, g, b, a }) {
|
|
7
|
+
return colord({ r, g, b, a }).toHex();
|
|
8
|
+
}
|
|
9
|
+
function hexToRgb(hex) {
|
|
10
|
+
return colord(hex).toRgb();
|
|
11
|
+
}
|
|
12
|
+
function hsvToRgb({ h, s, v, a }) {
|
|
13
|
+
return colord({ h, s, v, a }).toRgb();
|
|
14
|
+
}
|
|
15
|
+
function rgbToHsv({ r, g, b, a }) {
|
|
16
|
+
return colord({ r, b, g, a }).toHsv();
|
|
17
|
+
}
|
|
18
|
+
function textToRgb(str) {
|
|
19
|
+
return colord(str).toRgb();
|
|
20
|
+
}
|
|
21
|
+
function lighten(color, percent) {
|
|
22
|
+
return colord(color).lighten(percent).toHex();
|
|
23
|
+
}
|
|
24
|
+
function luminance(color) {
|
|
25
|
+
return colord(color).luminance();
|
|
26
|
+
}
|
|
27
|
+
function brightness(color) {
|
|
28
|
+
return colord(color).brightness();
|
|
29
|
+
}
|
|
30
|
+
function blend(fgColor, bgColor) {
|
|
31
|
+
return colord(fgColor).mix(bgColor);
|
|
32
|
+
}
|
|
33
|
+
function changeAlpha(color, alpha) {
|
|
34
|
+
return colord(color).alpha(alpha).toHex();
|
|
35
|
+
}
|
|
36
|
+
var hueStep = 2;
|
|
37
|
+
var saturationStep = 16;
|
|
38
|
+
var saturationStep2 = 5;
|
|
39
|
+
var brightnessStep1 = 5;
|
|
40
|
+
var brightnessStep2 = 15;
|
|
41
|
+
var lightColorCount = 5;
|
|
42
|
+
var darkColorCount = 4;
|
|
43
|
+
function colorPalette(color, index) {
|
|
44
|
+
if (typeof color !== "string" && (!color || color.r === void 0))
|
|
45
|
+
throw new TypeError("Expected a string or a {r, g, b} object as color");
|
|
46
|
+
const rgb = typeof color === "string" ? textToRgb(color) : color;
|
|
47
|
+
const oldHsv = colord(rgb).toHsv();
|
|
48
|
+
if (index === 6)
|
|
49
|
+
return rgbToHex(rgb);
|
|
50
|
+
const light = index < 6;
|
|
51
|
+
const i = light ? lightColorCount + 1 - index : index - lightColorCount - 1;
|
|
52
|
+
const newHsv = {
|
|
53
|
+
h: hue(oldHsv, i, light),
|
|
54
|
+
s: saturation(oldHsv, i, light),
|
|
55
|
+
v: value(oldHsv, i, light),
|
|
56
|
+
a: oldHsv.a
|
|
57
|
+
};
|
|
58
|
+
return colord(newHsv).toHex();
|
|
59
|
+
}
|
|
60
|
+
function hue(hsv, i, isLight) {
|
|
61
|
+
let hue2;
|
|
62
|
+
if (hsv.h >= 60 && hsv.h <= 240) {
|
|
63
|
+
hue2 = isLight ? hsv.h - hueStep * i : hsv.h + hueStep * i;
|
|
64
|
+
} else {
|
|
65
|
+
hue2 = isLight ? hsv.h + hueStep * i : hsv.h - hueStep * i;
|
|
66
|
+
}
|
|
67
|
+
if (hue2 < 0)
|
|
68
|
+
hue2 += 360;
|
|
69
|
+
else if (hue2 >= 360)
|
|
70
|
+
hue2 -= 360;
|
|
71
|
+
return hue2;
|
|
72
|
+
}
|
|
73
|
+
function saturation(hsv, i, isLight) {
|
|
74
|
+
let saturation2;
|
|
75
|
+
if (isLight)
|
|
76
|
+
saturation2 = hsv.s - saturationStep * i;
|
|
77
|
+
else if (i === darkColorCount)
|
|
78
|
+
saturation2 = hsv.s + saturationStep;
|
|
79
|
+
else
|
|
80
|
+
saturation2 = hsv.s + saturationStep2 * i;
|
|
81
|
+
if (saturation2 > 100)
|
|
82
|
+
saturation2 = 100;
|
|
83
|
+
if (isLight && i === lightColorCount && saturation2 > 10)
|
|
84
|
+
saturation2 = 10;
|
|
85
|
+
if (saturation2 < 6)
|
|
86
|
+
saturation2 = 6;
|
|
87
|
+
return saturation2;
|
|
88
|
+
}
|
|
89
|
+
function value(hsv, i, isLight) {
|
|
90
|
+
let value2;
|
|
91
|
+
value2 = isLight ? hsv.v + brightnessStep1 * i : hsv.v - brightnessStep2 * i;
|
|
92
|
+
if (value2 > 100)
|
|
93
|
+
value2 = 100;
|
|
94
|
+
return value2;
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
blend,
|
|
98
|
+
brightness,
|
|
99
|
+
changeAlpha,
|
|
100
|
+
colorPalette,
|
|
101
|
+
hexToRgb,
|
|
102
|
+
hsvToRgb,
|
|
103
|
+
lighten,
|
|
104
|
+
luminance,
|
|
105
|
+
rgbToHex,
|
|
106
|
+
rgbToHsv,
|
|
107
|
+
textToRgb
|
|
108
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hairy/palette",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.9",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"main": "./dist/index.cjs
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"jsdelivr": "./dist/index.iife.min.js"
|
|
8
8
|
},
|
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "ptsup src/index.ts --dts"
|
|
17
17
|
},
|
|
18
|
+
"module": "./dist/index.mjs",
|
|
18
19
|
"types": "./dist/index.d.ts",
|
|
19
|
-
"module": "./dist/index.esm.js",
|
|
20
20
|
"unpkg": "./dist/index.iife.min.js",
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
23
|
+
"import": "./dist/index.mjs",
|
|
24
|
+
"require": "./dist/index.cjs",
|
|
25
|
+
"types": "./dist/index.d.ts"
|
|
26
26
|
},
|
|
27
27
|
"./*": "./*"
|
|
28
28
|
}
|