From f9bc96f65795038db84f00611c2831a7c4e58696 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 19 Jan 2024 23:59:00 +0100 Subject: [PATCH] color-util: make return values of rgb_to_hsv() optional When we want to tint the bg color we don't care about the hue, we want to set it ourself after all, hence make the arguments optional, so that we don't even have to ask for it. --- src/shared/color-util.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/shared/color-util.c b/src/shared/color-util.c index 776445ecfc..9d714c0fff 100644 --- a/src/shared/color-util.c +++ b/src/shared/color-util.c @@ -11,35 +11,38 @@ void rgb_to_hsv(double r, double g, double b, assert(r >= 0 && r <= 1); assert(g >= 0 && g <= 1); assert(b >= 0 && b <= 1); - assert(ret_h); - assert(ret_s); - assert(ret_v); double max_color = fmax(r, fmax(g, b)); double min_color = fmin(r, fmin(g, b)); double delta = max_color - min_color; - *ret_v = max_color * 100.0; + if (ret_v) + *ret_v = max_color * 100.0; - if (max_color > 0) - *ret_s = delta / max_color * 100.0; - else { - *ret_s = 0; - *ret_h = NAN; + if (max_color <= 0) { + if (ret_s) + *ret_s = 0; + if (ret_h) + *ret_h = NAN; return; } - if (delta > 0) { - if (r >= max_color) - *ret_h = 60 * fmod((g - b) / delta, 6); - else if (g >= max_color) - *ret_h = 60 * (((b - r) / delta) + 2); - else if (b >= max_color) - *ret_h = 60 * (((r - g) / delta) + 4); + if (ret_s) + *ret_s = delta / max_color * 100.0; - *ret_h = fmod(*ret_h, 360); - } else - *ret_h = NAN; + if (ret_h) { + if (delta > 0) { + if (r >= max_color) + *ret_h = 60 * fmod((g - b) / delta, 6); + else if (g >= max_color) + *ret_h = 60 * (((b - r) / delta) + 2); + else if (b >= max_color) + *ret_h = 60 * (((r - g) / delta) + 4); + + *ret_h = fmod(*ret_h, 360); + } else + *ret_h = NAN; + } } void hsv_to_rgb(double h, double s, double v,