From af8cae4e6ffd844fa32f28207cd1d5ce44593e4c Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Sun, 24 Aug 2014 11:36:31 +0200 Subject: [PATCH] get ride of integer representation of the colors, no more needed since gtk chooser used with float representation. --- src/cfg.c | 4 ++-- src/graph.c | 20 ++++++++++---------- src/lib/color.c | 32 ++++++++++++-------------------- src/lib/color.h | 23 ++++++----------------- src/main.c | 20 ++++++++++---------- src/ui_color.c | 2 +- src/ui_pref.c | 14 +++++++++----- src/ui_sensorlist.c | 5 +++-- src/ui_sensorpref.c | 7 +++++-- 9 files changed, 58 insertions(+), 69 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index bb69d7a..265f84c 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -193,7 +193,7 @@ static struct color *get_background_color() free(scolor); if (!c) - return color_new(0xffff, 0xffff, 0xffff); + return color_new(1, 1, 1); return c; } @@ -209,7 +209,7 @@ static struct color *get_foreground_color() free(scolor); if (!c) - return color_new(0x0000, 0x0000, 0x0000); + return color_new(0, 0, 0); return c; } diff --git a/src/graph.c b/src/graph.c index 7205e63..ab507c0 100644 --- a/src/graph.c +++ b/src/graph.c @@ -111,15 +111,15 @@ draw_graph_background(cairo_t *cr, cairo_fill(cr); if (config->alpha_channel_enabled) cairo_set_source_rgba(cr, - bgcolor->f_red, - bgcolor->f_green, - bgcolor->f_blue, + bgcolor->red, + bgcolor->green, + bgcolor->blue, config->graph_bg_alpha); else cairo_set_source_rgb(cr, - bgcolor->f_red, - bgcolor->f_green, - bgcolor->f_blue); + bgcolor->red, + bgcolor->green, + bgcolor->blue); cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height); cairo_fill(cr); @@ -144,7 +144,7 @@ static void draw_background_lines(cairo_t *cr, cairo_set_line_width(cr, 1); cairo_set_dash(cr, dashes, ndash, 0); cairo_set_source_rgb(cr, - color->f_red, color->f_green, color->f_blue); + color->red, color->green, color->blue); /* vertical lines representing time steps */ for (i = 0; i <= 5; i++) { @@ -184,9 +184,9 @@ static void draw_sensor_curve(struct psensor *s, double v; cairo_set_source_rgb(cr, - s->color->f_red, - s->color->f_green, - s->color->f_blue); + s->color->red, + s->color->green, + s->color->blue); dt = et - bt; first = 1; diff --git a/src/lib/color.c b/src/lib/color.c index 54a5ef6..8474c72 100644 --- a/src/lib/color.c +++ b/src/lib/color.c @@ -23,33 +23,20 @@ #include "color.h" -void color_set(struct color *c, unsigned int r, unsigned int g, unsigned int b) +void color_set(struct color *c, double r, double g, double b) { c->red = r; c->green = g; c->blue = b; - - c->f_red = ((double)r) / 65535; - c->f_green = ((double)g) / 65535; - c->f_blue = ((double)b) / 65535; } -void color_set_f(struct color *c, double r, double g, double b) +struct color *color_new(double r, double g, double b) { - c->f_red = r; - c->f_green = g; - c->f_blue = b; - - c->red = 65535 * r; - c->green = 65535 * g; - c->blue = 65535 * b; -} + struct color *color; -struct color *color_new(unsigned int red, unsigned int green, unsigned int blue) -{ - struct color *color = malloc(sizeof(struct color)); + color = malloc(sizeof(struct color)); - color_set(color, red, green, blue); + color_set(color, r, g, b); return color; } @@ -94,14 +81,19 @@ struct color *str_to_color(const char *str) tmp[4] = '\0'; blue = strtol(tmp, NULL, 16); - return color_new(red, green, blue); + return color_new(((double)red)/65535, + ((double)green)/65535, + ((double)blue)/65535); } char *color_to_str(const struct color *color) { char *str = malloc(1 + 12 + 1); - sprintf(str, "#%.4x%.4x%.4x", color->red, color->green, color->blue); + sprintf(str, "#%.4x%.4x%.4x", + (int)(65535 * color->red), + (int)(65535 * color->green), + (int)(65535 * color->blue)); return str; } diff --git a/src/lib/color.h b/src/lib/color.h index 3af8deb..8d11558 100644 --- a/src/lib/color.h +++ b/src/lib/color.h @@ -29,30 +29,19 @@ * representation. */ struct color { - /* rgb 0..65535 */ - unsigned int red; - unsigned int green; - unsigned int blue; - /* rgb floating 0..1 */ - double f_red; - double f_green; - double f_blue; + double red; + double green; + double blue; }; -/** rgb 0..65535 */ -struct color *color_new(unsigned int r, unsigned int g, unsigned int b); +/** rgb 0..1 */ +struct color *color_new(double r, double g, double b); struct color *color_dup(struct color *); -/** rgb 0..65535 */ -void color_set(struct color *, - unsigned int r, - unsigned int g, - unsigned int b); - /** rgb 0..1 */ -void color_set_f(struct color *, double r, double g, double b); +void color_set(struct color *, double r, double g, double b); int is_color(const char *str); diff --git a/src/main.c b/src/main.c index 258b58b..e9b10b2 100644 --- a/src/main.c +++ b/src/main.c @@ -254,16 +254,16 @@ static void associate_colors(struct psensor **sensors) /* number of uniq colors */ #define COLORS_COUNT 8 - unsigned int colors[COLORS_COUNT][3] = { - {0x0000, 0x0000, 0x0000}, /* black */ - {0xffff, 0x0000, 0x0000}, /* red */ - {0x0000, 0x0000, 0xffff}, /* blue */ - {0x0000, 0xffff, 0x0000}, /* green */ - - {0x7fff, 0x7fff, 0x7fff}, /* grey */ - {0x7fff, 0x0000, 0x0000}, /* dark red */ - {0x0000, 0x0000, 0x7fff}, /* dark blue */ - {0x0000, 0x7fff, 0x0000} /* dark green */ + double colors[COLORS_COUNT][3] = { + {0, 0, 0}, /* black */ + {1, 0, 0}, /* red */ + {0, 0, 1}, /* blue */ + {0, 1, 0}, /* green */ + + {0.5, 0.5, 0.5},/* grey */ + {0.5, 0, 0}, /* dark red */ + {0, 0, 0.5}, /* dark blue */ + {0, 0.5, 0} /* dark green */ }; struct psensor **cur; int i; diff --git a/src/ui_color.c b/src/ui_color.c index d7f6eff..85277ff 100644 --- a/src/ui_color.c +++ b/src/ui_color.c @@ -63,7 +63,7 @@ int ui_change_color(const char *title, struct color *col, GtkWindow *win) else b = color.blue; - color_set(col, 65535*r, 65535*g, 65535*b); + color_set(col, r, g, b); } gtk_widget_destroy(GTK_WIDGET(colordlg)); diff --git a/src/ui_pref.c b/src/ui_pref.c index 7469aef..e614da3 100644 --- a/src/ui_pref.c +++ b/src/ui_pref.c @@ -29,9 +29,9 @@ GdkRGBA color_to_GdkRGBA(struct color *color) { GdkRGBA c; - c.red = color->f_red; - c.green = color->f_green; - c.blue = color->f_blue; + c.red = color->red; + c.green = color->green; + c.blue = color->blue; c.alpha = 1.0; return c; @@ -179,11 +179,15 @@ void ui_pref_dialog_run(struct ui_psensor *ui) gtk_color_chooser_get_rgba(w_color_fg, &color); color_set(cfg->graph_fgcolor, - color.red, color.green, color.blue); + color.red, + color.green, + color.blue); gtk_color_chooser_get_rgba(w_color_bg, &color); color_set(cfg->graph_bgcolor, - color.red, color.green, color.blue); + color.red, + color.green, + color.blue); value = gtk_range_get_value(GTK_RANGE(w_bg_opacity)); cfg->graph_bg_alpha = value; diff --git a/src/ui_sensorlist.c b/src/ui_sensorlist.c index 03f9898..d0138c7 100644 --- a/src/ui_sensorlist.c +++ b/src/ui_sensorlist.c @@ -58,7 +58,7 @@ static void populate(struct ui_psensor *ui) { GtkTreeIter iter; GtkListStore *store; - GdkColor color; + GdkRGBA color; char *scolor; struct psensor **ordered_sensors, **s_cur, *s; unsigned int enabled; @@ -76,8 +76,9 @@ static void populate(struct ui_psensor *ui) color.red = s->color->red; color.green = s->color->green; color.blue = s->color->blue; + color.alpha = 1.0; - scolor = gdk_color_to_string(&color); + scolor = gdk_rgba_to_string(&color); enabled = config_is_sensor_enabled(s->id); diff --git a/src/ui_sensorpref.c b/src/ui_sensorpref.c index 715eed5..126f33c 100644 --- a/src/ui_sensorpref.c +++ b/src/ui_sensorpref.c @@ -190,7 +190,7 @@ void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data) if (p) { gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color); - color_set_f(p->color, color.red, color.green, color.blue); + color_set(p->color, color.red, color.green, color.blue); } } @@ -385,7 +385,10 @@ static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg) config_set_sensor_alarm_enabled(s->id, s->alarm_enabled); } - color_set(s->color, p->color->red, p->color->green, p->color->blue); + color_set(s->color, + p->color->red, + p->color->green, + p->color->blue); config_set_sensor_color(s->id, s->color); if (s->appindicator_enabled != p->appindicator_enabled) { -- 2.7.4