Rename utils_strtod() to utils_parse_color()

Rename the function to a more sensible name, and remove unnecessary
arguments.  Now, format (# or 0x) is auto-detected.
This commit is contained in:
Colomban Wendling 2013-12-02 22:38:11 +01:00
parent f99e627bb0
commit 507005ca54
5 changed files with 18 additions and 23 deletions

View File

@ -253,7 +253,7 @@ static void parse_color(GKeyFile *kf, const gchar *str, gint *clr)
g_free(named_color);
c = utils_strtod(hex_clr, NULL, FALSE);
c = utils_parse_color(hex_clr);
if (c > -1)
{

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)
{
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)
type = 2;

View File

@ -977,35 +977,30 @@ gchar *utils_make_human_readable_str(guint64 size, gulong block_size,
}
/* utils_strtod() converts a string containing a hex colour ("0x00ff00") into an integer.
* Basically, it is the same as strtod() would do, but it does not understand hex colour values,
* before ANSI-C99. With with_route set, it takes strings of the format "#00ff00".
* Returns -1 on failure. */
gint utils_strtod(const gchar *source, gchar **end, gboolean with_route)
/* Converts a string containing a hex colour ("0x00ff00") into an integer.
* Returns an integer color in the format BBGGRR or -1 on failure. */
gint utils_parse_color(const gchar *source)
{
guint red, green, blue, offset = 0;
guint red, green, blue;
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;
else if (! with_route && (strlen(source) != 8 || source[0] != '0' ||
(source[1] != 'x' && source[1] != 'X')))
{
return -1;
}
/* offset is set to 1 when the string starts with 0x, otherwise it starts with #
* and we don't need to increase the index */
if (! with_route)
offset = 1;
if (strlen(source) != 6)
return -1;
red = utils_get_value_of_hex(
source[1 + offset]) * 16 + utils_get_value_of_hex(source[2 + offset]);
source[0]) * 16 + utils_get_value_of_hex(source[1]);
green = utils_get_value_of_hex(
source[3 + offset]) * 16 + utils_get_value_of_hex(source[4 + offset]);
source[2]) * 16 + utils_get_value_of_hex(source[3]);
blue = utils_get_value_of_hex(
source[5 + offset]) * 16 + utils_get_value_of_hex(source[6 + offset]);
source[4]) * 16 + utils_get_value_of_hex(source[5]);
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,
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);

View File

@ -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_strtod(colour, NULL, colour[0] == '#') : 0;
cc.rgbResult = (colour != NULL) ? utils_parse_color(colour) : 0;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&cc))