diff --git a/manifest-beta.json b/manifest-beta.json index adaca01b..577e4cee 100644 --- a/manifest-beta.json +++ b/manifest-beta.json @@ -1,7 +1,7 @@ { "id": "obsidian-excalidraw-plugin", "name": "Excalidraw", - "version": "2.18.0-beta-3", + "version": "2.18.0-beta-4", "minAppVersion": "1.5.7", "description": "An Obsidian plugin to edit and view Excalidraw drawings", "author": "Zsolt Viczian", diff --git a/package.json b/package.json index 6b052226..0aa6aebc 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "license": "MIT", "dependencies": { "@popperjs/core": "^2.11.8", - "@zsviczian/excalidraw": "0.18.0-56", + "@zsviczian/excalidraw": "0.18.0-58", "chroma-js": "^3.1.2", "clsx": "^2.0.0", "@zsviczian/colormaster": "^1.2.2", diff --git a/src/core/main.ts b/src/core/main.ts index 11101698..2946d539 100644 --- a/src/core/main.ts +++ b/src/core/main.ts @@ -90,6 +90,8 @@ import { CommandManager } from "./managers/CommandManager"; import { EventManager } from "./managers/EventManager"; import { UniversalInsertFileModal } from "src/shared/Dialogs/UniversalInsertFileModal"; import en from "src/lang/locale/en"; +import { get } from "http"; +import { getHighlightColor } from "src/utils/dynamicStyling"; declare const PLUGIN_VERSION:string; declare const INITIAL_TIMESTAMP: number; @@ -1486,4 +1488,8 @@ export default class ExcalidrawPlugin extends Plugin { public getObsidianDevice(): DeviceType { return DEVICE; } + + public getHighlightColor(color: string, sceneBgColor: string, opacity: number = 1): string { + return getHighlightColor(this.ea, color, sceneBgColor, opacity); + } } diff --git a/src/lang/locale/en.ts b/src/lang/locale/en.ts index 7362ab6a..5535c0bc 100644 --- a/src/lang/locale/en.ts +++ b/src/lang/locale/en.ts @@ -509,8 +509,7 @@ export default { "Toggle ON:Change grid color to match the canvas color
Toggle OFF:Use the color below as the grid color", GRID_COLOR_NAME: "Grid color", GRID_OPACITY_NAME: "Grid opacity", - GRID_OPACITY_DESC: "Grid opacity will also control the opacity of the binding box when binding an arrow to an element.
" + - "Set the opacity of the grid. 0 is transparent, 100 is opaque.", + GRID_OPACITY_DESC: "Sets the opacity of the grid. 0 is transparent, 100 is opaque.", GRID_DIRECTION_NAME: "Grid direction", GRID_DIRECTION_DESC: "The first toggle shows/hides the horizontal grid, the second toggle shows/hides the vertical grid.", GRID_HORIZONTAL: "Render horizontal grid", diff --git a/src/utils/dynamicStyling.ts b/src/utils/dynamicStyling.ts index 143c2e8b..2f6bdf01 100644 --- a/src/utils/dynamicStyling.ts +++ b/src/utils/dynamicStyling.ts @@ -1,5 +1,5 @@ import { ExcalidrawImperativeAPI } from "@zsviczian/excalidraw/types/excalidraw/types"; -import { ColorMaster } from "@zsviczian/colormaster"; +import CM, { ColorMaster } from "@zsviczian/colormaster"; import { ExcalidrawAutomate } from "src/shared/ExcalidrawAutomate"; import ExcalidrawView from "src/view/ExcalidrawView"; import { DynamicStyle } from "src/types/types"; @@ -190,4 +190,44 @@ export const setDynamicStyle = ( color = null; dynamicStyle = null; }); -} \ No newline at end of file +} + +const colorsCache: Map = new Map(); + +export function getHighlightColor( + ea: ExcalidrawAutomate, + strokeColor: string, + sceneBgColor: string, + opacity:number = 1 +): string { + strokeColor = strokeColor === "transparent" + ? (sceneBgColor==="transparent" ? "#ffffff": sceneBgColor) + : strokeColor; + + let contrasted = colorsCache.get(strokeColor); + if(!contrasted) { + const bg = ea.getCM(strokeColor); + const bgMix = ea.getCM(strokeColor); + const isDark = bg.isDark(); + + const step = 15; + const candidates = [ + CM((isDark ? bg.lighterBy(step) : bg.darkerBy(step)).stringHEX()), + CM((isDark ? bg.lighterBy(step) : bg.darkerBy(step)).stringHEX()), + isDark ? bg.lighterBy(step) : bg.darkerBy(step), + // Vibrancy boost by mixing toward the opposite theme color + bgMix.mix({color: ea.getCM(isDark ? "#ffffff" : "#000000"), ratio:0.35}), + ea.getCM(isDark ? "#ffffff" : "#000000"), + ea.getCM(isDark ? "#000000" : "#ffffff"), + ]; + + const desiredContrast = 2.5; + contrasted = candidates.find(color => + color.contrast({bgColor: strokeColor, ratio:false}) as number >= desiredContrast) ?? + candidates.reduce((best, color) => (color.contrast({bgColor: strokeColor, ratio: false}) > best.contrast({bgColor: strokeColor, ratio:false}) ? color : best) + ); + colorsCache.set(strokeColor, contrasted); + } + contrasted.alphaTo(opacity); + return contrasted.stringRGB({alpha:true}); + } \ No newline at end of file diff --git a/src/view/ExcalidrawView.ts b/src/view/ExcalidrawView.ts index 0c64c932..2d91c100 100644 --- a/src/view/ExcalidrawView.ts +++ b/src/view/ExcalidrawView.ts @@ -2586,7 +2586,7 @@ export default class ExcalidrawView extends TextFileView implements HoverParent{ const opacity = this.plugin.settings.gridSettings.OPACITY/100; if (this.plugin.settings.gridSettings.DYNAMIC_COLOR) { - // Dynamic color: concatenate opacity to the HEX string + // Dynamic color: concatenate opacity to the RGB string !!! Excalidraw expects an RGBA string !!! Regular = (isDark ? cm.lighterBy(10) : cm.darkerBy(10)).alphaTo(opacity).stringRGB({ alpha: true }); Bold = (isDark ? cm.lighterBy(5) : cm.darkerBy(5)).alphaTo(opacity).stringRGB({ alpha: true }); } else { @@ -2597,7 +2597,7 @@ export default class ExcalidrawView extends TextFileView implements HoverParent{ // Regular uses the custom color directly Regular = customCM.alphaTo(opacity).stringRGB({ alpha: true }); - // Bold is 7 shades lighter or darker based on the custom color's darkness + // Bold is 10 shades lighter or darker based on the custom color's darkness Bold = (customIsDark ? customCM.lighterBy(10) : customCM.darkerBy(10)).alphaTo(opacity).stringRGB({ alpha: true }); } @@ -4049,7 +4049,12 @@ export default class ExcalidrawView extends TextFileView implements HoverParent{ st = (this.excalidrawAPI as ExcalidrawImperativeAPI).getAppState(); canvasColor = canvasColor ?? st.viewBackgroundColor === "transparent" ? "white" : st.viewBackgroundColor; } - window.setTimeout(()=>this.updateScene({appState:{gridColor: this.getGridColor(canvasColor, st)}, captureUpdate: CaptureUpdateAction.NEVER})); + window.setTimeout(()=>this.updateScene( + { + appState:{ + gridColor: this.getGridColor(canvasColor, st), + }, + captureUpdate: CaptureUpdateAction.NEVER})); } public updateGridDirection(gridDirection: {horizontal: boolean, vertical: boolean}) {