@hairy/palette 0.3.2 → 0.3.4
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/LICENSE +1 -1
- package/dist/index.cjs.js +153 -0
- package/{index.d.ts → dist/index.d.ts} +25 -33
- package/dist/index.esm.js +108 -0
- package/package.json +16 -10
- package/src/index.ts +238 -0
- package/CHANGELOG.md +0 -31
- package/index.cjs.js +0 -293
- package/index.cjs.js.map +0 -7
- package/index.esm.js +0 -258
- package/index.esm.js.map +0 -7
- package/index.iife.js +0 -280
- package/index.iife.js.map +0 -7
- package/index.iife.min.js +0 -2
- package/index.iife.min.js.map +0 -7
- package/index.md +0 -84
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2019-PRESENT
|
|
3
|
+
Copyright (c) 2019-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
|
|
@@ -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
|
+
});
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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> & {
|
|
3
6
|
a?: number;
|
|
4
7
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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;
|
|
12
15
|
/**
|
|
13
16
|
* Converts a RGB/A color
|
|
14
17
|
*
|
|
@@ -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 }:
|
|
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:
|
|
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 }:
|
|
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-
|
|
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 }:
|
|
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:
|
|
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
|
|
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:
|
|
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
|
|
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:
|
|
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:
|
|
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:
|
|
109
|
+
declare function colorPalette(color: string | RgbaColor, index: PALETTE_INDEXES): string;
|
|
118
110
|
|
|
119
|
-
export { COLOR,
|
|
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 };
|
|
@@ -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,21 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hairy/palette",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.3.4",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "./dist/index.cjs.js",
|
|
5
6
|
"publishConfig": {
|
|
6
|
-
"jsdelivr": "./index.iife.min.js"
|
|
7
|
+
"jsdelivr": "./dist/index.iife.min.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"colord": "^2.9.3"
|
|
7
14
|
},
|
|
8
|
-
"license": "MIT",
|
|
9
15
|
"scripts": {
|
|
10
|
-
"build": "
|
|
16
|
+
"build": "ptsup src/index.ts --dts"
|
|
11
17
|
},
|
|
12
|
-
"types": "./index.d.ts",
|
|
13
|
-
"module": "./index.esm.js",
|
|
14
|
-
"unpkg": "./index.iife.min.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"module": "./dist/index.esm.js",
|
|
20
|
+
"unpkg": "./dist/index.iife.min.js",
|
|
15
21
|
"exports": {
|
|
16
22
|
".": {
|
|
17
|
-
"import": "./index.esm.js",
|
|
18
|
-
"require": "./index.cjs.js"
|
|
23
|
+
"import": "./dist/index.esm.js",
|
|
24
|
+
"require": "./dist/index.cjs.js"
|
|
19
25
|
},
|
|
20
26
|
"./*": "./*"
|
|
21
27
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
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
|
+
}
|