GdkRGBA: Use floats instead of doubles

This commit is contained in:
Timm Bäder 2019-12-10 14:22:52 +01:00
parent 0956c30ee5
commit 095a378dbc
10 changed files with 68 additions and 52 deletions

View File

@ -100,7 +100,7 @@ gdk_rgba_free (GdkRGBA *rgba)
gboolean gboolean
gdk_rgba_is_clear (const GdkRGBA *rgba) gdk_rgba_is_clear (const GdkRGBA *rgba)
{ {
return rgba->alpha < ((double) 0x00ff / (double) 0xffff); return rgba->alpha < ((float) 0x00ff / (float) 0xffff);
} }
/** /**
@ -115,7 +115,7 @@ gdk_rgba_is_clear (const GdkRGBA *rgba)
gboolean gboolean
gdk_rgba_is_opaque (const GdkRGBA *rgba) gdk_rgba_is_opaque (const GdkRGBA *rgba)
{ {
return rgba->alpha > ((double)0xff00 / (double)0xffff); return rgba->alpha > ((float)0xff00 / (float)0xffff);
} }
#define SKIP_WHITESPACES(s) while (*(s) == ' ') (s)++; #define SKIP_WHITESPACES(s) while (*(s) == ' ') (s)++;
@ -398,23 +398,25 @@ gdk_rgba_to_string (const GdkRGBA *rgba)
static gboolean static gboolean
parse_color_channel_value (GtkCssParser *parser, parse_color_channel_value (GtkCssParser *parser,
double *value, float *value,
gboolean is_percentage) gboolean is_percentage)
{ {
double dvalue;
if (is_percentage) if (is_percentage)
{ {
if (!gtk_css_parser_consume_percentage (parser, value)) if (!gtk_css_parser_consume_percentage (parser, &dvalue))
return FALSE; return FALSE;
*value = CLAMP (*value, 0.0, 100.0) / 100.0; *value = CLAMP (dvalue, 0.0, 100.0) / 100.0;
return TRUE; return TRUE;
} }
else else
{ {
if (!gtk_css_parser_consume_number (parser, value)) if (!gtk_css_parser_consume_number (parser, &dvalue))
return FALSE; return FALSE;
*value = CLAMP (*value, 0.0, 255.0) / 255.0; *value = CLAMP (dvalue, 0.0, 255.0) / 255.0;
return TRUE; return TRUE;
} }
} }
@ -425,6 +427,7 @@ parse_color_channel (GtkCssParser *parser,
gpointer data) gpointer data)
{ {
GdkRGBA *rgba = data; GdkRGBA *rgba = data;
double dvalue;
switch (arg) switch (arg)
{ {
@ -450,10 +453,10 @@ parse_color_channel (GtkCssParser *parser,
return 1; return 1;
case 3: case 3:
if (!gtk_css_parser_consume_number (parser, &rgba->alpha)) if (!gtk_css_parser_consume_number (parser, &dvalue))
return 0; return 0;
rgba->alpha = CLAMP (rgba->alpha, 0.0, 1.0); rgba->alpha = CLAMP (dvalue, 0.0, 1.0);
return 1; return 1;
default: default:

View File

@ -36,10 +36,10 @@ G_BEGIN_DECLS
struct _GdkRGBA struct _GdkRGBA
{ {
gdouble red; float red;
gdouble green; float green;
gdouble blue; float blue;
gdouble alpha; float alpha;
}; };
#define GDK_TYPE_RGBA (gdk_rgba_get_type ()) #define GDK_TYPE_RGBA (gdk_rgba_get_type ())

View File

@ -530,7 +530,8 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
GtkWidget *button; GtkWidget *button;
GtkWidget *label; GtkWidget *label;
gint i; gint i;
GdkRGBA color; double color[4];
GdkRGBA rgba;
GVariant *variant; GVariant *variant;
GVariantIter iter; GVariantIter iter;
gboolean selected; gboolean selected;
@ -568,14 +569,20 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
g_variant_iter_init (&iter, variant); g_variant_iter_init (&iter, variant);
i = 0; i = 0;
p = NULL; p = NULL;
while (g_variant_iter_loop (&iter, "(dddd)", &color.red, &color.green, &color.blue, &color.alpha)) while (g_variant_iter_loop (&iter, "(dddd)", &color[0], &color[1], &color[2], &color[3]))
{ {
i++; i++;
p = gtk_color_swatch_new (); p = gtk_color_swatch_new ();
gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (p), &color);
rgba.red = color[0];
rgba.green = color[1];
rgba.blue = color[2];
rgba.alpha = color[3];
gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (p), &rgba);
gtk_color_swatch_set_can_drop (GTK_COLOR_SWATCH (p), TRUE); gtk_color_swatch_set_can_drop (GTK_COLOR_SWATCH (p), TRUE);
atk_obj = gtk_widget_get_accessible (p); atk_obj = gtk_widget_get_accessible (p);
name = accessible_color_name (&color); name = accessible_color_name (&rgba);
text = g_strdup_printf (_("Custom color %d: %s"), i, name); text = g_strdup_printf (_("Custom color %d: %s"), i, name);
atk_object_set_name (atk_obj, text); atk_object_set_name (atk_obj, text);
g_free (text); g_free (text);
@ -598,9 +605,15 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
g_settings_get (priv->settings, I_("selected-color"), "(bdddd)", g_settings_get (priv->settings, I_("selected-color"), "(bdddd)",
&selected, &selected,
&color.red, &color.green, &color.blue, &color.alpha); &color[0], &color[1], &color[2], &color[3]);
if (selected) if (selected)
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc), &color); {
rgba.red = color[0];
rgba.green = color[1];
rgba.blue = color[2];
rgba.alpha = color[3];
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc), &rgba);
}
gtk_widget_hide (GTK_WIDGET (priv->editor)); gtk_widget_hide (GTK_WIDGET (priv->editor));
} }

View File

@ -555,7 +555,7 @@ gtk_color_editor_get_rgba (GtkColorChooser *chooser,
GdkRGBA *color) GdkRGBA *color)
{ {
GtkColorEditor *editor = GTK_COLOR_EDITOR (chooser); GtkColorEditor *editor = GTK_COLOR_EDITOR (chooser);
gdouble h, s, v; float h, s, v;
h = gtk_adjustment_get_value (editor->priv->h_adj); h = gtk_adjustment_get_value (editor->priv->h_adj);
s = gtk_adjustment_get_value (editor->priv->s_adj); s = gtk_adjustment_get_value (editor->priv->s_adj);
@ -569,7 +569,7 @@ gtk_color_editor_set_rgba (GtkColorChooser *chooser,
const GdkRGBA *color) const GdkRGBA *color)
{ {
GtkColorEditor *editor = GTK_COLOR_EDITOR (chooser); GtkColorEditor *editor = GTK_COLOR_EDITOR (chooser);
gdouble h, s, v; float h, s, v;
gtk_rgb_to_hsv (color->red, color->green, color->blue, &h, &s, &v); gtk_rgb_to_hsv (color->red, color->green, color->blue, &h, &s, &v);

View File

@ -149,11 +149,11 @@ portal_response_received (GDBusConnection *connection,
if (response == 0) if (response == 0)
{ {
GdkRGBA c; double d1, d2, d3;
c.alpha = 1.0; if (g_variant_lookup (ret, "color", "(ddd)", &d1, &d2, &d3))
if (g_variant_lookup (ret, "color", "(ddd)", &c.red, &c.green, &c.blue)) g_task_return_pointer (picker->task,
g_task_return_pointer (picker->task, gdk_rgba_copy (&c), (GDestroyNotify)gdk_rgba_free); gdk_rgba_copy (&(GdkRGBA){d1, d2, d3, 1.0f}), (GDestroyNotify)gdk_rgba_free);
else else
g_task_return_new_error (picker->task, g_task_return_new_error (picker->task,
G_IO_ERROR, G_IO_ERROR,

View File

@ -129,15 +129,15 @@ color_picked (GObject *source,
} }
else else
{ {
GdkRGBA c; double d1, d2, d3;
g_variant_get (ret, "(@a{sv})", &dict); g_variant_get (ret, "(@a{sv})", &dict);
c.alpha = 1; if (!g_variant_lookup (dict, "color", "(ddd)", &d1, &d2, &d3))
if (!g_variant_lookup (dict, "color", "(ddd)", &c.red, &c.green, &c.blue))
g_task_return_new_error (picker->task, G_IO_ERROR, G_IO_ERROR_FAILED, "No color received"); g_task_return_new_error (picker->task, G_IO_ERROR, G_IO_ERROR_FAILED, "No color received");
else else
g_task_return_pointer (picker->task, gdk_rgba_copy (&c), (GDestroyNotify)gdk_rgba_free); g_task_return_pointer (picker->task,
gdk_rgba_copy (&(GdkRGBA){d1, d2, d3, 1.0f}), (GDestroyNotify)gdk_rgba_free);
g_variant_unref (dict); g_variant_unref (dict);
g_variant_unref (ret); g_variant_unref (ret);

View File

@ -118,8 +118,8 @@ create_texture (GtkColorPlane *plane)
gint width, height, stride; gint width, height, stride;
guint red, green, blue; guint red, green, blue;
guint32 *data, *p; guint32 *data, *p;
gdouble h, s, v; float h, s, v;
gdouble r, g, b; float r, g, b;
gdouble sf, vf; gdouble sf, vf;
gint x, y; gint x, y;

View File

@ -78,7 +78,7 @@ gtk_color_scale_snapshot_trough (GtkColorScale *scale,
GBytes *bytes; GBytes *bytes;
guchar *data, *p; guchar *data, *p;
gdouble h; gdouble h;
gdouble r, g, b; float r, g, b;
gdouble f; gdouble f;
int hue_x, hue_y; int hue_x, hue_y;

View File

@ -39,12 +39,12 @@
/* Converts from HSV to RGB */ /* Converts from HSV to RGB */
static void static void
hsv_to_rgb (gdouble *h, hsv_to_rgb (float *h,
gdouble *s, float *s,
gdouble *v) float *v)
{ {
gdouble hue, saturation, value; float hue, saturation, value;
gdouble f, p, q, t; float f, p, q, t;
if (*s == 0.0) if (*s == 0.0)
{ {
@ -112,14 +112,14 @@ hsv_to_rgb (gdouble *h,
/* Converts from RGB to HSV */ /* Converts from RGB to HSV */
static void static void
rgb_to_hsv (gdouble *r, rgb_to_hsv (float *r,
gdouble *g, float *g,
gdouble *b) float *b)
{ {
gdouble red, green, blue; float red, green, blue;
gdouble h, s, v; float h, s, v;
gdouble min, max; float min, max;
gdouble delta; float delta;
red = *r; red = *r;
green = *g; green = *g;
@ -200,8 +200,8 @@ rgb_to_hsv (gdouble *r,
* output values will be in the same range. * output values will be in the same range.
*/ */
void void
gtk_hsv_to_rgb (gdouble h, gdouble s, gdouble v, gtk_hsv_to_rgb (float h, float s, float v,
gdouble *r, gdouble *g, gdouble *b) float *r, float *g, float *b)
{ {
g_return_if_fail (h >= 0.0 && h <= 1.0); g_return_if_fail (h >= 0.0 && h <= 1.0);
g_return_if_fail (s >= 0.0 && s <= 1.0); g_return_if_fail (s >= 0.0 && s <= 1.0);
@ -234,8 +234,8 @@ gtk_hsv_to_rgb (gdouble h, gdouble s, gdouble v,
* output values will be in the same range. * output values will be in the same range.
*/ */
void void
gtk_rgb_to_hsv (gdouble r, gdouble g, gdouble b, gtk_rgb_to_hsv (float r, float g, float b,
gdouble *h, gdouble *s, gdouble *v) float *h, float *s, float *v)
{ {
g_return_if_fail (r >= 0.0 && r <= 1.0); g_return_if_fail (r >= 0.0 && r <= 1.0);
g_return_if_fail (g >= 0.0 && g <= 1.0); g_return_if_fail (g >= 0.0 && g <= 1.0);

View File

@ -40,11 +40,11 @@
G_BEGIN_DECLS G_BEGIN_DECLS
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
void gtk_hsv_to_rgb (gdouble h, gdouble s, gdouble v, void gtk_hsv_to_rgb (float h, float s, float v,
gdouble *r, gdouble *g, gdouble *b); float *r, float *g, float *b);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
void gtk_rgb_to_hsv (gdouble r, gdouble g, gdouble b, void gtk_rgb_to_hsv (float r, float g, float b,
gdouble *h, gdouble *s, gdouble *v); float *h, float *s, float *v);
G_END_DECLS G_END_DECLS