From 3d5fce1b9f27e80a92af070a04ccf0cb39c774b1 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 2 Jun 2024 08:45:09 -0400 Subject: [PATCH 1/5] Fix a bug in color conversion The linear space corresponding to oklab is oklab, not srgb-linear. --- gtk/gtkcsscolor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gtk/gtkcsscolor.c b/gtk/gtkcsscolor.c index 053d5a0296..66830ad5fd 100644 --- a/gtk/gtkcsscolor.c +++ b/gtk/gtkcsscolor.c @@ -321,7 +321,8 @@ convert_linear_to_linear (GtkCssColor *output, GtkCssColorSpace dest_linear; float v[4]; - if (dest == GTK_CSS_COLOR_SPACE_OKLCH) + if (dest == GTK_CSS_COLOR_SPACE_OKLCH || + dest == GTK_CSS_COLOR_SPACE_OKLAB) dest_linear = GTK_CSS_COLOR_SPACE_OKLAB; else dest_linear = GTK_CSS_COLOR_SPACE_SRGB_LINEAR; From 76caf2c812ba9299fa7f9a0a2f3a7e012132caec Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 2 Jun 2024 08:19:31 -0400 Subject: [PATCH 2/5] color value: Organize the code Add some sections, so I can find my way around. --- gtk/gtkcsscolorvalue.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/gtk/gtkcsscolorvalue.c b/gtk/gtkcsscolorvalue.c index aaa6d8a1bb..4fc3dfc9c5 100644 --- a/gtk/gtkcsscolorvalue.c +++ b/gtk/gtkcsscolorvalue.c @@ -74,6 +74,8 @@ struct _GtkCssValue }; }; +/* {{{ GtkCssValue vfuncs */ + static void gtk_css_value_color_free (GtkCssValue *color) { @@ -328,6 +330,9 @@ static const GtkCssValueClass GTK_CSS_VALUE_COLOR = { gtk_css_value_color_print }; +/* }}} */ +/* {{{ Resolving colors */ + static void apply_alpha (const GdkRGBA *in, GdkRGBA *out, @@ -497,6 +502,9 @@ gtk_css_color_value_resolve (GtkCssValue *color, return gtk_css_color_value_do_resolve (color, provider, current, NULL); } +/* }}} */ +/* {{{ Constructors */ + static GtkCssValue transparent_black_singleton = { >K_CSS_VALUE_COLOR, 1, TRUE, FALSE, FALSE, COLOR_TYPE_LITERAL, .rgba = {0, 0, 0, 0} }; static GtkCssValue white_singleton = { >K_CSS_VALUE_COLOR, 1, TRUE, FALSE, FALSE, COLOR_TYPE_LITERAL, @@ -664,6 +672,9 @@ gtk_css_color_value_new_current_color (void) return gtk_css_value_ref (¤t_color); } +/* }}} */ +/* {{{ Parsing */ + typedef struct { GtkCssValue *color; @@ -1553,6 +1564,9 @@ gtk_css_color_value_parse (GtkCssParser *parser) return NULL; } +/* }}} */ +/* {{{ Utilities */ + const GdkRGBA * gtk_css_color_value_get_rgba (const GtkCssValue *color) { @@ -1570,3 +1584,6 @@ gtk_css_color_value_get_color (const GtkCssValue *color) return &color->color; } + +/* }}} */ +/* vim:set foldmethod=marker expandtab: */ From 62b9a4af755db43b8fa2dbef19ba8bac1cf803bf Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 2 Jun 2024 09:18:55 -0400 Subject: [PATCH 3/5] Drop an unused static --- gtk/gtkcsscolorvalue.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/gtk/gtkcsscolorvalue.c b/gtk/gtkcsscolorvalue.c index 4fc3dfc9c5..93f494ef54 100644 --- a/gtk/gtkcsscolorvalue.c +++ b/gtk/gtkcsscolorvalue.c @@ -249,17 +249,6 @@ gtk_css_value_color_transition (GtkCssValue *start, return gtk_css_color_value_new_mix (start, end, progress); } -static inline void -append_color_component (GString *string, - const GtkCssColor *color, - guint idx) -{ - if (gtk_css_color_component_missing (color, idx)) - g_string_append (string, "none"); - else - g_string_append_printf (string, "%g", gtk_css_color_get_component (color, idx)); -} - static void gtk_css_value_color_print (const GtkCssValue *value, GString *string) From 20392e8840bae17ef3155fa763248c541ab512d0 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 2 Jun 2024 09:25:52 -0400 Subject: [PATCH 4/5] Cosmetics --- gtk/gtkcsscolorvalue.c | 48 ++++++++++++++++++++--------------------- gtk/gtkcssnumbervalue.c | 12 +++++------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/gtk/gtkcsscolorvalue.c b/gtk/gtkcsscolorvalue.c index 93f494ef54..608e126ffe 100644 --- a/gtk/gtkcsscolorvalue.c +++ b/gtk/gtkcsscolorvalue.c @@ -348,7 +348,7 @@ apply_shade (const GdkRGBA *in, static inline double transition (double start, double end, - double progress) + double progress) { return start + (end - start) * progress; } @@ -664,6 +664,29 @@ gtk_css_color_value_new_current_color (void) /* }}} */ /* {{{ Parsing */ +gboolean +gtk_css_color_value_can_parse (GtkCssParser *parser) +{ + /* This is way too generous, but meh... */ + return gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_IDENT) + || gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_AT_KEYWORD) + || gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_HASH_ID) + || gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_HASH_UNRESTRICTED) + || gtk_css_parser_has_function (parser, "lighter") + || gtk_css_parser_has_function (parser, "darker") + || gtk_css_parser_has_function (parser, "shade") + || gtk_css_parser_has_function (parser, "alpha") + || gtk_css_parser_has_function (parser, "mix") + || gtk_css_parser_has_function (parser, "hsl") + || gtk_css_parser_has_function (parser, "hsla") + || gtk_css_parser_has_function (parser, "rgb") + || gtk_css_parser_has_function (parser, "rgba") + || gtk_css_parser_has_function (parser, "hwb") + || gtk_css_parser_has_function (parser, "oklab") + || gtk_css_parser_has_function (parser, "oklch") + || gtk_css_parser_has_function (parser, "color"); +} + typedef struct { GtkCssValue *color; @@ -727,29 +750,6 @@ parse_color_number (GtkCssParser *parser, } } -gboolean -gtk_css_color_value_can_parse (GtkCssParser *parser) -{ - /* This is way too generous, but meh... */ - return gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_IDENT) - || gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_AT_KEYWORD) - || gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_HASH_ID) - || gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_HASH_UNRESTRICTED) - || gtk_css_parser_has_function (parser, "lighter") - || gtk_css_parser_has_function (parser, "darker") - || gtk_css_parser_has_function (parser, "shade") - || gtk_css_parser_has_function (parser, "alpha") - || gtk_css_parser_has_function (parser, "mix") - || gtk_css_parser_has_function (parser, "hsl") - || gtk_css_parser_has_function (parser, "hsla") - || gtk_css_parser_has_function (parser, "rgb") - || gtk_css_parser_has_function (parser, "rgba") - || gtk_css_parser_has_function (parser, "hwb") - || gtk_css_parser_has_function (parser, "oklab") - || gtk_css_parser_has_function (parser, "oklch") - || gtk_css_parser_has_function (parser, "color"); -} - typedef struct { GdkRGBA *rgba; diff --git a/gtk/gtkcssnumbervalue.c b/gtk/gtkcssnumbervalue.c index 1defc7eda4..96cd1b1d36 100644 --- a/gtk/gtkcssnumbervalue.c +++ b/gtk/gtkcssnumbervalue.c @@ -1015,11 +1015,11 @@ gtk_css_number_value_multiply (GtkCssValue *value, break; } - return gtk_css_math_value_new (TYPE_PRODUCT, 0, - (GtkCssValue *[]) { - gtk_css_value_ref (value), - gtk_css_number_value_new (factor, GTK_CSS_NUMBER) - }, 2); + return gtk_css_math_value_new (TYPE_PRODUCT, 0, + (GtkCssValue *[]) { + gtk_css_value_ref (value), + gtk_css_number_value_new (factor, GTK_CSS_NUMBER) + }, 2); } GtkCssValue * @@ -1314,7 +1314,7 @@ gtk_css_arg2_value_new (guint type, return gtk_css_dimension_value_new (v, unit); } -/* This funciton is called at parsing time, so units are not +/* This function is called at parsing time, so units are not * canonical, and length values can't necessarily be unified. */ GtkCssValue * From 434134454527f604e5993f1661720e3536d8c7a3 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 2 Jun 2024 11:04:09 -0400 Subject: [PATCH 5/5] css: Add a utility for color space coords --- gtk/gtkcsscolor.c | 55 ++++++++++++++++++++++++++++++++++++++++ gtk/gtkcsscolorprivate.h | 3 +++ 2 files changed, 58 insertions(+) diff --git a/gtk/gtkcsscolor.c b/gtk/gtkcsscolor.c index 66830ad5fd..63907d67bc 100644 --- a/gtk/gtkcsscolor.c +++ b/gtk/gtkcsscolor.c @@ -157,6 +157,61 @@ gtk_css_color_to_string (const GtkCssColor *color) return g_string_free (gtk_css_color_print (color, FALSE, g_string_new ("")), FALSE); } +const char * +gtk_css_color_space_get_coord_name (GtkCssColorSpace color_space, + guint coord) +{ + if (coord == 3) + return "alpha"; + + switch (color_space) + { + case GTK_CSS_COLOR_SPACE_SRGB: + case GTK_CSS_COLOR_SPACE_SRGB_LINEAR: + switch (coord) + { + case 0: return "r"; + case 1: return "g"; + case 2: return "b"; + default: g_assert_not_reached (); + } + case GTK_CSS_COLOR_SPACE_HSL: + switch (coord) + { + case 0: return "h"; + case 1: return "s"; + case 2: return "l"; + default: g_assert_not_reached (); + } + case GTK_CSS_COLOR_SPACE_HWB: + switch (coord) + { + case 0: return "h"; + case 1: return "w"; + case 2: return "b"; + default: g_assert_not_reached (); + } + case GTK_CSS_COLOR_SPACE_OKLAB: + switch (coord) + { + case 0: return "l"; + case 1: return "a"; + case 2: return "b"; + default: g_assert_not_reached (); + } + case GTK_CSS_COLOR_SPACE_OKLCH: + switch (coord) + { + case 0: return "l"; + case 1: return "c"; + case 2: return "h"; + default: g_assert_not_reached (); + } + default: + g_assert_not_reached (); + } +} + /* }}} */ /* {{{ Color conversion */ diff --git a/gtk/gtkcsscolorprivate.h b/gtk/gtkcsscolorprivate.h index 5ed457d224..f48764f410 100644 --- a/gtk/gtkcsscolorprivate.h +++ b/gtk/gtkcsscolorprivate.h @@ -109,4 +109,7 @@ void gtk_css_color_interpolate (const GtkCssColor *from, GtkCssHueInterpolation interp, GtkCssColor *output); +const char * gtk_css_color_space_get_coord_name (GtkCssColorSpace color_space, + guint coord); + G_END_DECLS