Skip to content

Commit 5aa8b06

Browse files
committed
feat: 图片反转颜色功能
1 parent 9beffdf commit 5aa8b06

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

app/src/core/service/controlService/shortcutKeysEngine/shortcutKeysRegister.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { PenStrokeMethods } from "@/core/stage/stageManager/basicMethods/PenStro
1111
import { ConnectableEntity } from "@/core/stage/stageObject/abstract/ConnectableEntity";
1212
import { MultiTargetUndirectedEdge } from "@/core/stage/stageObject/association/MutiTargetUndirectedEdge";
1313
import { CollisionBox } from "@/core/stage/stageObject/collisionBox/collisionBox";
14-
import { TextNode } from "@/core/stage/stageObject/entity/TextNode";
1514
import { activeProjectAtom, store } from "@/state";
1615
// import ColorWindow from "@/sub/ColorWindow";
1716
import FindWindow from "@/sub/FindWindow";
@@ -25,6 +24,8 @@ import { Rectangle } from "@graphif/shapes";
2524
import { toast } from "sonner";
2625
import { v4 } from "uuid";
2726
import { onNewDraft, onOpenFile } from "../../GlobalMenu";
27+
import { ImageNode } from "@/core/stage/stageObject/entity/ImageNode";
28+
import { TextNode } from "@/core/stage/stageObject/entity/TextNode";
2829

2930
/**
3031
* 快捷键注册函数
@@ -581,7 +582,7 @@ export class KeyBindsRegistrar {
581582

582583
// 做计划的功能
583584
await this.project.keyBinds.create("toggleCheckmarkOnTextNodes", "o k k", () => {
584-
const selectedTextNodes = this.project.stageManager
585+
const selectedTextNodes: TextNode[] = this.project.stageManager
585586
.getSelectedEntities()
586587
.filter((node) => node instanceof TextNode);
587588
for (const node of selectedTextNodes) {
@@ -596,6 +597,19 @@ export class KeyBindsRegistrar {
596597
this.project.stageManager.updateReferences();
597598
});
598599

600+
// 反转选中图片的颜色
601+
await this.project.keyBinds.create("reverseImageColors", "r r r", () => {
602+
const selectedImageNodes: ImageNode[] = this.project.stageManager
603+
.getSelectedEntities()
604+
.filter((node) => node instanceof ImageNode);
605+
for (const node of selectedImageNodes) {
606+
node.reverseColors();
607+
}
608+
if (selectedImageNodes.length > 0) {
609+
toast(`已反转 ${selectedImageNodes.length} 张图片的颜色`);
610+
}
611+
});
612+
599613
// 主题切换相关功能
600614
// 创建一个可以在非React环境中使用的setter函数
601615
const setTheme = (theme: string) => {

app/src/core/stage/stageObject/entity/ImageNode.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,46 @@ export class ImageNode extends ConnectableEntity {
120120
this.collisionBox.shapes[0] = newRectangle;
121121
this.updateFatherSectionByMove();
122122
}
123+
124+
/**
125+
* 反转图片颜色
126+
* 将图片的RGB值转换为互补色(255-R, 255-G, 255-B)
127+
*/
128+
reverseColors() {
129+
if (!this.bitmap) return;
130+
131+
// 创建临时canvas
132+
const canvas = document.createElement("canvas");
133+
const ctx = canvas.getContext("2d");
134+
if (!ctx) return;
135+
136+
// 设置canvas尺寸
137+
canvas.width = this.bitmap.width;
138+
canvas.height = this.bitmap.height;
139+
140+
// 绘制原图
141+
ctx.drawImage(this.bitmap, 0, 0);
142+
143+
// 获取图像数据
144+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
145+
const data = imageData.data;
146+
147+
// 反转颜色(255-R, 255-G, 255-B)
148+
for (let i = 0; i < data.length; i += 4) {
149+
data[i] = 255 - data[i]; // R
150+
data[i + 1] = 255 - data[i + 1]; // G
151+
data[i + 2] = 255 - data[i + 2]; // B
152+
// data[i + 3] 保持不变(alpha通道)
153+
}
154+
155+
// 将修改后的图像数据绘制回canvas
156+
ctx.putImageData(imageData, 0, 0);
157+
158+
// 创建新的ImageBitmap
159+
createImageBitmap(imageData).then((newBitmap) => {
160+
this.bitmap = newBitmap;
161+
// 记录操作历史
162+
this.project.historyManager.recordStep();
163+
});
164+
}
123165
}

app/src/locales/en.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,3 +1302,8 @@ visualSettingsGroup:
13021302
title: Basic Settings
13031303
background:
13041304
title: Background Settings
1305+
1306+
# Keybinds translation
1307+
reverseImageColors:
1308+
title: Reverse Image Colors
1309+
description: Reverse the colors of selected images (makes white background black and vice versa)

app/src/locales/zh_CN.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,5 +1305,10 @@ keyBinds:
13051305
title: 将选中的多个文本节点,挼ruá (合并)成一个文本节点,颜色也会取平均值
13061306
description: 仅对文本节点生效,顺序按从上到下排列,节点的位置按节点矩形左上角顶点坐标为准
13071307
swapTextAndDetails:
1308-
title: 详略交换
1309-
description: 将所有选中的文本节点的详细信息和实际内容进行交换,连按5次e,主要用于直接粘贴进来的文本内容想写入详细信息
1308+
title: 详略交换
1309+
description: 将所有选中的文本节点的详细信息和实际内容进行交换,连按5次e,主要用于直接粘贴进来的文本内容想写入详细信息
1310+
1311+
# 快捷键翻译
1312+
reverseImageColors:
1313+
title: 反转图片颜色
1314+
description: 反转选中图片的颜色(将白色背景变为黑色,反之亦然)

app/src/sub/SettingsWindow/keybinds.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
Brush,
1919
FileQuestion,
2020
Fullscreen,
21+
Image,
2122
Keyboard,
2223
MousePointer,
2324
Move,
@@ -397,4 +398,5 @@ const shortcutKeysGroups: ShortcutKeysGroup[] = [
397398
"alignTopToBottomNoSpace",
398399
],
399400
},
401+
{ title: "image", icon: <Image />, keys: ["reverseImageColors"] },
400402
];

0 commit comments

Comments
 (0)