Skip to content

Commit aa40baf

Browse files
author
committed
Deployed 2dff8d9 with MkDocs version: 1.6.1
1 parent 2ae9dda commit aa40baf

1 file changed

Lines changed: 43 additions & 13 deletions

File tree

assets/javascripts/theme-palette.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(() => {
22
const STORAGE_KEY = "qrcraft_palette_v1";
3+
const STYLE_TAG_ID = "qrcraft-theme-palette-style";
34

45
const presets = [
56
{
@@ -45,17 +46,50 @@
4546
return typeof s === "string" && /^#[0-9a-fA-F]{6}$/.test(s);
4647
}
4748

49+
function hexToRgb(hex) {
50+
if (!isHexColor(hex)) return null;
51+
const r = parseInt(hex.slice(1, 3), 16);
52+
const g = parseInt(hex.slice(3, 5), 16);
53+
const b = parseInt(hex.slice(5, 7), 16);
54+
return { r, g, b };
55+
}
56+
57+
function ensureStyleTag() {
58+
const existing = document.getElementById(STYLE_TAG_ID);
59+
if (existing instanceof HTMLStyleElement) return existing;
60+
const style = document.createElement("style");
61+
style.id = STYLE_TAG_ID;
62+
document.head.appendChild(style);
63+
return style;
64+
}
65+
66+
function clearStyleTag() {
67+
const el = document.getElementById(STYLE_TAG_ID);
68+
if (el && el.parentNode) el.parentNode.removeChild(el);
69+
}
70+
4871
function applyColors(primary, accent) {
49-
const root = document.documentElement;
50-
if (isHexColor(primary)) {
51-
root.style.setProperty("--md-primary-fg-color", primary);
52-
root.style.setProperty("--md-primary-fg-color--light", primary);
53-
root.style.setProperty("--md-primary-fg-color--dark", primary);
72+
const p = isHexColor(primary) ? primary : null;
73+
const a = isHexColor(accent) ? accent : null;
74+
const pRgb = p ? hexToRgb(p) : null;
75+
const aRgb = a ? hexToRgb(a) : null;
76+
77+
const lines = [];
78+
lines.push(":root {");
79+
if (p) {
80+
lines.push(` --md-primary-fg-color: ${p} !important;`);
81+
lines.push(` --md-primary-fg-color--light: ${p} !important;`);
82+
lines.push(` --md-primary-fg-color--dark: ${p} !important;`);
83+
if (pRgb) lines.push(` --md-primary-fg-color--rgb: ${pRgb.r}, ${pRgb.g}, ${pRgb.b} !important;`);
5484
}
55-
if (isHexColor(accent)) {
56-
root.style.setProperty("--md-accent-fg-color", accent);
57-
root.style.setProperty("--md-accent-fg-color--transparent", `${accent}22`);
85+
if (a) {
86+
lines.push(` --md-accent-fg-color: ${a} !important;`);
87+
if (aRgb) lines.push(` --md-accent-fg-color--rgb: ${aRgb.r}, ${aRgb.g}, ${aRgb.b} !important;`);
5888
}
89+
lines.push("}");
90+
91+
const style = ensureStyleTag();
92+
style.textContent = lines.join("\n");
5993
}
6094

6195
function applyFromStorage() {
@@ -254,11 +288,7 @@
254288

255289
resetBtn.addEventListener("click", () => {
256290
localStorage.removeItem(STORAGE_KEY);
257-
document.documentElement.style.removeProperty("--md-primary-fg-color");
258-
document.documentElement.style.removeProperty("--md-primary-fg-color--light");
259-
document.documentElement.style.removeProperty("--md-primary-fg-color--dark");
260-
document.documentElement.style.removeProperty("--md-accent-fg-color");
261-
document.documentElement.style.removeProperty("--md-accent-fg-color--transparent");
291+
clearStyleTag();
262292
primaryInput.value = "#6750A4";
263293
accentInput.value = "#EFB8C8";
264294
});

0 commit comments

Comments
 (0)