1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| if (GLOBAL_CONFIG.mainTone.mode == "both") { // both继续请求 try { const response = await fetch(GLOBAL_CONFIG.mainTone.api + path); if (response.ok && response.headers.get("content-type")?.includes("application/json")) { const obj = await response.json(); let value = obj.RGB;
if (getContrastYIQ(value) value = LightenDarkenColor(colorHex(value), -40); }
root.style.setProperty("--anzhiyu-bar-background", value); requestAnimationFrame(() => { anzhiyu.initThemeColor(); });
if (GLOBAL_CONFIG.mainTone.cover_change) { document.documentElement.style.setProperty("--anzhiyu-main", value); document.documentElement.style.setProperty( "--anzhiyu-theme-op", getComputedStyle(document.documentElement).getPropertyValue("--anzhiyu-main") + "23" ); document.documentElement.style.setProperty( "--anzhiyu-theme-op-deep", getComputedStyle(document.documentElement).getPropertyValue("--anzhiyu-main") + "dd" ); } } else { root.style.setProperty("--anzhiyu-bar-background", fallbackValue); requestAnimationFrame(() => { anzhiyu.initThemeColor(); }); document.documentElement.style.setProperty("--anzhiyu-main", fallbackValue); } } catch { root.style.setProperty("--anzhiyu-bar-background", fallbackValue); requestAnimationFrame(() => { anzhiyu.initThemeColor(); }); document.documentElement.style.setProperty("--anzhiyu-main", fallbackValue); } } else { - root.style.setProperty("--anzhiyu-bar-background", fallbackValue); - requestAnimationFrame(() => { - anzhiyu.initThemeColor(); - }); - document.documentElement.style.setProperty("--anzhiyu-main", fallbackValue); + const colorThief = new ColorThief(); + const img = new Image(); + img.crossOrigin = "Anonymous"; + img.src = path; + img.onload = () => { + let ItemColor = 'rgb(' + colorThief.getColor(img) + ')'; + ItemColor = colorHex(ItemColor); + if (getContrastYIQ(ItemColor) === "light") { + ItemColor = LightenDarkenColor(colorHex(ItemColor), -40); + } + root.style.setProperty("--anzhiyu-bar-background", ItemColor); + requestAnimationFrame(() => { + anzhiyu.initThemeColor(); + }); + if (GLOBAL_CONFIG.mainTone.cover_change) { + document.documentElement.style.setProperty("--anzhiyu-main", ItemColor); + document.documentElement.style.setProperty( + "--anzhiyu-theme-op", + getComputedStyle(document.documentElement).getPropertyValue("--anzhiyu-main") + "23" + ); + document.documentElement.style.setProperty( + "--anzhiyu-theme-op-deep", + getComputedStyle(document.documentElement).getPropertyValue("--anzhiyu-main") + "dd" + ); + } + // console.info(ItemColor); + } }
|