mirror of
https://github.com/Vencord/Vesktop.git
synced 2026-11-26 11:56:23 -08:00
fix & improve splash themeing
Resolve colours by painting them onto a canvas instead of parsing getComputedStyle output, which now handles all formats and doesn't rely on Chromium returning a specific format Also now passes theme variables to splash via URL params instead of insertCSS to be more reliable Assisted by Claude Opus 5.5
This commit is contained in:
4 files changed
+43
-65
No files matched your search
+5
-15
@@ -23,26 +23,16 @@ export function createSplashWindow(startMinimized = false) {
|
||||
}
|
||||
});
|
||||
|
||||
loadView(splash, "splash.html");
|
||||
|
||||
const { splashBackground, splashColor, splashTheming, splashPixelated } = Settings.store;
|
||||
|
||||
const params = new URLSearchParams();
|
||||
if (splashTheming) {
|
||||
if (splashColor) {
|
||||
const semiTransparentSplashColor = splashColor.replace("rgb(", "rgba(").replace(")", ", 0.2)");
|
||||
|
||||
splash.webContents.insertCSS(`body { --fg: ${splashColor} !important }`);
|
||||
splash.webContents.insertCSS(`body { --fg-semi-trans: ${semiTransparentSplashColor} !important }`);
|
||||
}
|
||||
|
||||
if (splashBackground) {
|
||||
splash.webContents.insertCSS(`body { --bg: ${splashBackground} !important }`);
|
||||
}
|
||||
if (splashColor) params.set("fg", splashColor);
|
||||
if (splashBackground) params.set("bg", splashBackground);
|
||||
}
|
||||
if (splashPixelated) params.set("pixelated", "");
|
||||
|
||||
if (splashPixelated) {
|
||||
splash.webContents.insertCSS(`img { image-rendering: pixelated; }`);
|
||||
}
|
||||
loadView(splash, "splash.html", params);
|
||||
|
||||
return splash;
|
||||
}
|
||||
|
||||
@@ -11,3 +11,18 @@ contextBridge.exposeInMainWorld("VesktopSplashNative", {
|
||||
ipcRenderer.on("update-splash-message", (_, message: string) => callback(message));
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const params = new URLSearchParams(location.search);
|
||||
const root = document.documentElement;
|
||||
|
||||
const fg = params.get("fg");
|
||||
const bg = params.get("bg");
|
||||
|
||||
if (fg) {
|
||||
root.style.setProperty("--fg", fg);
|
||||
root.style.setProperty("--fg-semi-trans", `color-mix(in srgb, ${fg} 20%, transparent)`);
|
||||
}
|
||||
if (bg) root.style.setProperty("--bg", bg);
|
||||
if (params.has("pixelated")) root.classList.add("pixelated");
|
||||
});
|
||||
@@ -6,69 +6,38 @@
|
||||
|
||||
import { Settings } from "./settings";
|
||||
|
||||
function isValidColor(color: CSSStyleValue | undefined): color is CSSUnparsedValue & { [0]: string } {
|
||||
return color instanceof CSSUnparsedValue && typeof color[0] === "string" && CSS.supports("color", color[0]);
|
||||
}
|
||||
const ctx = document.createElement("canvas").getContext("2d", { willReadFrequently: true })!;
|
||||
ctx.canvas.width = ctx.canvas.height = 1;
|
||||
|
||||
// https://gist.github.com/earthbound19/e7fe15fdf8ca3ef814750a61bc75b5ce
|
||||
function clamp(value: number, min: number, max: number) {
|
||||
return Math.max(Math.min(value, max), min);
|
||||
}
|
||||
const linearToGamma = (c: number) => (c >= 0.0031308 ? 1.055 * Math.pow(c, 1 / 2.4) - 0.055 : 12.92 * c);
|
||||
// Resolves any css colour (including var() references and modern colour spaces) to rgb() by painting it onto a canvas,
|
||||
// so we don't depend on the serialisation format of getComputedStyle, which changes between Chromium versions
|
||||
function resolveColor(variable: string) {
|
||||
if (!getComputedStyle(document.body).getPropertyValue(variable).trim()) return null;
|
||||
|
||||
function oklabToSRGB({ L, a, b }: { L: number; a: number; b: number }) {
|
||||
let l = L + a * +0.3963377774 + b * +0.2158037573;
|
||||
let m = L + a * -0.1055613458 + b * -0.0638541728;
|
||||
let s = L + a * -0.0894841775 + b * -1.291485548;
|
||||
l **= 3;
|
||||
m **= 3;
|
||||
s **= 3;
|
||||
let R = l * +4.0767416621 + m * -3.3077115913 + s * +0.2309699292;
|
||||
let G = l * -1.2684380046 + m * +2.6097574011 + s * -0.3413193965;
|
||||
let B = l * -0.0041960863 + m * -0.7034186147 + s * +1.707614701;
|
||||
R = 255 * linearToGamma(R);
|
||||
G = 255 * linearToGamma(G);
|
||||
B = 255 * linearToGamma(B);
|
||||
R = Math.round(clamp(R, 0, 255));
|
||||
G = Math.round(clamp(G, 0, 255));
|
||||
B = Math.round(clamp(B, 0, 255));
|
||||
|
||||
return `rgb(${R}, ${G}, ${B})`;
|
||||
}
|
||||
|
||||
function resolveColor(color: string) {
|
||||
const span = document.createElement("span");
|
||||
span.style.color = color;
|
||||
span.style.color = `var(${variable})`;
|
||||
span.style.display = "none";
|
||||
|
||||
document.body.append(span);
|
||||
let rgbColor = getComputedStyle(span).color;
|
||||
const computedColor = getComputedStyle(span).color;
|
||||
span.remove();
|
||||
|
||||
if (rgbColor.startsWith("oklab(")) {
|
||||
// scam
|
||||
const [_, L, a, b] = rgbColor.match(/oklab\((.+?)[, ]+(.+?)[, ]+(.+?)\)/) ?? [];
|
||||
if (L && a && b) {
|
||||
rgbColor = oklabToSRGB({ L: parseFloat(L), a: parseFloat(a), b: parseFloat(b) });
|
||||
}
|
||||
}
|
||||
ctx.clearRect(0, 0, 1, 1);
|
||||
ctx.fillStyle = computedColor;
|
||||
ctx.fillRect(0, 0, 1, 1);
|
||||
|
||||
return rgbColor;
|
||||
const [r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data;
|
||||
if (a === 0) return null;
|
||||
|
||||
return `rgb(${r}, ${g}, ${b})`;
|
||||
}
|
||||
|
||||
const updateSplashColors = () => {
|
||||
const bodyStyles = document.body.computedStyleMap();
|
||||
const color = resolveColor("--text-default");
|
||||
const backgroundColor = resolveColor("--background-base-lowest");
|
||||
|
||||
const color = bodyStyles.get("--text-default");
|
||||
const backgroundColor = bodyStyles.get("--background-base-lowest");
|
||||
|
||||
if (isValidColor(color)) {
|
||||
Settings.store.splashColor = resolveColor(color[0]);
|
||||
}
|
||||
|
||||
if (isValidColor(backgroundColor)) {
|
||||
Settings.store.splashBackground = resolveColor(backgroundColor[0]);
|
||||
}
|
||||
if (color) Settings.store.splashColor = color;
|
||||
if (backgroundColor) Settings.store.splashBackground = backgroundColor;
|
||||
};
|
||||
|
||||
if (document.readyState === "complete") {
|
||||
|
||||
@@ -48,6 +48,10 @@
|
||||
height: 128px;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.pixelated img {
|
||||
image-rendering: pixelated;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
|
||||
Reference in new issue
Block a user