[geany/geany] 898097: Unify color parsing

Colomban Wendling git-noreply at xxxxx
Wed Dec 4 14:31:17 UTC 2013


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Wed, 04 Dec 2013 14:31:17 UTC
Commit:      898097063098b5ec73611f5d0359d963922b9dd3
             https://github.com/geany/geany/commit/898097063098b5ec73611f5d0359d963922b9dd3

Log Message:
-----------
Unify color parsing

Change utils_parse_color() to use gdk_color_parse() and follow its
syntax, additionally supporting our "0x" prefix as a synonym for the
"#" prefix;  and use this everywhere.

Also add utils_color_to_bgr() and utils_parse_color_to_bgr() to provide
conversion to the 24 bits BGR format used by Scintilla.


Modified Paths:
--------------
    src/highlighting.c
    src/keyfile.c
    src/prefs.c
    src/sciwrappers.c
    src/tools.c
    src/utils.c
    src/utils.h
    src/win32.c

Modified: src/highlighting.c
2 files changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -230,7 +230,7 @@ static void parse_color(GKeyFile *kf, const gchar *str, gint *clr)
 	if (named_color)
 		str = named_color;
 
-	c = utils_parse_color(str);
+	c = utils_parse_color_to_bgr(str);
 	if (c == -1)
 		geany_debug("Bad color '%s'", str);
 	else


Modified: src/keyfile.c
4 files changed, 2 insertions(+), 2 deletions(-)
===================================================================
@@ -857,10 +857,10 @@ static void load_dialog_prefs(GKeyFile *config)
 		vc->colour_fore = g_new0(GdkColor, 1);
 		vc->colour_back = g_new0(GdkColor, 1);
 		tmp_string = utils_get_setting_string(config, "VTE", "colour_fore", "#ffffff");
-		gdk_color_parse(tmp_string, vc->colour_fore);
+		utils_parse_color(tmp_string, vc->colour_fore);
 		g_free(tmp_string);
 		tmp_string = utils_get_setting_string(config, "VTE", "colour_back", "#000000");
-		gdk_color_parse(tmp_string, vc->colour_back);
+		utils_parse_color(tmp_string, vc->colour_back);
 		g_free(tmp_string);
 	}
 #endif


Modified: src/prefs.c
2 files changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -477,7 +477,7 @@ static void prefs_init_dialog(void)
 	}
 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
 
-	gdk_color_parse(editor_prefs.long_line_color, &color);
+	utils_parse_color(editor_prefs.long_line_color, &color);
 	widget = ui_lookup_widget(ui_widgets.prefs_dialog, "long_line_color");
 	gtk_color_button_set_color(GTK_COLOR_BUTTON(widget), &color);
 


Modified: src/sciwrappers.c
2 files changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -69,7 +69,7 @@ void sci_set_line_numbers(ScintillaObject *sci, gboolean set, gint extra_width)
 
 void sci_set_mark_long_lines(ScintillaObject *sci, gint type, gint column, const gchar *colour)
 {
-	glong colour_val = utils_parse_color(colour); /* Scintilla uses a "long" value */
+	glong colour_val = utils_parse_color_to_bgr(colour); /* Scintilla uses a "long" value */
 
 	if (column == 0)
 		type = 2;


Modified: src/tools.c
12 files changed, 2 insertions(+), 10 deletions(-)
===================================================================
@@ -980,7 +980,7 @@ void tools_color_chooser(const gchar *color)
 #ifdef G_OS_WIN32
 	win32_show_color_dialog(color);
 #else
-	gchar *c = (gchar*) color;
+	GdkColor gc;
 	GtkWidget *colorsel;
 
 	if (ui_widgets.open_colorsel == NULL)
@@ -999,16 +999,8 @@ void tools_color_chooser(const gchar *color)
 	else
 		colorsel = gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(ui_widgets.open_colorsel));
 	/* if color is non-NULL set it in the dialog as preselected color */
-	if (c != NULL && (c[0] == '0' || c[0] == '#'))
+	if (color != NULL && utils_parse_color(color, &gc))
 	{
-		GdkColor gc;
-
-		if (c[0] == '0' && c[1] == 'x')
-		{	/* we have a string of the format "0x00ff00" and we need it to "#00ff00" */
-			c[1] = '#';
-			c++;
-		}
-		gdk_color_parse(c, &gc);
 		gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &gc);
 		gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &gc);
 	}


Modified: src/utils.c
65 files changed, 27 insertions(+), 38 deletions(-)
===================================================================
@@ -964,55 +964,44 @@ gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
 }
 
 
-static gboolean read_hex(const gchar *s, guint len, gint *h)
+/* converts a color representation using gdk_color_parse(), with additional
+ * support of the "0x" prefix as a synonym for "#" */
+gboolean utils_parse_color(const gchar *spec, GdkColor *color)
 {
-	guint i;
-	*h = 0;
-	for (i = 0; i < len; i++)
+	gchar buf[64] = {0};
+
+	g_return_val_if_fail(spec != NULL, -1);
+
+	if (spec[0] == '0' && (spec[1] == 'x' || spec[1] == 'X'))
 	{
-		if (! g_ascii_isxdigit(s[i]))
-			return FALSE;
-		*h = (*h << 4) | g_ascii_xdigit_value(s[i]);
+		/* convert to # format for GDK to understand it */
+		buf[0] = '#';
+		strncpy(buf + 1, spec + 2, sizeof(buf) - 2);
+		spec = buf;
 	}
-	return TRUE;
+
+	return gdk_color_parse(spec, color);
 }
 
 
-/* Converts a string containing an hex or HTML RGB color with 1 or 2 digits per
- * channel (0x00ff11, 0x0f1, #00ff11, #0f1) to an integer.
- * Returns an integer color in the format BBGGRR or -1 on failure. */
-gint utils_parse_color(const gchar *source)
+/* converts a GdkColor to the packed 24 bits BGR format, as understood by Scintilla
+ * returns a 24 bits BGR color, or -1 on failure */
+gint utils_color_to_bgr(const GdkColor *c)
 {
-	gint r, g, b;
-	guint len;
+	g_return_val_if_fail(c != NULL, -1);
+	return (c->red / 256) | ((c->green / 256) << 8) | ((c->blue / 256) << 16);
+}
 
-	g_return_val_if_fail(source != NULL, -1);
 
-	if (source[0] == '#')
-		source++;
-	else if (source[0] == '0' && (source[1] == 'x' || source[1] == 'X'))
-		source += 2;
+/* parses @p spec using utils_parse_color() and convert it to 24 bits BGR using
+ * utils_color_to_bgr() */
+gint utils_parse_color_to_bgr(const gchar *spec)
+{
+	GdkColor color;
+	if (utils_parse_color(spec, &color))
+		return utils_color_to_bgr(&color);
 	else
 		return -1;
-
-	len = strlen(source);
-	if (len % 3 || len < 3 || len > 6)
-		return -1;
-	len /= 3;
-
-	if (! read_hex(source, len, &r) ||
-		! read_hex(source + len, len, &g) ||
-		! read_hex(source + len * 2, len, &b))
-		return -1;
-
-	if (len < 2)
-	{
-		r |= r << 4;
-		g |= g << 4;
-		b |= b << 4;
-	}
-
-	return (r | (g << 8) | (b << 16));
 }
 
 


Modified: src/utils.h
6 files changed, 5 insertions(+), 1 deletions(-)
===================================================================
@@ -220,7 +220,11 @@ guint utils_string_regex_replace_all(GString *haystack, GRegex *regex,
 gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
 									 gulong display_unit);
 
-gint utils_parse_color(const gchar *source);
+gboolean utils_parse_color(const gchar *spec, GdkColor *color);
+
+gint utils_color_to_bgr(const GdkColor *color);
+
+gint utils_parse_color_to_bgr(const gchar *spec);
 
 gchar *utils_get_current_time_string(void);
 


Modified: src/win32.c
2 files changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -565,7 +565,7 @@ void win32_show_color_dialog(const gchar *colour)
 	cc.lStructSize = sizeof(cc);
 	cc.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets.window));
 	cc.lpCustColors = (LPDWORD) acr_cust_clr;
-	cc.rgbResult = (colour != NULL) ? utils_parse_color(colour) : 0;
+	cc.rgbResult = (colour != NULL) ? utils_parse_color_to_bgr(colour) : 0;
 	cc.Flags = CC_FULLOPEN | CC_RGBINIT;
 
 	if (ChooseColor(&cc))



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list