Skip to content

Commit

Permalink
Unify color parsing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
b4n committed Dec 4, 2013
1 parent 1590007 commit 8980970
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/highlighting.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/keyfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/prefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/sciwrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 2 additions & 10 deletions src/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
65 changes: 27 additions & 38 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down
6 changes: 5 additions & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ void utils_beep(void);
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);

Expand Down
2 changes: 1 addition & 1 deletion src/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 8980970

Please sign in to comment.