Skip to content

Commit 39d8915

Browse files
authored
feat: support preset color (#85)
* chore: bump father version * chore: preset color * test: update test case * test: update test case * chore: bumdle size
1 parent 5e83dfa commit 39d8915

File tree

5 files changed

+327
-13
lines changed

5 files changed

+327
-13
lines changed

.fatherrc.js

-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,4 @@ import { defineConfig } from 'father';
22

33
export default defineConfig({
44
plugins: ['@rc-component/father-plugin'],
5-
targets: {
6-
// It's annoying ts will add prop def in class.
7-
// We have to use low version to compatible with this
8-
// Since some user not upgrade their webpack.
9-
chrome: 73,
10-
},
115
});

package.json

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ant-design/fast-color",
3-
"version": "2.0.6",
3+
"version": "3.0.0",
44
"description": "fast and small color class",
55
"keywords": [
66
"react",
@@ -35,16 +35,15 @@
3535
"coverage": "rc-test --coverage",
3636
"lint": "eslint src/ tests/ --ext .tsx,.ts,.jsx,.js",
3737
"now-build": "npm run build",
38-
"prepublishOnly": "npm run compile && np --yolo --no-publish",
38+
"prepublishOnly": "npm run compile && rc-np",
3939
"start": "dumi dev",
4040
"test": "rc-test"
4141
},
42-
"dependencies": {
43-
"@babel/runtime": "^7.24.7"
44-
},
42+
"dependencies": {},
4543
"devDependencies": {
4644
"@ctrl/tinycolor": "^4.1.0",
47-
"@rc-component/father-plugin": "^1.0.3",
45+
"@rc-component/father-plugin": "^2.0.4",
46+
"@rc-component/np": "^1.0.0",
4847
"@types/jest": "^29.5.12",
4948
"@types/node": "^22.1.0",
5049
"@umijs/fabric": "^4.0.1",
@@ -53,7 +52,6 @@
5352
"dumi": "^2.3.8",
5453
"eslint": "^8.57.0",
5554
"father": "^4.4.4",
56-
"np": "^10.0.5",
5755
"rc-test": "^7.0.15",
5856
"typescript": "^5.4.5",
5957
"vitest": "^1.6.0"

src/FastColor.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import presetColors from './presetColors';
12
import type { ColorInput, HSL, HSV, OptionalA, RGB } from './types';
23

34
type Constructor<T> = new (...args: any[]) => T;
@@ -126,6 +127,15 @@ export class FastColor {
126127
this.fromHslString(trimStr);
127128
} else if (matchPrefix('hsv') || matchPrefix('hsb')) {
128129
this.fromHsvString(trimStr);
130+
} else {
131+
// From preset color
132+
const presetColor = presetColors[trimStr.toLowerCase()];
133+
if (presetColor) {
134+
this.fromHexString(
135+
// Convert 36 hex to 16 hex
136+
parseInt(presetColor, 36).toString(16).padStart(6, '0'),
137+
);
138+
}
129139
}
130140
} else if (input instanceof FastColor) {
131141
this.r = input.r;

src/presetColors.ts

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// 36 Hex to reduce the size of the file
2+
export default {
3+
aliceblue: '9ehhb',
4+
antiquewhite: '9sgk7',
5+
aqua: '1ekf',
6+
aquamarine: '4zsno',
7+
azure: '9eiv3',
8+
beige: '9lhp8',
9+
bisque: '9zg04',
10+
black: '0',
11+
blanchedalmond: '9zhe5',
12+
blue: '73',
13+
blueviolet: '5e31e',
14+
brown: '6g016',
15+
burlywood: '8ouiv',
16+
cadetblue: '3qba8',
17+
chartreuse: '4zshs',
18+
chocolate: '87k0u',
19+
coral: '9yvyo',
20+
cornflowerblue: '3xael',
21+
cornsilk: '9zjz0',
22+
crimson: '8l4xo',
23+
cyan: '1ekf',
24+
darkblue: '3v',
25+
darkcyan: 'rkb',
26+
darkgoldenrod: '776yz',
27+
darkgray: '6mbhl',
28+
darkgreen: 'jr4',
29+
darkgrey: '6mbhl',
30+
darkkhaki: '7ehkb',
31+
darkmagenta: '5f91n',
32+
darkolivegreen: '3bzfz',
33+
darkorange: '9yygw',
34+
darkorchid: '5z6x8',
35+
darkred: '5f8xs',
36+
darksalmon: '9441m',
37+
darkseagreen: '5lwgf',
38+
darkslateblue: '2th1n',
39+
darkslategray: '1ugcv',
40+
darkslategrey: '1ugcv',
41+
darkturquoise: '14up',
42+
darkviolet: '5rw7n',
43+
deeppink: '9yavn',
44+
deepskyblue: '11xb',
45+
dimgray: '442g9',
46+
dimgrey: '442g9',
47+
dodgerblue: '16xof',
48+
firebrick: '6y7tu',
49+
floralwhite: '9zkds',
50+
forestgreen: '1cisi',
51+
fuchsia: '9y70f',
52+
gainsboro: '8m8kc',
53+
ghostwhite: '9pq0v',
54+
goldenrod: '8j4f4',
55+
gold: '9zda8',
56+
gray: '50i2o',
57+
green: 'pa8',
58+
greenyellow: '6senj',
59+
grey: '50i2o',
60+
honeydew: '9eiuo',
61+
hotpink: '9yrp0',
62+
indianred: '80gnw',
63+
indigo: '2xcoy',
64+
ivory: '9zldc',
65+
khaki: '9edu4',
66+
lavenderblush: '9ziet',
67+
lavender: '90c8q',
68+
lawngreen: '4vk74',
69+
lemonchiffon: '9zkct',
70+
lightblue: '6s73a',
71+
lightcoral: '9dtog',
72+
lightcyan: '8s1rz',
73+
lightgoldenrodyellow: '9sjiq',
74+
lightgray: '89jo3',
75+
lightgreen: '5nkwg',
76+
lightgrey: '89jo3',
77+
lightpink: '9z6wx',
78+
lightsalmon: '9z2ii',
79+
lightseagreen: '19xgq',
80+
lightskyblue: '5arju',
81+
lightslategray: '4nwk9',
82+
lightslategrey: '4nwk9',
83+
lightsteelblue: '6wau6',
84+
lightyellow: '9zlcw',
85+
lime: '1edc',
86+
limegreen: '1zcxe',
87+
linen: '9shk6',
88+
magenta: '9y70f',
89+
maroon: '4zsow',
90+
mediumaquamarine: '40eju',
91+
mediumblue: '5p',
92+
mediumorchid: '79qkz',
93+
mediumpurple: '5r3rv',
94+
mediumseagreen: '2d9ip',
95+
mediumslateblue: '4tcku',
96+
mediumspringgreen: '1di2',
97+
mediumturquoise: '2uabw',
98+
mediumvioletred: '7rn9h',
99+
midnightblue: 'z980',
100+
mintcream: '9ljp6',
101+
mistyrose: '9zg0x',
102+
moccasin: '9zfzp',
103+
navajowhite: '9zest',
104+
navy: '3k',
105+
oldlace: '9wq92',
106+
olive: '50hz4',
107+
olivedrab: '472ub',
108+
orange: '9z3eo',
109+
orangered: '9ykg0',
110+
orchid: '8iu3a',
111+
palegoldenrod: '9bl4a',
112+
palegreen: '5yw0o',
113+
paleturquoise: '6v4ku',
114+
palevioletred: '8k8lv',
115+
papayawhip: '9zi6t',
116+
peachpuff: '9ze0p',
117+
peru: '80oqn',
118+
pink: '9z8wb',
119+
plum: '8nba5',
120+
powderblue: '6wgdi',
121+
purple: '4zssg',
122+
rebeccapurple: '3zk49',
123+
red: '9y6tc',
124+
rosybrown: '7cv4f',
125+
royalblue: '2jvtt',
126+
saddlebrown: '5fmkz',
127+
salmon: '9rvci',
128+
sandybrown: '9jn1c',
129+
seagreen: '1tdnb',
130+
seashell: '9zje6',
131+
sienna: '6973h',
132+
silver: '7ir40',
133+
skyblue: '5arjf',
134+
slateblue: '45e4t',
135+
slategray: '4e100',
136+
slategrey: '4e100',
137+
snow: '9zke2',
138+
springgreen: '1egv',
139+
steelblue: '2r1kk',
140+
tan: '87yx8',
141+
teal: 'pds',
142+
thistle: '8ggk8',
143+
tomato: '9yqfb',
144+
turquoise: '2j4r4',
145+
violet: '9b10u',
146+
wheat: '9ld4j',
147+
white: '9zldr',
148+
whitesmoke: '9lhpx',
149+
yellow: '9zl6o',
150+
yellowgreen: '61fzm',
151+
};

tests/preset.test.ts

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import { FastColor } from '../src';
2+
3+
describe('index', () => {
4+
const originPresetColors = {
5+
aliceblue: '#f0f8ff',
6+
antiquewhite: '#faebd7',
7+
aqua: '#00ffff',
8+
aquamarine: '#7fffd4',
9+
azure: '#f0ffff',
10+
beige: '#f5f5dc',
11+
bisque: '#ffe4c4',
12+
black: '#000000',
13+
blanchedalmond: '#ffebcd',
14+
blue: '#0000ff',
15+
blueviolet: '#8a2be2',
16+
brown: '#a52a2a',
17+
burlywood: '#deb887',
18+
cadetblue: '#5f9ea0',
19+
chartreuse: '#7fff00',
20+
chocolate: '#d2691e',
21+
coral: '#ff7f50',
22+
cornflowerblue: '#6495ed',
23+
cornsilk: '#fff8dc',
24+
crimson: '#dc143c',
25+
cyan: '#00ffff',
26+
darkblue: '#00008b',
27+
darkcyan: '#008b8b',
28+
darkgoldenrod: '#b8860b',
29+
darkgray: '#a9a9a9',
30+
darkgreen: '#006400',
31+
darkgrey: '#a9a9a9',
32+
darkkhaki: '#bdb76b',
33+
darkmagenta: '#8b008b',
34+
darkolivegreen: '#556b2f',
35+
darkorange: '#ff8c00',
36+
darkorchid: '#9932cc',
37+
darkred: '#8b0000',
38+
darksalmon: '#e9967a',
39+
darkseagreen: '#8fbc8f',
40+
darkslateblue: '#483d8b',
41+
darkslategray: '#2f4f4f',
42+
darkslategrey: '#2f4f4f',
43+
darkturquoise: '#00ced1',
44+
darkviolet: '#9400d3',
45+
deeppink: '#ff1493',
46+
deepskyblue: '#00bfff',
47+
dimgray: '#696969',
48+
dimgrey: '#696969',
49+
dodgerblue: '#1e90ff',
50+
firebrick: '#b22222',
51+
floralwhite: '#fffaf0',
52+
forestgreen: '#228b22',
53+
fuchsia: '#ff00ff',
54+
gainsboro: '#dcdcdc',
55+
ghostwhite: '#f8f8ff',
56+
goldenrod: '#daa520',
57+
gold: '#ffd700',
58+
gray: '#808080',
59+
green: '#008000',
60+
greenyellow: '#adff2f',
61+
grey: '#808080',
62+
honeydew: '#f0fff0',
63+
hotpink: '#ff69b4',
64+
indianred: '#cd5c5c',
65+
indigo: '#4b0082',
66+
ivory: '#fffff0',
67+
khaki: '#f0e68c',
68+
lavenderblush: '#fff0f5',
69+
lavender: '#e6e6fa',
70+
lawngreen: '#7cfc00',
71+
lemonchiffon: '#fffacd',
72+
lightblue: '#add8e6',
73+
lightcoral: '#f08080',
74+
lightcyan: '#e0ffff',
75+
lightgoldenrodyellow: '#fafad2',
76+
lightgray: '#d3d3d3',
77+
lightgreen: '#90ee90',
78+
lightgrey: '#d3d3d3',
79+
lightpink: '#ffb6c1',
80+
lightsalmon: '#ffa07a',
81+
lightseagreen: '#20b2aa',
82+
lightskyblue: '#87cefa',
83+
lightslategray: '#778899',
84+
lightslategrey: '#778899',
85+
lightsteelblue: '#b0c4de',
86+
lightyellow: '#ffffe0',
87+
lime: '#00ff00',
88+
limegreen: '#32cd32',
89+
linen: '#faf0e6',
90+
magenta: '#ff00ff',
91+
maroon: '#800000',
92+
mediumaquamarine: '#66cdaa',
93+
mediumblue: '#0000cd',
94+
mediumorchid: '#ba55d3',
95+
mediumpurple: '#9370db',
96+
mediumseagreen: '#3cb371',
97+
mediumslateblue: '#7b68ee',
98+
mediumspringgreen: '#00fa9a',
99+
mediumturquoise: '#48d1cc',
100+
mediumvioletred: '#c71585',
101+
midnightblue: '#191970',
102+
mintcream: '#f5fffa',
103+
mistyrose: '#ffe4e1',
104+
moccasin: '#ffe4b5',
105+
navajowhite: '#ffdead',
106+
navy: '#000080',
107+
oldlace: '#fdf5e6',
108+
olive: '#808000',
109+
olivedrab: '#6b8e23',
110+
orange: '#ffa500',
111+
orangered: '#ff4500',
112+
orchid: '#da70d6',
113+
palegoldenrod: '#eee8aa',
114+
palegreen: '#98fb98',
115+
paleturquoise: '#afeeee',
116+
palevioletred: '#db7093',
117+
papayawhip: '#ffefd5',
118+
peachpuff: '#ffdab9',
119+
peru: '#cd853f',
120+
pink: '#ffc0cb',
121+
plum: '#dda0dd',
122+
powderblue: '#b0e0e6',
123+
purple: '#800080',
124+
rebeccapurple: '#663399',
125+
red: '#ff0000',
126+
rosybrown: '#bc8f8f',
127+
royalblue: '#4169e1',
128+
saddlebrown: '#8b4513',
129+
salmon: '#fa8072',
130+
sandybrown: '#f4a460',
131+
seagreen: '#2e8b57',
132+
seashell: '#fff5ee',
133+
sienna: '#a0522d',
134+
silver: '#c0c0c0',
135+
skyblue: '#87ceeb',
136+
slateblue: '#6a5acd',
137+
slategray: '#708090',
138+
slategrey: '#708090',
139+
snow: '#fffafa',
140+
springgreen: '#00ff7f',
141+
steelblue: '#4682b4',
142+
tan: '#d2b48c',
143+
teal: '#008080',
144+
thistle: '#d8bfd8',
145+
tomato: '#ff6347',
146+
turquoise: '#40e0d0',
147+
violet: '#ee82ee',
148+
wheat: '#f5deb3',
149+
white: '#ffffff',
150+
whitesmoke: '#f5f5f5',
151+
yellow: '#ffff00',
152+
yellowgreen: '#9acd32',
153+
};
154+
155+
for (const colorName in originPresetColors) {
156+
it(`preset color: ${colorName}`, () => {
157+
const color = new FastColor(colorName);
158+
expect(color.toHexString()).toBe(originPresetColors[colorName]);
159+
});
160+
}
161+
});

0 commit comments

Comments
 (0)