Skip to content

Commit 1d38fb6

Browse files
authored
refactor: use @ant-design/fast-color instead (#4070)
* refactor: Use @ant-design/fast-color instead * fix: test failed * chore: remove isValidColor All FastColor objects are valid. So isValid is always true. FastColor("not-a-color") -> `#000000` * refactor: rename directory `colorful` to `color` * fix: ci fail
1 parent 861f39b commit 1d38fb6

File tree

13 files changed

+878
-1087
lines changed

13 files changed

+878
-1087
lines changed

packages/@core/base/shared/build.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineBuildConfig({
77
'src/index',
88
'src/constants/index',
99
'src/utils/index',
10-
'src/colorful/index',
10+
'src/color/index',
1111
'src/cache/index',
1212
],
1313
});

packages/@core/base/shared/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
"development": "./src/utils/index.ts",
3636
"default": "./dist/utils/index.mjs"
3737
},
38-
"./colorful": {
39-
"types": "./src/colorful/index.ts",
40-
"development": "./src/colorful/index.ts",
41-
"default": "./dist/colorful/index.mjs"
38+
"./color": {
39+
"types": "./src/color/index.ts",
40+
"development": "./src/color/index.ts",
41+
"default": "./dist/color/index.mjs"
4242
},
4343
"./cache": {
4444
"types": "./src/cache/index.ts",
@@ -55,7 +55,7 @@
5555
}
5656
},
5757
"dependencies": {
58-
"@ctrl/tinycolor": "^4.1.0",
58+
"@ant-design/fast-color": "^2.0.5",
5959
"@vue/shared": "^3.4.35",
6060
"clsx": "^2.1.1",
6161
"defu": "^6.1.4",

packages/@core/base/shared/src/colorful/convert.test.ts renamed to packages/@core/base/shared/src/color/convert.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { convertToHsl, convertToHslCssVar, isValidColor } from './convert';
3+
import { convertToHsl, convertToHslCssVar, convertToRgb } from './convert';
44

55
describe('color conversion functions', () => {
66
it('should correctly convert color to HSL format', () => {
@@ -26,16 +26,16 @@ describe('color conversion functions', () => {
2626
const expectedHsl = '0 100% 50% / 0.5';
2727
expect(convertToHslCssVar(color)).toEqual(expectedHsl);
2828
});
29-
});
3029

31-
describe('isValidColor', () => {
32-
it('isValidColor function', () => {
33-
// 测试有效颜色
34-
expect(isValidColor('blue')).toBe(true);
35-
expect(isValidColor('#000000')).toBe(true);
30+
it('should correctly convert color to RGB CSS variable format', () => {
31+
const color = 'hsl(284, 100%, 50%)';
32+
const expectedRgb = 'rgb(187,0,255)';
33+
expect(convertToRgb(color)).toEqual(expectedRgb);
34+
});
3635

37-
// 测试无效颜色
38-
expect(isValidColor('invalid color')).toBe(false);
39-
expect(isValidColor()).toBe(false);
36+
it('should correctly convert color with alpha to RGBA CSS variable format', () => {
37+
const color = 'hsla(284, 100%, 50%, 0.92)';
38+
const expectedRgba = 'rgba(187,0,255,0.92)';
39+
expect(convertToRgb(color)).toEqual(expectedRgba);
4040
});
4141
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { FastColor } from '@ant-design/fast-color';
2+
3+
const Color = FastColor;
4+
5+
/**
6+
* 将颜色转换为HSL格式。
7+
*
8+
* HSL是一种颜色模型,包括色相(Hue)、饱和度(Saturation)和亮度(Lightness)三个部分。
9+
*
10+
* @param {string} color 输入的颜色。
11+
* @returns {string} HSL格式的颜色字符串。
12+
*/
13+
function convertToHsl(color: string): string {
14+
const { a, h, l, s } = new Color(color).toHsl();
15+
const hsl = `hsl(${Math.round(h)} ${Math.round(s * 100)}% ${Math.round(l * 100)}%)`;
16+
return a < 1 ? `${hsl} ${a}` : hsl;
17+
}
18+
19+
/**
20+
* 将颜色转换为HSL CSS变量。
21+
*
22+
* 这个函数与convertToHsl函数类似,但是返回的字符串格式稍有不同,
23+
* 以便可以作为CSS变量使用。
24+
*
25+
* @param {string} color 输入的颜色。
26+
* @returns {string} 可以作为CSS变量使用的HSL格式的颜色字符串。
27+
*/
28+
function convertToHslCssVar(color: string): string {
29+
const { a, h, l, s } = new Color(color).toHsl();
30+
const hsl = `${Math.round(h)} ${Math.round(s * 100)}% ${Math.round(l * 100)}%`;
31+
return a < 1 ? `${hsl} / ${a}` : hsl;
32+
}
33+
34+
function convertToRgb(color: string): string {
35+
return new Color(color).toRgbString();
36+
}
37+
38+
export { Color, convertToHsl, convertToHslCssVar, convertToRgb };

packages/@core/base/shared/src/colorful/generator.ts renamed to packages/@core/base/shared/src/color/generator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { TinyColor } from '@ctrl/tinycolor';
21
import { getColors } from 'theme-colors';
32

4-
import { convertToHslCssVar } from './convert';
3+
import { Color, convertToHslCssVar } from './convert';
54

65
interface ColorItem {
76
alias?: string;
@@ -14,7 +13,7 @@ function generatorColorVariables(colorItems: ColorItem[]) {
1413

1514
colorItems.forEach(({ alias, color, name }) => {
1615
if (color) {
17-
const colorsMap = getColors(new TinyColor(color).toHexString());
16+
const colorsMap = getColors(new Color(color).toHexString());
1817
let mainColor = colorsMap['500'];
1918

2019
const colorKeys = Object.keys(colorsMap);

packages/@core/base/shared/src/colorful/convert.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './cache';
2-
export * from './colorful';
2+
export * from './color';
33
export * from './constants';
44
export * from './utils';

packages/@core/ui-kit/shadcn-ui/src/components/menu-badge/menu-badge.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { MenuRecordBadgeRaw } from '@vben-core/typings';
33
44
import { computed } from 'vue';
55
6-
import { isValidColor } from '@vben-core/shared';
6+
import { convertToRgb } from '@vben-core/shared';
77
88
import BadgeDot from './menu-badge-dot.vue';
99
@@ -34,9 +34,9 @@ const badgeClass = computed(() => {
3434
});
3535
3636
const badgeStyle = computed(() => {
37-
if (badgeClass.value && isValidColor(badgeClass.value)) {
37+
if (badgeClass.value) {
3838
return {
39-
backgroundColor: badgeClass.value,
39+
backgroundColor: convertToRgb(badgeClass.value),
4040
};
4141
}
4242
return {};

packages/effects/hooks/src/use-design-tokens.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { reactive, watch } from 'vue';
22

33
import { preferences } from '@vben/preferences';
4-
import { hlsStringToRGBString, updateCSSVariables } from '@vben/utils';
4+
import { convertToRgb, updateCSSVariables } from '@vben/utils';
55

66
/**
77
* 用于适配各个框架的设计系统
@@ -102,7 +102,7 @@ export function useNaiveDesignTokens() {
102102

103103
const getCssVariableValue = (variable: string, isColor: boolean = true) => {
104104
const value = rootStyles.getPropertyValue(variable);
105-
return isColor ? hlsStringToRGBString(value) : value;
105+
return isColor ? convertToRgb(`hsl(${value})`) : value;
106106
};
107107

108108
watch(
@@ -145,8 +145,6 @@ export function useNaiveDesignTokens() {
145145
commonTokens.invertedColor = getCssVariableValue('--background-deep');
146146

147147
commonTokens.borderRadius = getCssVariableValue('--radius', false);
148-
149-
// antDesignTokens.colorBgMask = getCssVariableValue('--overlay');
150148
},
151149
{ immediate: true },
152150
);
@@ -160,7 +158,7 @@ export function useElementPlusDesignTokens() {
160158

161159
const getCssVariableValue = (variable: string, isColor: boolean = true) => {
162160
const value = rootStyles.getPropertyValue(variable);
163-
return isColor ? `hsl(${value})` : value;
161+
return isColor ? convertToRgb(`hsl(${value})`) : value;
164162
};
165163
watch(
166164
() => preferences.theme,

packages/effects/layouts/src/widgets/preferences/blocks/theme/builtin.vue

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
BUILT_IN_THEME_PRESETS,
1010
type BuiltinThemePreset,
1111
} from '@vben/preferences';
12-
import { convertToHsl, TinyColor } from '@vben/utils';
12+
import { Color, convertToHsl } from '@vben/utils';
1313
1414
defineOptions({
1515
name: 'PreferenceBuiltinTheme',
@@ -22,17 +22,11 @@ const modelValue = defineModel<BuiltinThemeType>({ default: 'default' });
2222
const themeColorPrimary = defineModel<string>('themeColorPrimary');
2323
2424
const inputValue = computed(() => {
25-
return new TinyColor(themeColorPrimary.value).toHexString();
25+
return new Color(themeColorPrimary.value || '').toHexString();
2626
});
2727
2828
const builtinThemePresets = computed(() => {
29-
return [
30-
// {
31-
// color: 'hsl(231 98% 65%)',
32-
// type: 'default',
33-
// },
34-
...BUILT_IN_THEME_PRESETS,
35-
];
29+
return [...BUILT_IN_THEME_PRESETS];
3630
});
3731
3832
function typeView(name: BuiltinThemeType) {

packages/utils/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './helpers';
2-
export * from '@vben-core/shared/colorful';
2+
export * from '@vben-core/shared/color';
33
export * from '@vben-core/shared/utils';

0 commit comments

Comments
 (0)