Merge branch 'wip/color-parsing-improvements'

This commit is contained in:
Colomban Wendling 2013-12-03 03:09:41 +01:00
commit 3522e81d73
5 changed files with 44 additions and 63 deletions

View File

@ -219,9 +219,7 @@ static gboolean read_named_style(const gchar *named_style, GeanyLexerStyle *styl
static void parse_color(GKeyFile *kf, const gchar *str, gint *clr) static void parse_color(GKeyFile *kf, const gchar *str, gint *clr)
{ {
gint c; gint c;
gchar hex_clr[9] = { 0 };
gchar *named_color = NULL; gchar *named_color = NULL;
const gchar *start;
g_return_if_fail(clr != NULL); g_return_if_fail(clr != NULL);
@ -232,35 +230,13 @@ static void parse_color(GKeyFile *kf, const gchar *str, gint *clr)
if (named_color) if (named_color)
str = named_color; str = named_color;
if (str[0] == '#') c = utils_parse_color(str);
start = str + 1; if (c == -1)
else if (strlen(str) > 1 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
start = str + 2;
else
{
geany_debug("Bad color '%s'", str); geany_debug("Bad color '%s'", str);
g_free(named_color);
return;
}
if (strlen(start) == 3)
{
g_snprintf(hex_clr, 9, "0x%c%c%c%c%c%c", start[0], start[0],
start[1], start[1], start[2], start[2]);
}
else else
g_snprintf(hex_clr, 9, "0x%s", start);
g_free(named_color);
c = utils_strtod(hex_clr, NULL, FALSE);
if (c > -1)
{
*clr = c; *clr = c;
return;
} g_free(named_color);
geany_debug("Bad color '%s'", str);
} }

View File

@ -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) void sci_set_mark_long_lines(ScintillaObject *sci, gint type, gint column, const gchar *colour)
{ {
glong colour_val = utils_strtod(colour, NULL, TRUE); /* Scintilla uses a "long" value */ glong colour_val = utils_parse_color(colour); /* Scintilla uses a "long" value */
if (column == 0) if (column == 0)
type = 2; type = 2;

View File

@ -964,50 +964,55 @@ gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
} }
static guint utils_get_value_of_hex(const gchar ch) static gboolean read_hex(const gchar *s, guint len, gint *h)
{ {
if (ch >= '0' && ch <= '9') guint i;
return ch - '0'; *h = 0;
else if (ch >= 'A' && ch <= 'F') for (i = 0; i < len; i++)
return ch - 'A' + 10; {
else if (ch >= 'a' && ch <= 'f') if (! g_ascii_isxdigit(s[i]))
return ch - 'a' + 10; return FALSE;
else *h = (*h << 4) | g_ascii_xdigit_value(s[i]);
return 0; }
return TRUE;
} }
/* utils_strtod() converts a string containing a hex colour ("0x00ff00") into an integer. /* Converts a string containing an hex or HTML RGB color with 1 or 2 digits per
* Basically, it is the same as strtod() would do, but it does not understand hex colour values, * channel (0x00ff11, 0x0f1, #00ff11, #0f1) to an integer.
* before ANSI-C99. With with_route set, it takes strings of the format "#00ff00". * Returns an integer color in the format BBGGRR or -1 on failure. */
* Returns -1 on failure. */ gint utils_parse_color(const gchar *source)
gint utils_strtod(const gchar *source, gchar **end, gboolean with_route)
{ {
guint red, green, blue, offset = 0; gint r, g, b;
guint len;
g_return_val_if_fail(source != NULL, -1); g_return_val_if_fail(source != NULL, -1);
if (with_route && (strlen(source) != 7 || source[0] != '#')) if (source[0] == '#')
source++;
else if (source[0] == '0' && (source[1] == 'x' || source[1] == 'X'))
source += 2;
else
return -1; return -1;
else if (! with_route && (strlen(source) != 8 || source[0] != '0' ||
(source[1] != 'x' && source[1] != 'X'))) 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)
{ {
return -1; r |= r << 4;
g |= g << 4;
b |= b << 4;
} }
/* offset is set to 1 when the string starts with 0x, otherwise it starts with # return (r | (g << 8) | (b << 16));
* and we don't need to increase the index */
if (! with_route)
offset = 1;
red = utils_get_value_of_hex(
source[1 + offset]) * 16 + utils_get_value_of_hex(source[2 + offset]);
green = utils_get_value_of_hex(
source[3 + offset]) * 16 + utils_get_value_of_hex(source[4 + offset]);
blue = utils_get_value_of_hex(
source[5 + offset]) * 16 + utils_get_value_of_hex(source[6 + offset]);
return (red | (green << 8) | (blue << 16));
} }

View File

@ -220,7 +220,7 @@ void utils_beep(void);
gchar *utils_make_human_readable_str(guint64 size, gulong block_size, gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
gulong display_unit); gulong display_unit);
gint utils_strtod(const gchar *source, gchar **end, gboolean with_route); gint utils_parse_color(const gchar *source);
gchar *utils_get_current_time_string(void); gchar *utils_get_current_time_string(void);

View File

@ -565,7 +565,7 @@ void win32_show_color_dialog(const gchar *colour)
cc.lStructSize = sizeof(cc); cc.lStructSize = sizeof(cc);
cc.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets.window)); cc.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets.window));
cc.lpCustColors = (LPDWORD) acr_cust_clr; cc.lpCustColors = (LPDWORD) acr_cust_clr;
cc.rgbResult = (colour != NULL) ? utils_strtod(colour, NULL, colour[0] == '#') : 0; cc.rgbResult = (colour != NULL) ? utils_parse_color(colour) : 0;
cc.Flags = CC_FULLOPEN | CC_RGBINIT; cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&cc)) if (ChooseColor(&cc))