Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Display OKLCH colors correctly in the built-in color manager https://github.com/siyuan-note/siyuan/issues/18966

Daniel54a2b8e6f251
diff --git a/app/src/protyle/toolbar/builtinColorDialog.ts b/app/src/protyle/toolbar/builtinColorDialog.ts
index cf8f9277be..4a6796007e 100644
--- a/app/src/protyle/toolbar/builtinColorDialog.ts
+++ b/app/src/protyle/toolbar/builtinColorDialog.ts
@@ -67,17 +67,29 @@ const getEntryLabel = (type: TBuiltinColorType, key: number | TBuiltinInlineStyl
return `${getTypeLabel(type)} ${key}`;
};
+const colorCanvas = document.createElement("canvas");
+colorCanvas.width = 1;
+colorCanvas.height = 1;
+const colorContext = colorCanvas.getContext("2d", {willReadFrequently: true});
+
const colorToHex = (value: string, fallback: string) => {
const normalized = value.trim();
if (/^#[0-9a-f]{6}$/i.test(normalized)) {
return normalized.toLowerCase();
}
const match = normalized.match(/^rgba?\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)/i);
- if (!match) {
+ if (match) {
+ return `#${match.slice(1, 4).map(item => Math.max(0, Math.min(255, Math.round(Number(item))))
+ .toString(16).padStart(2, "0")).join("")}`;
+ }
+ if (!colorContext || !CSS.supports("color", normalized)) {
return fallback;
}
- return `#${match.slice(1, 4).map(item => Math.max(0, Math.min(255, Math.round(Number(item))))
- .toString(16).padStart(2, "0")).join("")}`;
+ colorContext.clearRect(0, 0, 1, 1);
+ colorContext.fillStyle = normalized;
+ colorContext.fillRect(0, 0, 1, 1);
+ const color = colorContext.getImageData(0, 0, 1, 1).data;
+ return `#${Array.from(color.slice(0, 3)).map(item => item.toString(16).padStart(2, "0")).join("")}`;
};
const resolveThemeColor = (value: string, fallback: string) => {