Repository: python-pillow/Pillow

Issue:
PIL.ImageColor.getrgb add support for HSB/HSV format
The current `PIL.ImageColor.getrgb` does not support [HSB/HSV](https://en.wikipedia.org/wiki/HSL_and_HSV) format, only the related HSL, but for easier conversion of some coordinates it would be nice to have support for HSB/HSV also.



Discussion:

Resolving patch:
diff --git a/src/PIL/ImageColor.py b/src/PIL/ImageColor.py
--- a/src/PIL/ImageColor.py
+++ b/src/PIL/ImageColor.py
@@ -101,6 +101,20 @@ def getrgb(color):
             int(rgb[2] * 255 + 0.5)
             )
 
+    m = re.match(r"hs[bv]\(\s*(\d+\.?\d*)\s*,\s*(\d+\.?\d*)%\s*,\s*(\d+\.?\d*)%\s*\)$", color)
+    if m:
+        from colorsys import hsv_to_rgb
+        rgb = hsv_to_rgb(
+            float(m.group(1)) / 360.0,
+            float(m.group(2)) / 100.0,
+            float(m.group(3)) / 100.0,
+            )
+        return (
+            int(rgb[0] * 255 + 0.5),
+            int(rgb[1] * 255 + 0.5),
+            int(rgb[2] * 255 + 0.5)
+            )
+
     m = re.match(r"rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$",
                  color)
     if m:


Test patch:
diff --git a/Tests/test_imagecolor.py b/Tests/test_imagecolor.py
--- a/Tests/test_imagecolor.py
+++ b/Tests/test_imagecolor.py
@@ -78,10 +78,21 @@ def test_functions(self):
         self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360,100%,50%)"))
         self.assertEqual((0, 255, 255), ImageColor.getrgb("hsl(180,100%,50%)"))
 
+        self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(0,100%,100%)"))
+        self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(360,100%,100%)"))
+        self.assertEqual((0, 255, 255), ImageColor.getrgb("hsv(180,100%,100%)"))
+
+        # alternate format
+        self.assertEqual(ImageColor.getrgb("hsb(0,100%,50%)"),
+                         ImageColor.getrgb("hsv(0,100%,50%)"))
+
         # floats
         self.assertEqual((254, 3, 3), ImageColor.getrgb("hsl(0.1,99.2%,50.3%)"))
         self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360.,100.0%,50%)"))
 
+        self.assertEqual((253, 2, 2), ImageColor.getrgb("hsv(0.1,99.2%,99.3%)"))
+        self.assertEqual((255, 0, 0), ImageColor.getrgb("hsv(360.,100.0%,100%)"))
+
         # case insensitivity
         self.assertEqual(ImageColor.getrgb("RGB(255,0,0)"),
                          ImageColor.getrgb("rgb(255,0,0)"))
@@ -91,6 +102,10 @@ def test_functions(self):
                          ImageColor.getrgb("rgba(255,0,0,0)"))
         self.assertEqual(ImageColor.getrgb("HSL(0,100%,50%)"),
                          ImageColor.getrgb("hsl(0,100%,50%)"))
+        self.assertEqual(ImageColor.getrgb("HSV(0,100%,50%)"),
+                         ImageColor.getrgb("hsv(0,100%,50%)"))
+        self.assertEqual(ImageColor.getrgb("HSB(0,100%,50%)"),
+                         ImageColor.getrgb("hsb(0,100%,50%)"))
 
         # space agnosticism
         self.assertEqual((255, 0, 0),
@@ -101,6 +116,8 @@ def test_functions(self):
                          ImageColor.getrgb("rgba(  255  ,  0  ,  0  ,  0  )"))
         self.assertEqual((255, 0, 0),
                          ImageColor.getrgb("hsl(  0  ,  100%  ,  50%  )"))
+        self.assertEqual((255, 0, 0),
+                         ImageColor.getrgb("hsv(  0  ,  100%  ,  100%  )"))
 
         # wrong number of components
         self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0)")
@@ -120,6 +137,12 @@ def test_functions(self):
         self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100,50%)")
         self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100%,50)")
 
+        self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%)")
+        self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%,0%,0%)")
+        self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0%,100%,50%)")
+        self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100,50%)")
+        self.assertRaises(ValueError, ImageColor.getrgb, "hsv(0,100%,50)")
+
     # look for rounding errors (based on code by Tim Hatch)
     def test_rounding_errors(self):