color utils: use qt's native color funcs

This commit is contained in:
end-4
2025-05-19 23:27:57 +02:00
parent 912422bca5
commit 2475a64bdc
@@ -1,56 +1,4 @@
// Utility functions for color manipulation.
/**
* Converts an RGB color object to HSV color space.
*
* @param {{r: number, g: number, b: number, a: number}} c - The color object with r, g, b, a properties (0-1).
* @returns {{h: number, s: number, v: number, a: number}} The HSV representation with alpha.
*/
function rgb2hsv(c) {
var r = c.r, g = c.g, b = c.b;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, v = max;
var d = max - min;
s = max === 0 ? 0 : d / max;
if (max === min) {
h = 0;
} else {
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return {h: h, s: s, v: v, a: c.a};
}
/**
* Converts an HSV color value to an RGBA color.
*
* @param {number} h - Hue component (0-1).
* @param {number} s - Saturation component (0-1).
* @param {number} v - Value component (0-1).
* @param {number} a - Alpha component (0-1).
* @returns {Qt.rgba} The resulting color as a Qt.rgba object.
*/
function hsv2rgb(h, s, v, a) {
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch(i % 6){
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return Qt.rgba(r, g, b, a);
}
// This module provides high level utility functions for color manipulation.
/**
* Returns a color with the hue of color2 and the saturation, value, and alpha of color1.
@@ -60,15 +8,16 @@ function hsv2rgb(h, s, v, a) {
* @returns {Qt.rgba} The resulting color.
*/
function colorWithHueOf(color1, color2) {
// Convert both colors to HSV
var c1 = Qt.color(color1);
var c2 = Qt.color(color2);
var hsv1 = rgb2hsv(c1);
var hsv2 = rgb2hsv(c2);
// Qt.color hsvHue/hsvSaturation/hsvValue/alpha return 0-1
var hue = c2.hsvHue;
var sat = c1.hsvSaturation;
var val = c1.hsvValue;
var alpha = c1.a;
// Use hue from color2, saturation/value/alpha from color1
return hsv2rgb(hsv2.h, hsv1.s, hsv1.v, hsv1.a);
return Qt.hsva(hue, sat, val, alpha);
}
/**
@@ -79,15 +28,15 @@ function colorWithHueOf(color1, color2) {
* @returns {Qt.rgba} The resulting color.
*/
function colorWithSaturationOf(color1, color2) {
// Convert both colors to HSV
var c1 = Qt.color(color1);
var c2 = Qt.color(color2);
var hsv1 = rgb2hsv(c1);
var hsv2 = rgb2hsv(c2);
var hue = c1.hsvHue;
var sat = c2.hsvSaturation;
var val = c1.hsvValue;
var alpha = c1.a;
// Use hue from color2, saturation/value/alpha from color1
return hsv2rgb(hsv1.h, hsv2.s, hsv1.v, hsv1.a);
return Qt.hsva(hue, sat, val, alpha);
}
/**