Added getrgb hsb color string

This commit is contained in:
Andrew Murray 2018-05-31 06:14:29 +10:00
parent b50f63430f
commit 3d82672404
3 changed files with 10 additions and 2 deletions

View File

@ -82,6 +82,10 @@ class TestImageColor(PillowTestCase):
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%)"))
@ -100,6 +104,8 @@ class TestImageColor(PillowTestCase):
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),

View File

@ -34,7 +34,9 @@ The ImageColor module supports the following string formats:
* Hue-Saturation-Value (HSV) functions, given as ``hsv(hue, saturation%,
value%)`` where hue and saturation are the same as HSL, and value is between
0% and 100% (black=0%, normal=100%). For example, ``hsv(0,100%,100%)`` is
pure red.
pure red. This format is also known as Hue-Saturation-Brightness (HSB), and
can be given as ``hsb(hue, saturation%, brightness%)``, where each of the
values are used as they are in HSV.
* Common HTML color names. The :py:mod:`~PIL.ImageColor` module provides some
140 standard color names, based on the colors supported by the X Window

View File

@ -101,7 +101,7 @@ def getrgb(color):
int(rgb[2] * 255 + 0.5)
)
m = re.match(r"hsv\(\s*(\d+\.?\d*)\s*,\s*(\d+\.?\d*)%\s*,\s*(\d+\.?\d*)%\s*\)$", color)
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(