[geany/geany] ebde42: Accept colors with only 1 digit per channel in utils_parse_color()

Colomban Wendling git-noreply at xxxxx
Mon Dec 2 21:53:35 UTC 2013


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Mon, 02 Dec 2013 21:53:35 UTC
Commit:      ebde42617a5c4e57ae0d717b56a03201f51e0988
             https://github.com/geany/geany/commit/ebde42617a5c4e57ae0d717b56a03201f51e0988

Log Message:
-----------
Accept colors with only 1 digit per channel in utils_parse_color()

AKA short HTML color notation.


Modified Paths:
--------------
    src/utils.c

Modified: src/utils.c
25 files changed, 18 insertions(+), 7 deletions(-)
===================================================================
@@ -978,11 +978,13 @@ static gboolean read_hex(const gchar *s, guint len, gint *h)
 }
 
 
-/* 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
+ * 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)
 {
-	gint red, green, blue;
+	gint r, g, b;
+	guint len;
 
 	g_return_val_if_fail(source != NULL, -1);
 
@@ -993,15 +995,24 @@ gint utils_parse_color(const gchar *source)
 	else
 		return -1;
 
-	if (strlen(source) != 6)
+	len = strlen(source);
+	if (len % 3 || len < 3 || len > 6)
 		return -1;
+	len /= 3;
 
-	if (! read_hex(source, 2, &red) ||
-		! read_hex(source + 2, 2, &green) ||
-		! read_hex(source + 4, 2, &blue))
+	if (! read_hex(source, len, &r) ||
+		! read_hex(source + len, len, &g) ||
+		! read_hex(source + len * 2, len, &b))
 		return -1;
 
-	return (red | (green << 8) | (blue << 16));
+	if (len < 2)
+	{
+		r |= r << 4;
+		g |= g << 4;
+		b |= b << 4;
+	}
+
+	return (r | (g << 8) | (b << 16));
 }
 
 



--------------
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