make transparency not ass

This commit is contained in:
end-4
2025-12-21 23:29:32 +01:00
parent d5dbf7ab7f
commit 4041310b4d
3 changed files with 75 additions and 25 deletions
@@ -135,4 +135,38 @@ Singleton {
var c = Qt.color(color);
return c.hslLightness < 0.5;
}
/**
* Clamps a value to the inclusive range [0, 1].
*
* @param {number} x - The value to clamp.
* @returns {number} The clamped value in the range [0, 1].
*/
function clamp01(x) {
return Math.min(1, Math.max(0, x));
}
/**
* Solves for the solid overlay color that, when composited over a base color
* with a given opacity, yields the target color.
*
* The compositing equation is:
* result = overlay * overlayOpacity + base * (1 - overlayOpacity)
*
* This function algebraically inverts that equation per channel.
*
* @param {Qt.rgba} baseColor - The base (background) color.
* @param {Qt.rgba} targetColor - The resulting color after compositing.
* @param {number} overlayOpacity - The overlay opacity (0-1).
* @returns {Qt.rgba} The solved overlay color
*/
function solveOverlayColor(baseColor, targetColor, overlayOpacity) {
let invA = 1.0 - overlayOpacity;
let r = (targetColor.r - baseColor.r * invA) / overlayOpacity;
let g = (targetColor.g - baseColor.g * invA) / overlayOpacity;
let b = (targetColor.b - baseColor.b * invA) / overlayOpacity;
return Qt.rgba(clamp01(r), clamp01(g), clamp01(b), overlayOpacity);
}
}