Skip to content

Commit 7d2c6f3

Browse files
committed
✨ 完善颜色管理面板,增加从节点添加颜色库、自动整理颜色库顺序按钮
1 parent 218866f commit 7d2c6f3

File tree

4 files changed

+120
-12
lines changed

4 files changed

+120
-12
lines changed

app/src/core/dataStruct/Color.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,39 @@ export class Color {
155155
return { h: h * 360, s, l };
156156
}
157157

158+
/**
159+
* 计算颜色的色相
160+
* @param color
161+
* @returns 色相值(0-360)
162+
*/
163+
public static getHue(color: Color): number {
164+
const r = color.r / 255;
165+
const g = color.g / 255;
166+
const b = color.b / 255;
167+
168+
const max = Math.max(r, g, b);
169+
const min = Math.min(r, g, b);
170+
let hue = 0;
171+
172+
if (max === min) {
173+
hue = 0; // achromatic
174+
} else {
175+
const diff = max - min;
176+
if (max === r) {
177+
hue = ((g - b) / diff) % 6;
178+
} else if (max === g) {
179+
hue = (b - r) / diff + 2;
180+
} else if (max === b) {
181+
hue = (r - g) / diff + 4;
182+
}
183+
hue = Math.round(hue * 60);
184+
if (hue < 0) {
185+
hue += 360;
186+
}
187+
}
188+
return hue;
189+
}
190+
158191
// 辅助方法:HSL转RGB
159192
private hslToRgb(hsl: { h: number; s: number; l: number }): { r: number; g: number; b: number } {
160193
let k, r, g, b;

app/src/core/service/feedbackService/ColorManager.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,31 @@ export namespace ColorManager {
8484
}
8585
return false;
8686
}
87+
/**
88+
* 按照色相环的顺序整理用户实体填充颜色
89+
*/
90+
export async function organizeUserEntityFillColors() {
91+
const colors = await getUserEntityFillColors();
92+
const sortedColors = sortColorsByHue(colors);
93+
await store.set("entityFillColors", colorToColorData(sortedColors));
94+
store.save();
95+
}
96+
97+
/**
98+
* 按照色相环的顺序排序颜色
99+
* @param colors
100+
*/
101+
function sortColorsByHue(colors: Color[]): Color[] {
102+
return colors.sort((a, b) => getColorHue(a) - getColorHue(b));
103+
}
104+
/**
105+
* 计算颜色的色相
106+
* @param color
107+
* @returns 色相值(0-360)
108+
*/
109+
function getColorHue(color: Color): number {
110+
return Color.getHue(color);
111+
}
87112
}
88113
/**
89114
json数据格式

app/src/pages/_app.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ export default function App() {
348348
</IconButton>
349349
{isDesktop && (
350350
<IconButton
351+
className={cn(isWindowCollapsing && "animate-bounce")}
351352
onClick={async (e) => {
352353
e.stopPropagation();
353354
// const size = await getCurrentWindow().outerSize();

app/src/pages/_color_manager_panel.tsx

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import Button from "../components/Button";
33
import { Dialog } from "../components/dialog";
44
import { Color } from "../core/dataStruct/Color";
55
import { ColorManager } from "../core/service/feedbackService/ColorManager";
6+
import { StageManager } from "../core/stage/stageManager/StageManager";
7+
import { TextNode } from "../core/stage/stageObject/entity/TextNode";
8+
import { Section } from "../core/stage/stageObject/entity/Section";
9+
import { LineEdge } from "../core/stage/stageObject/association/LineEdge";
610

711
/**
812
* 自定义颜色设置面板
@@ -19,15 +23,29 @@ export default function ColorManagerPanel() {
1923
const [currentColorList, setCurrentColorList] = useState<Color[]>([]);
2024

2125
return (
22-
<div className="h-96 w-96 bg-neutral-700 p-4">
26+
<div className="bg-panel-bg h-96 w-96 p-4">
2327
<div>
24-
<p>当前颜色</p>
28+
<p>我的颜色库</p>
2529
{/* <ColorDotElement color={Color.Red} /> */}
2630
<div className="flex flex-wrap items-center justify-center">
2731
{currentColorList.map((color) => (
28-
<ColorDotElement key={color.toString()} color={color} />
32+
<ColorDotElement
33+
key={color.toString()}
34+
color={color}
35+
onclick={() => {
36+
console.log(color);
37+
const rgbSharpString = color.toHexString();
38+
if (rgbSharpString.length === 9) {
39+
// 去掉透明度
40+
setPreAddColor(rgbSharpString.slice(0, 7));
41+
}
42+
}}
43+
/>
2944
))}
3045
</div>
46+
{currentColorList.length !== 0 && (
47+
<div className="text-panel-details-text text-center text-xs">提示:点击颜色可以复制颜色值到待添加颜色</div>
48+
)}
3149
</div>
3250
<div>
3351
<p>添加颜色:</p>
@@ -60,25 +78,56 @@ export default function ColorManagerPanel() {
6078
>
6179
确认添加
6280
</Button>
81+
<Button
82+
onClick={() => {
83+
StageManager.getSelectedStageObjects().forEach((stageObject) => {
84+
if (stageObject instanceof TextNode) {
85+
ColorManager.addUserEntityFillColor(stageObject.color);
86+
} else if (stageObject instanceof Section) {
87+
ColorManager.addUserEntityFillColor(stageObject.color);
88+
} else if (stageObject instanceof LineEdge) {
89+
ColorManager.addUserEntityFillColor(stageObject.color);
90+
}
91+
});
92+
}}
93+
>
94+
将选中的节点颜色添加到库
95+
</Button>
96+
<Button
97+
onClick={() => {
98+
ColorManager.organizeUserEntityFillColors();
99+
}}
100+
>
101+
一键整理颜色库
102+
</Button>
63103
</div>
64104
</div>
65105
);
66106
}
67-
function ColorDotElement({ color }: { color: Color }) {
107+
108+
function ColorDotElement({ color, onclick }: { color: Color; onclick: (e: any) => void }) {
68109
const r = color.r;
69110
const g = color.g;
70111
const b = color.b;
71112
const a = color.a;
72113
return (
73-
<div className="relative m-1 h-8 w-8 rounded-full" style={{ backgroundColor: `rgba(${r}, ${g}, ${b}, ${a})` }}>
74-
<Button
75-
className="absolute -right-2 -top-2 h-2 w-2 rounded-full text-xs"
76-
onClick={() => {
77-
ColorManager.removeUserEntityFillColor(color);
78-
}}
114+
<div className="my-1">
115+
<div
116+
className="relative mx-1 h-8 min-w-8 rounded-full hover:cursor-pointer"
117+
style={{ backgroundColor: `rgba(${r}, ${g}, ${b}, ${a})` }}
118+
onClick={onclick}
79119
>
80-
x
81-
</Button>
120+
<Button
121+
className="absolute -right-2 -top-2 h-2 w-2 rounded-full text-xs"
122+
onClick={() => {
123+
ColorManager.removeUserEntityFillColor(color);
124+
}}
125+
tooltip="删除"
126+
>
127+
x
128+
</Button>
129+
</div>
130+
<span className="mx-0.5 cursor-text select-all rounded bg-black px-1 text-xs text-neutral-300">{`${r}, ${g}, ${b}`}</span>
82131
</div>
83132
);
84133
}

0 commit comments

Comments
 (0)