2025-10-18 21:05:34 +03:00
|
|
|
|
import {readCssVar} from "../utils/css-var";
|
|
|
|
|
|
import Color, { ColorInstance } from "color";
|
|
|
|
|
|
|
2024-07-25 00:01:39 +03:00
|
|
|
|
const registeredClasses = new Set<string>();
|
2022-09-25 14:19:30 +02:00
|
|
|
|
|
2025-10-18 21:05:34 +03:00
|
|
|
|
// Read the color lightness limits defined in the theme as CSS variables
|
|
|
|
|
|
|
|
|
|
|
|
const lightThemeColorMaxLightness = readCssVar(
|
|
|
|
|
|
document.documentElement,
|
|
|
|
|
|
"tree-item-light-theme-max-color-lightness"
|
|
|
|
|
|
).asNumber(70);
|
|
|
|
|
|
|
|
|
|
|
|
const darkThemeColorMinLightness = readCssVar(
|
|
|
|
|
|
document.documentElement,
|
|
|
|
|
|
"tree-item-dark-theme-min-color-lightness"
|
|
|
|
|
|
).asNumber(50);
|
|
|
|
|
|
|
2025-10-18 23:52:43 +03:00
|
|
|
|
function createClassForColor(colorString: string | null) {
|
|
|
|
|
|
if (!colorString?.trim()) {
|
2022-09-25 14:19:30 +02:00
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-18 23:52:43 +03:00
|
|
|
|
const color = parseColor(colorString);
|
|
|
|
|
|
if (!color) {
|
|
|
|
|
|
return;
|
2022-09-25 14:19:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-18 23:52:43 +03:00
|
|
|
|
const className = `color-${color.hex().substring(1)}`;
|
|
|
|
|
|
const adjustedColor = adjustColorLightness(color, lightThemeColorMaxLightness!,
|
|
|
|
|
|
darkThemeColorMinLightness!);
|
2025-10-18 21:05:34 +03:00
|
|
|
|
|
2022-09-25 14:19:30 +02:00
|
|
|
|
if (!registeredClasses.has(className)) {
|
2025-10-18 21:05:34 +03:00
|
|
|
|
$("head").append(`<style>
|
|
|
|
|
|
.${className}, span.fancytree-active.${className} {
|
|
|
|
|
|
--light-theme-custom-color: ${adjustedColor.lightThemeColor};
|
|
|
|
|
|
--dark-theme-custom-color: ${adjustedColor.darkThemeColor}
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>`);
|
2022-09-25 14:19:30 +02:00
|
|
|
|
|
|
|
|
|
|
registeredClasses.add(className);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return className;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-18 23:52:43 +03:00
|
|
|
|
function parseColor(color: string) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// Parse the given color in the CIELAB color space
|
|
|
|
|
|
return Color(color);
|
|
|
|
|
|
} catch (ex) {
|
2025-10-19 00:02:22 +03:00
|
|
|
|
console.error(ex);
|
2025-10-18 23:52:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-18 21:05:34 +03:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns a pair of colors — one optimized for light themes and the other for dark themes, derived
|
|
|
|
|
|
* from the specified color to maintain sufficient contrast with each theme.
|
|
|
|
|
|
* The adjustment is performed by limiting the color’s lightness in the CIELAB color space,
|
|
|
|
|
|
* according to the lightThemeMaxLightness and darkThemeMinLightness parameters.
|
|
|
|
|
|
*/
|
2025-10-18 23:52:43 +03:00
|
|
|
|
function adjustColorLightness(color: ColorInstance, lightThemeMaxLightness: number, darkThemeMinLightness: number) {
|
|
|
|
|
|
let labColor = color.lab();
|
2025-10-18 21:05:34 +03:00
|
|
|
|
|
2025-10-18 21:22:55 +03:00
|
|
|
|
const lightness = labColor.l();
|
|
|
|
|
|
|
2025-10-18 21:05:34 +03:00
|
|
|
|
// For the light theme, limit the maximum lightness
|
2025-10-18 21:22:55 +03:00
|
|
|
|
const lightThemeColor = labColor.l(Math.min(lightness, lightThemeMaxLightness)).hex();
|
|
|
|
|
|
|
|
|
|
|
|
// For the dark theme, limit the minimum lightness
|
|
|
|
|
|
const darkThemeColor = labColor.l(Math.max(lightness, darkThemeMinLightness)).hex();
|
2025-10-18 21:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
return {lightThemeColor, darkThemeColor};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-25 14:19:30 +02:00
|
|
|
|
export default {
|
|
|
|
|
|
createClassForColor
|
2025-10-18 21:05:34 +03:00
|
|
|
|
};
|