From c99278aa581efb02edbde0dab6742f6284a022a2 Mon Sep 17 00:00:00 2001 From: Alice Mikhaylenko Date: Mon, 3 Jun 2024 19:17:42 +0400 Subject: [PATCH] csscolor: Allow printing hsl and hwb as color() too --- gtk/gtkcsscolor.c | 79 ++++++++++++++++++++++++------------------ gtk/gtkcsscolorvalue.c | 4 +-- 2 files changed, 48 insertions(+), 35 deletions(-) diff --git a/gtk/gtkcsscolor.c b/gtk/gtkcsscolor.c index 63907d67bc..d64bca245a 100644 --- a/gtk/gtkcsscolor.c +++ b/gtk/gtkcsscolor.c @@ -73,46 +73,54 @@ append_color_component (GString *string, g_string_append_printf (string, "%g", gtk_css_color_get_component (color, idx)); } +static void +print_as_rgb (const GtkCssColor *color, + GString *string) +{ + GtkCssColor tmp; + + gtk_css_color_convert (color, GTK_CSS_COLOR_SPACE_SRGB, &tmp); + if (tmp.values[3] > 0.999) + { + g_string_append_printf (string, "rgb(%d,%d,%d)", + (int)(0.5 + CLAMP (tmp.values[0], 0., 1.) * 255.), + (int)(0.5 + CLAMP (tmp.values[1], 0., 1.) * 255.), + (int)(0.5 + CLAMP (tmp.values[2], 0., 1.) * 255.)); + } + else + { + char alpha[G_ASCII_DTOSTR_BUF_SIZE]; + + g_ascii_formatd (alpha, G_ASCII_DTOSTR_BUF_SIZE, "%g", CLAMP (tmp.values[3], 0, 1)); + + g_string_append_printf (string, "rgba(%d,%d,%d,%s)", + (int)(0.5 + CLAMP (tmp.values[0], 0., 1.) * 255.), + (int)(0.5 + CLAMP (tmp.values[1], 0., 1.) * 255.), + (int)(0.5 + CLAMP (tmp.values[2], 0., 1.) * 255.), + alpha); + } +} + GString * gtk_css_color_print (const GtkCssColor *color, gboolean serialize_as_rgb, GString *string) { + GtkCssColorSpace print_color_space = color->color_space; + GtkCssColor tmp; + switch (color->color_space) { + case GTK_CSS_COLOR_SPACE_SRGB: case GTK_CSS_COLOR_SPACE_HSL: case GTK_CSS_COLOR_SPACE_HWB: -print_rgb: - { - GtkCssColor tmp; - - gtk_css_color_convert (color, GTK_CSS_COLOR_SPACE_SRGB, &tmp); - if (tmp.values[3] > 0.999) - { - g_string_append_printf (string, "rgb(%d,%d,%d)", - (int)(0.5 + CLAMP (tmp.values[0], 0., 1.) * 255.), - (int)(0.5 + CLAMP (tmp.values[1], 0., 1.) * 255.), - (int)(0.5 + CLAMP (tmp.values[2], 0., 1.) * 255.)); - } - else - { - char alpha[G_ASCII_DTOSTR_BUF_SIZE]; - - g_ascii_formatd (alpha, G_ASCII_DTOSTR_BUF_SIZE, "%g", CLAMP (tmp.values[3], 0, 1)); - - g_string_append_printf (string, "rgba(%d,%d,%d,%s)", - (int)(0.5 + CLAMP (tmp.values[0], 0., 1.) * 255.), - (int)(0.5 + CLAMP (tmp.values[1], 0., 1.) * 255.), - (int)(0.5 + CLAMP (tmp.values[2], 0., 1.) * 255.), - alpha); - } - } - return string; - - case GTK_CSS_COLOR_SPACE_SRGB: if (serialize_as_rgb) - goto print_rgb; + { + print_as_rgb (color, string); + return string; + } + print_color_space = GTK_CSS_COLOR_SPACE_SRGB; g_string_append (string, "color(srgb "); break; @@ -132,18 +140,23 @@ print_rgb: g_assert_not_reached (); } + if (print_color_space != color->color_space) + gtk_css_color_convert (color, print_color_space, &tmp); + else + tmp = *color; + for (guint i = 0; i < 3; i++) { if (i > 0) g_string_append_c (string, ' '); - append_color_component (string, color, i); + append_color_component (string, &tmp, i); } - if (gtk_css_color_component_missing (color, 3) || - color->values[3] < 0.999) + if (gtk_css_color_component_missing (&tmp, 3) || + tmp.values[3] < 0.999) { g_string_append (string, " / "); - append_color_component (string, color, 3); + append_color_component (string, &tmp, 3); } g_string_append_c (string, ')'); diff --git a/gtk/gtkcsscolorvalue.c b/gtk/gtkcsscolorvalue.c index 608e126ffe..7dd1aa9f74 100644 --- a/gtk/gtkcsscolorvalue.c +++ b/gtk/gtkcsscolorvalue.c @@ -1447,7 +1447,7 @@ gtk_css_color_value_parse (GtkCssParser *parser) if (!parse_color_function (parser, COLOR_SYNTAX_DETECTING, FALSE, TRUE, FALSE, parse_hsla_color_channel, &data)) return NULL; - return gtk_css_color_value_new_color (GTK_CSS_COLOR_SPACE_HSL, FALSE, (float *) &hsla, data.missing); + return gtk_css_color_value_new_color (GTK_CSS_COLOR_SPACE_HSL, TRUE, (float *) &hsla, data.missing); } else if (gtk_css_parser_has_function (parser, "hwb")) { @@ -1456,7 +1456,7 @@ gtk_css_color_value_parse (GtkCssParser *parser) if (!parse_color_function (parser, COLOR_SYNTAX_MODERN, FALSE, TRUE, FALSE, parse_hwb_color_channel, &hwb)) return NULL; - return gtk_css_color_value_new_color (GTK_CSS_COLOR_SPACE_HWB, FALSE, (float *) &hwb, hwb.missing); + return gtk_css_color_value_new_color (GTK_CSS_COLOR_SPACE_HWB, TRUE, (float *) &hwb, hwb.missing); } else if (gtk_css_parser_has_function (parser, "oklab")) {