Skip to content

Commit 54f6fee

Browse files
committed
feat(web): 添加颜色类型
1 parent cdf6ee1 commit 54f6fee

File tree

9 files changed

+110
-73
lines changed

9 files changed

+110
-73
lines changed

packages/hexon-web/.storybook/preview.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ThemeProvider } from "../src/lib/theme";
2-
import { blueTheme } from "../src/constants";
3-
import "../src/reset.less";
2+
import { blueTheme } from "../src/themes";
3+
import "../src/styles/reset.less";
44
export const parameters = {
55
actions: { argTypesRegex: "^on[A-Z].*" },
66
controls: {

packages/hexon-web/src/App.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import account from "./account";
33
import { useAccountProvider } from "./lib/account";
4-
import { blueTheme } from "./constants";
4+
import { blueTheme } from "./themes";
55
import { ThemeProvider, useThemeSwitcherProvider } from "./lib/theme";
66
77
const switcher = useThemeSwitcherProvider("blue", blueTheme);
@@ -16,5 +16,5 @@ useAccountProvider(account);
1616
</template>
1717

1818
<style lang="less">
19-
@import "./reset.less";
19+
@import "./styles/reset.less";
2020
</style>

packages/hexon-web/src/components/HButton.stories.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import HIcon from "./HIcon.vue";
4949
{() => ({
5050
components: { HButton },
5151
template: `
52-
<div style="padding:20px; background-color: gray;">
52+
<div style="padding:20px;">
5353
<HButton type="primary" inverted>primary</HButton>&nbsp;
5454
<HButton type="success" inverted>success</HButton>&nbsp;
5555
<HButton type="warning" inverted>warning</HButton>&nbsp;

packages/hexon-web/src/components/HButton.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ button {
6363
}
6464
&.primary {
6565
background-color: var(--color-primary-0);
66-
color: var(--color-white);
66+
color: var(--color-foreground-9);
6767
&:hover {
6868
background-color: var(--color-primary-l2);
6969
}
@@ -83,7 +83,7 @@ button {
8383
}
8484
&.success {
8585
background-color: var(--color-success-0);
86-
color: var(--color-white);
86+
color: var(--color-foreground-9);
8787
&:hover {
8888
background-color: var(--color-success-l2);
8989
}
@@ -103,7 +103,7 @@ button {
103103
}
104104
&.warning {
105105
background-color: var(--color-warning-0);
106-
color: var(--color-white);
106+
color: var(--color-foreground-9);
107107
&:hover {
108108
background-color: var(--color-warning-l2);
109109
}
@@ -123,7 +123,7 @@ button {
123123
}
124124
&.error {
125125
background-color: var(--color-error-0);
126-
color: var(--color-white);
126+
color: var(--color-foreground-9);
127127
&:hover {
128128
background-color: var(--color-error-l2);
129129
}

packages/hexon-web/src/components/HIcon.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { computed, toRefs } from "@vue/reactivity";
33
import { Icon } from "@vicons/utils";
44
55
const props = withDefaults(
6-
defineProps<{ size?: string | number; clickable: boolean }>(),
6+
defineProps<{ size?: string | number; clickable?: boolean }>(),
77
{
88
size: "1em",
99
clickable: false,

packages/hexon-web/src/constants.ts

-63
This file was deleted.
File renamed without changes.

packages/hexon-web/src/themes.ts

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { hex, rgb } from "color-convert";
2+
import { ITheme } from "./lib/theme";
3+
4+
type modifier = "l" | "a" | "d";
5+
type amount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
6+
7+
type ModifiedColor = {
8+
[key in `${modifier}${amount}`]: string;
9+
};
10+
11+
type modifyFn = (name: string, amt: number) => string;
12+
type modifierFnMap = { [key in modifier]: modifyFn };
13+
14+
const modifiers: modifierFnMap = {
15+
l: (name, amt) => {
16+
// 变白
17+
const [r, g, b] = hex.rgb(name);
18+
const go = (c: number) => c + Math.abs(255 - c) * amt;
19+
return `#${rgb.hex([go(r), go(g), go(b)])}`;
20+
},
21+
d: (name, amt) => {
22+
// 变黑
23+
const [r, g, b] = hex.rgb(name);
24+
const go = (c: number) => c * (1 - amt);
25+
return `#${rgb.hex([go(r), go(g), go(b)])}`;
26+
},
27+
a: (name, amt) => {
28+
// 变透明
29+
const alpha = Math.round((1 - amt) * 255).toString(16);
30+
return `${name}${alpha}`;
31+
},
32+
};
33+
34+
const createColor = (name: string) => {
35+
const indexs = new Array(9).fill(0).map((v, i) => (i + 1) / 10);
36+
function createModifiedColor(type: modifier) {
37+
return indexs
38+
.map((amt) => modifiers[type](name, amt))
39+
.reduce((o, v, idx) => {
40+
o[`${type}${(idx + 1) as amount}`] = v;
41+
return o;
42+
}, {} as ModifiedColor);
43+
}
44+
return {
45+
0: name,
46+
...createModifiedColor("a"),
47+
...createModifiedColor("d"),
48+
...createModifiedColor("l"),
49+
};
50+
};
51+
52+
type ColorPack = ITheme &
53+
ModifiedColor & {
54+
0: string;
55+
};
56+
57+
export type HTheme = {
58+
color: {
59+
primary: ColorPack;
60+
success: ColorPack;
61+
warning: ColorPack;
62+
error: ColorPack;
63+
background: {
64+
1: string;
65+
2: string;
66+
3: string;
67+
9: string;
68+
};
69+
foreground: {
70+
1: string;
71+
2: string;
72+
3: string;
73+
6: string;
74+
9: string;
75+
};
76+
};
77+
};
78+
79+
// https://flatuicolors.com/palette/defo
80+
export const blueTheme: HTheme = {
81+
color: {
82+
primary: createColor("#3498db"),
83+
success: createColor("#27ae60"),
84+
warning: createColor("#f39c12"),
85+
error: createColor("#e74c3c"),
86+
background: {
87+
1: "#ffffff",
88+
2: "#f8f8f8",
89+
3: "#eeeeee",
90+
9: "#282828",
91+
},
92+
foreground: {
93+
1: "#000000",
94+
2: "#484848",
95+
3: "#000000",
96+
6: "#9e9e9e",
97+
9: "#ffffff",
98+
},
99+
},
100+
};

0 commit comments

Comments
 (0)