|
1 | 1 | (() => { |
2 | 2 | const STORAGE_KEY = "qrcraft_palette_v1"; |
| 3 | + const STYLE_TAG_ID = "qrcraft-theme-palette-style"; |
3 | 4 |
|
4 | 5 | const presets = [ |
5 | 6 | { |
|
45 | 46 | return typeof s === "string" && /^#[0-9a-fA-F]{6}$/.test(s); |
46 | 47 | } |
47 | 48 |
|
| 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 | + |
48 | 71 | 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;`); |
54 | 84 | } |
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;`); |
58 | 88 | } |
| 89 | + lines.push("}"); |
| 90 | + |
| 91 | + const style = ensureStyleTag(); |
| 92 | + style.textContent = lines.join("\n"); |
59 | 93 | } |
60 | 94 |
|
61 | 95 | function applyFromStorage() { |
|
254 | 288 |
|
255 | 289 | resetBtn.addEventListener("click", () => { |
256 | 290 | 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(); |
262 | 292 | primaryInput.value = "#6750A4"; |
263 | 293 | accentInput.value = "#EFB8C8"; |
264 | 294 | }); |
|
0 commit comments