css: Add gtk_css_color_to_color

This api lets one obtain a color state and color values from
a GtkCssColor. We don't want to force everything though sRGB,
but we can't quite avoid conversion here, since we don't have
a 100% match between the css color spaces and color states.

css color cleanup
This commit is contained in:
Matthias Clasen 2024-07-31 19:59:09 -04:00
parent c9c0d444a2
commit f9612533c2
2 changed files with 58 additions and 0 deletions

View File

@ -19,6 +19,7 @@
#include "gtkcsscolorprivate.h" #include "gtkcsscolorprivate.h"
#include "gtkcolorutilsprivate.h" #include "gtkcolorutilsprivate.h"
#include "gdkcolorstateprivate.h"
/* {{{ Initialization */ /* {{{ Initialization */
@ -1163,5 +1164,52 @@ gtk_css_color_interpolation_method_print (GtkCssColorSpace in,
} }
} }
/* }}} */
/* {{{ GdkColor conversion */
void
gtk_css_color_to_color (const GtkCssColor *css,
GdkColor *color)
{
switch (css->color_space)
{
case GTK_CSS_COLOR_SPACE_SRGB:
gdk_color_init (color, GDK_COLOR_STATE_SRGB, css->values);
break;
case GTK_CSS_COLOR_SPACE_SRGB_LINEAR:
gdk_color_init (color, GDK_COLOR_STATE_SRGB_LINEAR, css->values);
break;
case GTK_CSS_COLOR_SPACE_REC2100_PQ:
gdk_color_init (color, GDK_COLOR_STATE_REC2100_PQ, css->values);
break;
case GTK_CSS_COLOR_SPACE_HSL:
case GTK_CSS_COLOR_SPACE_HWB:
case GTK_CSS_COLOR_SPACE_OKLAB:
case GTK_CSS_COLOR_SPACE_OKLCH:
{
GtkCssColor tmp;
gtk_css_color_convert (css, GTK_CSS_COLOR_SPACE_SRGB, &tmp);
gdk_color_init (color, GDK_COLOR_STATE_SRGB, tmp.values);
}
break;
case GTK_CSS_COLOR_SPACE_REC2020:
case GTK_CSS_COLOR_SPACE_DISPLAY_P3:
case GTK_CSS_COLOR_SPACE_XYZ:
{
GtkCssColor tmp;
gtk_css_color_convert (css, GTK_CSS_COLOR_SPACE_REC2100_PQ, &tmp);
gdk_color_init (color, GDK_COLOR_STATE_REC2100_PQ, tmp.values);
}
break;
default:
g_assert_not_reached ();
}
}
/* }}} */ /* }}} */
/* vim:set foldmethod=marker expandtab: */ /* vim:set foldmethod=marker expandtab: */

View File

@ -22,6 +22,7 @@
#include "gtk/css/gtkcssparserprivate.h" #include "gtk/css/gtkcssparserprivate.h"
#include "gtkcsstypesprivate.h" #include "gtkcsstypesprivate.h"
#include "gdk/gdkcolorprivate.h"
G_BEGIN_DECLS G_BEGIN_DECLS
@ -115,4 +116,13 @@ void gtk_css_color_interpolation_method_print (GtkCssColorSpace in,
GtkCssHueInterpolation interp, GtkCssHueInterpolation interp,
GString *string); GString *string);
static inline gboolean
gtk_css_color_is_clear (const GtkCssColor *color)
{
return color->values[3] < (float) 0x00ff / (float) 0xffff;
}
void gtk_css_color_to_color (const GtkCssColor *css,
GdkColor *color);
G_END_DECLS G_END_DECLS