[GRASS-SVN] r69683 - in grass/trunk/lib/gis: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 6 16:45:34 PDT 2016


Author: wenzeslaus
Date: 2016-10-06 16:45:34 -0700 (Thu, 06 Oct 2016)
New Revision: 69683

Modified:
   grass/trunk/lib/gis/color_str.c
   grass/trunk/lib/gis/testsuite/gis_lib_str_color.py
Log:
libgis: support also HTML/CSS hash hexadecimal colors in G_str_to_color() (missing docs) [news]

Modified: grass/trunk/lib/gis/color_str.c
===================================================================
--- grass/trunk/lib/gis/color_str.c	2016-10-06 21:44:43 UTC (rev 69682)
+++ grass/trunk/lib/gis/color_str.c	2016-10-06 23:45:34 UTC (rev 69683)
@@ -131,6 +131,19 @@
 	return 1;
     }
 
+    int hex;
+
+    if (sscanf(buf, "#%x", &hex) == 1) {
+	*red = (hex >> 16) & 0xFF;
+	*grn = (hex >> 8) & 0xFF;
+	*blu = hex & 0xFF;
+	if (*red < 0 || *red > 255 ||
+	    *grn < 0 || *grn > 255 || *blu < 0 || *blu > 255)
+	    return 0;
+
+	return 1;
+    }
+
     /* Look for this color in the standard (preallocated) colors */
     for (i = 0; i < num_names; i++) {
 	const struct color_name *name = &standard_color_names[i];

Modified: grass/trunk/lib/gis/testsuite/gis_lib_str_color.py
===================================================================
--- grass/trunk/lib/gis/testsuite/gis_lib_str_color.py	2016-10-06 21:44:43 UTC (rev 69682)
+++ grass/trunk/lib/gis/testsuite/gis_lib_str_color.py	2016-10-06 23:45:34 UTC (rev 69683)
@@ -64,6 +64,39 @@
         """Test with whitespace (spaces) around the string"""
         self.convert_color("  50:150:250    ", 50, 150, 250)
 
+    def test_html_hash_hex(self):
+        """Test HTML format with hash and hexadecimal (6 letters, #RRGGBB)"""
+        self.convert_color("#3296FA", 50, 150, 250)
+
+    def test_html_hash_hex_more_colors(self):
+        """Test HTML format with more colors"""
+        self.convert_color("#A6CEE3", 166, 206, 227)
+        self.convert_color("#33A02C", 51, 160, 44)
+        self.convert_color("#FB9A99", 251, 154, 153)
+        self.convert_color("#FF7F00", 255, 127, 0)
+
+    def test_html_hash_hex_more_colors_lowercase(self):
+        """Test HTML format with lowercase letters"""
+        self.convert_color("#a6cee3", 166, 206, 227)
+        self.convert_color("#33a02c", 51, 160, 44)
+        self.convert_color("#fb9a99", 251, 154, 153)
+        self.convert_color("#ff7f00", 255, 127, 0)
+
+    def test_html_hash_hex_more_colors_mixedcase(self):
+        """Test HTML format with mixed case letters"""
+        self.convert_color("#a6CeE3", 166, 206, 227)
+        self.convert_color("#33a02C", 51, 160, 44)
+        self.convert_color("#fB9A99", 251, 154, 153)
+        self.convert_color("#Ff7f00", 255, 127, 0)
+
+    def test_html_hash_hex_black(self):
+        """Test HTML format black color"""
+        self.convert_color("#000000", 0, 0, 0)
+
+    def test_html_hash_hex_white(self):
+        """Test HTML format white color"""
+        self.convert_color("#FFFFFF", 255, 255, 255)
+
     def test_grass_named_black(self):
         """Test GRASS GIS color black color"""
         self.convert_color("black", 0, 0, 0)



More information about the grass-commit mailing list