Skip to content

Commit 8ffc853

Browse files
authored
chore: switch back to @ctrl/tinycolor (#4077)
* chore: switch back to `@ctrl/tinycolor` * fix: ci
1 parent a9a14fd commit 8ffc853

File tree

7 files changed

+24
-52
lines changed

7 files changed

+24
-52
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
}
5656
},
5757
"dependencies": {
58-
"@ant-design/fast-color": "^2.0.6",
58+
"@ctrl/tinycolor": "^4.1.0",
5959
"@vue/shared": "^3.4.36",
6060
"clsx": "^2.1.1",
6161
"defu": "^6.1.4",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ describe('color conversion functions', () => {
2929

3030
it('should correctly convert color to RGB CSS variable format', () => {
3131
const color = 'hsl(284, 100%, 50%)';
32-
const expectedRgb = 'rgb(187,0,255)';
32+
const expectedRgb = 'rgb(187, 0, 255)';
3333
expect(convertToRgb(color)).toEqual(expectedRgb);
3434
});
3535

3636
it('should correctly convert color with alpha to RGBA CSS variable format', () => {
3737
const color = 'hsla(284, 100%, 50%, 0.92)';
38-
const expectedRgba = 'rgba(187,0,255,0.92)';
38+
const expectedRgba = 'rgba(187, 0, 255, 0.92)';
3939
expect(convertToRgb(color)).toEqual(expectedRgba);
4040
});
4141
});

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { FastColor } from '@ant-design/fast-color';
2-
3-
const Color = FastColor;
1+
import { TinyColor } from '@ctrl/tinycolor';
42

53
/**
64
* 将颜色转换为HSL格式。
@@ -11,7 +9,7 @@ const Color = FastColor;
119
* @returns {string} HSL格式的颜色字符串。
1210
*/
1311
function convertToHsl(color: string): string {
14-
const { a, h, l, s } = new Color(color).toHsl();
12+
const { a, h, l, s } = new TinyColor(color).toHsl();
1513
const hsl = `hsl(${Math.round(h)} ${Math.round(s * 100)}% ${Math.round(l * 100)}%)`;
1614
return a < 1 ? `${hsl} ${a}` : hsl;
1715
}
@@ -26,13 +24,21 @@ function convertToHsl(color: string): string {
2624
* @returns {string} 可以作为CSS变量使用的HSL格式的颜色字符串。
2725
*/
2826
function convertToHslCssVar(color: string): string {
29-
const { a, h, l, s } = new Color(color).toHsl();
27+
const { a, h, l, s } = new TinyColor(color).toHsl();
3028
const hsl = `${Math.round(h)} ${Math.round(s * 100)}% ${Math.round(l * 100)}%`;
3129
return a < 1 ? `${hsl} / ${a}` : hsl;
3230
}
3331

34-
function convertToRgb(color: string): string {
35-
return new Color(color).toRgbString();
32+
/**
33+
* 将颜色转换为RGB颜色字符串
34+
* TinyColor无法处理hsl内包含'deg'、'grad'、'rad'或'turn'的字符串
35+
* 比如 hsl(231deg 98% 65%)将被解析为rgb(0, 0, 0)
36+
* 这里在转换之前先将这些单位去掉
37+
* @param str 表示HLS颜色值的字符串
38+
* @returns 如果颜色值有效,则返回对应的RGB颜色字符串;如果无效,则返回rgb(0, 0, 0)
39+
*/
40+
function convertToRgb(str: string): string {
41+
return new TinyColor(str.replaceAll(/deg|grad|rad|turn/g, '')).toRgbString();
3642
}
3743

38-
export { Color, convertToHsl, convertToHslCssVar, convertToRgb };
44+
export { convertToHsl, convertToHslCssVar, convertToRgb, TinyColor };

packages/@core/base/shared/src/color/generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getColors } from 'theme-colors';
22

3-
import { Color, convertToHslCssVar } from './convert';
3+
import { convertToHslCssVar, TinyColor } from './convert';
44

55
interface ColorItem {
66
alias?: string;
@@ -13,7 +13,7 @@ function generatorColorVariables(colorItems: ColorItem[]) {
1313

1414
colorItems.forEach(({ alias, color, name }) => {
1515
if (color) {
16-
const colorsMap = getColors(new Color(color).toHexString());
16+
const colorsMap = getColors(new TinyColor(color).toHexString());
1717
let mainColor = colorsMap['500'];
1818

1919
const colorKeys = Object.keys(colorsMap);

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

Lines changed: 2 additions & 2 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 { Color, convertToHsl } from '@vben/utils';
12+
import { convertToHsl, TinyColor } from '@vben/utils';
1313
1414
defineOptions({
1515
name: 'PreferenceBuiltinTheme',
@@ -22,7 +22,7 @@ const modelValue = defineModel<BuiltinThemeType>({ default: 'default' });
2222
const themeColorPrimary = defineModel<string>('themeColorPrimary');
2323
2424
const inputValue = computed(() => {
25-
return new Color(themeColorPrimary.value || '').toHexString();
25+
return new TinyColor(themeColorPrimary.value || '').toHexString();
2626
});
2727
2828
const builtinThemePresets = computed(() => {

packages/effects/layouts/src/widgets/preferences/preferences-sheet.vue

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,6 @@ async function handleReset() {
277277
v-model:theme-semi-dark-menu="themeSemiDarkMenu"
278278
/>
279279
</Block>
280-
<!-- <Block :title="$t('preferences.theme-color')">
281-
<ThemeColor
282-
v-model="themeColorPrimary"
283-
:color-primary-presets="colorPrimaryPresets"
284-
/>
285-
</Block> -->
286280
<Block :title="$t('preferences.theme.builtin.title')">
287281
<BuiltinTheme
288282
v-model="themeBuiltinType"

pnpm-lock.yaml

Lines changed: 3 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)