Merge branch 'matthiasc/for-main' into 'main'

css: Make some color value functions static

See merge request GNOME/gtk!7319
This commit is contained in:
Matthias Clasen 2024-05-31 16:40:33 +00:00
commit dff340530a
6 changed files with 137 additions and 48 deletions

View File

@ -229,6 +229,32 @@ gtk_rgb_to_hsv (float r, float g, float b,
*v = b;
}
void
gtk_rgb_to_hsl (float red, float green, float blue,
float *hue, float *saturation, float *lightness)
{
GdkHSLA hsla;
_gdk_hsla_init_from_rgba (&hsla, &(GdkRGBA) { red, green, blue, 1.0 });
*hue = hsla.hue;
*saturation = hsla.saturation;
*lightness = hsla.lightness;
}
void
gtk_hsl_to_rgb (float hue, float saturation, float lightness,
float *red, float *green, float *blue)
{
GdkRGBA rgba;
_gdk_rgba_init_from_hsla (&rgba, &(GdkHSLA) { hue, saturation, lightness, 1.0 });
*red = rgba.red;
*green = rgba.green;
*blue = rgba.blue;
}
void
gtk_rgb_to_hwb (float red, float green, float blue,
float *hue, float *white, float *black)
@ -320,8 +346,8 @@ unapply_gamma (float v)
}
void
gtk_oklab_to_rgb (float L, float a, float b,
float *red, float *green, float *blue)
gtk_oklab_to_linear_srgb (float L, float a, float b,
float *red, float *green, float *blue)
{
float l = L + 0.3963377774f * a + 0.2158037573f * b;
float m = L - 0.1055613458f * a - 0.0638541728f * b;
@ -331,24 +357,27 @@ gtk_oklab_to_rgb (float L, float a, float b,
m = powf (m, 3);
s = powf (s, 3);
float linear_red = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
float linear_green = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
float linear_blue = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
*red = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
*green = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
*blue = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
}
void
gtk_oklab_to_rgb (float L, float a, float b,
float *red, float *green, float *blue)
{
float linear_red, linear_green, linear_blue;
gtk_oklab_to_linear_srgb (L, a, b, &linear_red, &linear_green, &linear_blue);
gtk_linear_srgb_to_rgb (linear_red, linear_green, linear_blue, red, green, blue);
}
void
gtk_rgb_to_oklab (float red, float green, float blue,
float *L, float *a, float *b)
gtk_linear_srgb_to_oklab (float red, float green, float blue,
float *L, float *a, float *b)
{
float linear_red, linear_green, linear_blue;
gtk_rgb_to_linear_srgb (red, green, blue, &linear_red, &linear_green, &linear_blue);
float l = 0.4122214708f * linear_red + 0.5363325363f * linear_green + 0.0514459929f * linear_blue;
float m = 0.2119034982f * linear_red + 0.6806995451f * linear_green + 0.1073969566f * linear_blue;
float s = 0.0883024619f * linear_red + 0.2817188376f * linear_green + 0.6299787005f * linear_blue;
float l = 0.4122214708f * red + 0.5363325363f * green + 0.0514459929f * blue;
float m = 0.2119034982f * red + 0.6806995451f * green + 0.1073969566f * blue;
float s = 0.0883024619f * red + 0.2817188376f * green + 0.6299787005f * blue;
l = cbrtf (l);
m = cbrtf (m);
@ -359,6 +388,15 @@ gtk_rgb_to_oklab (float red, float green, float blue,
*b = 0.0259040371f*l + 0.7827717662f*m - 0.8086757660f*s;
}
void
gtk_rgb_to_oklab (float red, float green, float blue,
float *L, float *a, float *b)
{
float linear_red, linear_green, linear_blue;
gtk_rgb_to_linear_srgb (red, green, blue, &linear_red, &linear_green, &linear_blue);
gtk_linear_srgb_to_oklab (linear_red, linear_green, linear_blue, L, a, b);
}
void
gtk_rgb_to_linear_srgb (float red, float green, float blue,
float *linear_red, float *linear_green, float *linear_blue)

View File

@ -21,7 +21,12 @@
G_BEGIN_DECLS
void gtk_rgb_to_hwb (float red, float green, float blue,
void gtk_rgb_to_hsl (float red, float green, float blue,
float *hue, float *saturation, float *lightness);
void gtk_hsl_to_rgb (float hue, float saturation, float lightness,
float *red, float *green, float *blue);
void gtk_rgb_to_hwb (float red, float green, float blue,
float *hue, float *white, float *black);
void gtk_hwb_to_rgb (float hue, float white, float black,
float *red, float *green, float *blue);
@ -31,6 +36,11 @@ void gtk_oklab_to_oklch (float L, float a, float b,
void gtk_oklch_to_oklab (float L, float C, float H,
float *L2, float *a, float *b);
void gtk_oklab_to_linear_srgb (float L, float a, float b,
float *red, float *green, float *blue);
void gtk_linear_srgb_to_oklab (float red, float green, float blue,
float *L, float *a, float *b);
void gtk_oklab_to_rgb (float L, float a, float b,
float *red, float *green, float *blue);
void gtk_rgb_to_oklab (float red, float green, float blue,
@ -41,5 +51,6 @@ void gtk_rgb_to_linear_srgb (float red, float green, float blue
void gtk_linear_srgb_to_rgb (float linear_red, float linear_green, float linear_blue,
float *red, float *green, float *blue);
G_END_DECLS

View File

@ -30,6 +30,15 @@
#include "gdk/gdkrgbaprivate.h"
#include "gtkcolorutilsprivate.h"
typedef enum {
GTK_CSS_COLOR_SPACE_SRGB,
GTK_CSS_COLOR_SPACE_SRGB_LINEAR,
} GtkCssColorSpace;
static GtkCssValue * gtk_css_color_value_new_mix (GtkCssValue *color1,
GtkCssValue *color2,
double factor);
typedef enum {
COLOR_TYPE_LITERAL,
COLOR_TYPE_COLOR,
@ -70,10 +79,12 @@ struct _GtkCssValue
GtkCssValue *color2;
double factor;
} mix;
struct
{
float L, a, b, alpha;
} oklab;
struct
{
float L, C, H, alpha;
@ -209,7 +220,7 @@ gtk_css_value_color_equal (const GtkCssValue *value1,
case COLOR_TYPE_COLOR:
return value1->color.color_space == value2->color.color_space &&
memcmp (value1->color.values, value2->color.values, sizeof(float) * 4) == 0;
memcmp (value1->color.values, value2->color.values, sizeof (float) * 4) == 0;
case COLOR_TYPE_NAME:
return g_str_equal (value1->name, value2->name);
@ -665,7 +676,7 @@ gtk_css_color_value_new_literal (const GdkRGBA *color)
return value;
}
GtkCssValue *
static GtkCssValue *
gtk_css_value_value_new_color (GtkCssColorSpace color_space,
float values[4])
{
@ -674,7 +685,7 @@ gtk_css_value_value_new_color (GtkCssColorSpace color_space,
value = gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_COLOR);
value->type = COLOR_TYPE_COLOR;
value->color.color_space = color_space;
memcpy (value->color.values, values, sizeof(float) * 4);
memcpy (value->color.values, values, sizeof (float) * 4);
return value;
}
@ -693,7 +704,7 @@ gtk_css_color_value_new_name (const char *name)
return value;
}
GtkCssValue *
static GtkCssValue *
gtk_css_color_value_new_shade (GtkCssValue *color,
double factor)
{
@ -718,7 +729,7 @@ gtk_css_color_value_new_shade (GtkCssValue *color,
return value;
}
GtkCssValue *
static GtkCssValue *
gtk_css_color_value_new_alpha (GtkCssValue *color,
double factor)
{
@ -743,7 +754,7 @@ gtk_css_color_value_new_alpha (GtkCssValue *color,
return value;
}
GtkCssValue *
static GtkCssValue *
gtk_css_color_value_new_mix (GtkCssValue *color1,
GtkCssValue *color2,
double factor)
@ -781,7 +792,7 @@ gtk_css_color_value_new_current_color (void)
return gtk_css_value_ref (&current_color);
}
GtkCssValue *
static GtkCssValue *
gtk_css_color_value_new_oklab (float L,
float a,
float b,
@ -799,7 +810,7 @@ gtk_css_color_value_new_oklab (float L,
return value;
}
GtkCssValue *
static GtkCssValue *
gtk_css_color_value_new_oklch (float L,
float C,
float H,

View File

@ -24,33 +24,12 @@
G_BEGIN_DECLS
typedef enum {
GTK_CSS_COLOR_SPACE_SRGB,
GTK_CSS_COLOR_SPACE_SRGB_LINEAR,
} GtkCssColorSpace;
GtkCssValue * gtk_css_color_value_new_transparent (void) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_white (void) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_literal (const GdkRGBA *color) G_GNUC_PURE;
GtkCssValue * gtk_css_value_value_new_color (GtkCssColorSpace color_space,
float values[4]) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_name (const char *name) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_shade (GtkCssValue *color,
double factor) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_alpha (GtkCssValue *color,
double factor) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_mix (GtkCssValue *color1,
GtkCssValue *color2,
double factor) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_current_color (void) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_oklab (float L,
float a,
float b,
float alpha) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_oklch (float L,
float C,
float H,
float alpha) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_name (const char *name) G_GNUC_PURE;
GtkCssValue * gtk_css_color_value_new_literal (const GdkRGBA *color) G_GNUC_PURE;
gboolean gtk_css_color_value_can_parse (GtkCssParser *parser);
GtkCssValue * gtk_css_color_value_parse (GtkCssParser *parser);

View File

@ -47,6 +47,7 @@ struct _GtkInspectorCssEditorPrivate
GtkToggleButton *disable_button;
guint timeout;
GList *errors;
gboolean show_deprecations;
};
typedef struct {
@ -94,6 +95,12 @@ query_tooltip_cb (GtkWidget *widget,
{
CssError *css_error = l->data;
if (g_error_matches (css_error->error,
GTK_CSS_PARSER_WARNING,
GTK_CSS_PARSER_WARNING_DEPRECATED) &&
!ce->priv->show_deprecations)
continue;
if (gtk_text_iter_in_range (&iter, &css_error->start, &css_error->end))
{
gtk_tooltip_set_text (tooltip, css_error->error->message);
@ -170,6 +177,29 @@ disable_toggled (GtkToggleButton *button,
GTK_STYLE_PROVIDER_PRIORITY_USER);
}
static void
toggle_deprecations (GtkToggleButton *button,
GtkInspectorCssEditor *ce)
{
GtkTextTagTable *tags;
GtkTextTag *tag;
PangoUnderline underline;
if (!ce->priv->display)
return;
ce->priv->show_deprecations = gtk_toggle_button_get_active (button);
tags = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (ce->priv->text));
tag = gtk_text_tag_table_lookup (tags, "deprecation");
if (ce->priv->show_deprecations)
underline = PANGO_UNDERLINE_SINGLE;
else
underline = PANGO_UNDERLINE_NONE;
g_object_set (tag, "underline", underline, NULL);
}
static char *
get_current_text (GtkTextBuffer *buffer)
{
@ -316,7 +346,12 @@ show_parsing_error (GtkCssProvider *provider,
end->line_bytes);
if (error->domain == GTK_CSS_PARSER_WARNING)
tag_name = "warning";
{
if (error->code == GTK_CSS_PARSER_WARNING_DEPRECATED)
tag_name = "deprecation";
else
tag_name = "warning";
}
else
tag_name = "error";
@ -407,6 +442,7 @@ gtk_inspector_css_editor_class_init (GtkInspectorCssEditorClass *klass)
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorCssEditor, view);
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorCssEditor, disable_button);
gtk_widget_class_bind_template_callback (widget_class, disable_toggled);
gtk_widget_class_bind_template_callback (widget_class, toggle_deprecations);
gtk_widget_class_bind_template_callback (widget_class, save_clicked);
gtk_widget_class_bind_template_callback (widget_class, text_changed);
gtk_widget_class_bind_template_callback (widget_class, query_tooltip_cb);

View File

@ -7,6 +7,13 @@
<property name="underline-rgba">darkorange</property>
</object>
</child>
<child type="tag">
<object class="GtkTextTag">
<property name="name">deprecation</property>
<property name="underline">none</property>
<property name="underline-rgba">blue</property>
</object>
</child>
<child type="tag">
<object class="GtkTextTag">
<property name="name">error</property>
@ -39,6 +46,13 @@
<signal name="clicked" handler="save_clicked"/>
</object>
</child>
<child>
<object class="GtkToggleButton" id="deprections_button">
<property name="tooltip-text" translatable="yes">Show deprecations</property>
<property name="icon-name">dialog-information-symbolic</property>
<signal name="toggled" handler="toggle_deprecations"/>
</object>
</child>
</object>
</child>
<child>