Added getrgb hsv color string

This commit is contained in:
Andrew Murray 2018-05-31 06:13:22 +10:00
parent 06efe1826b
commit b50f63430f
3 changed files with 36 additions and 0 deletions

View File

@ -78,10 +78,17 @@ class TestImageColor(PillowTestCase):
self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360,100%,50%)")) self.assertEqual((255, 0, 0), ImageColor.getrgb("hsl(360,100%,50%)"))
self.assertEqual((0, 255, 255), ImageColor.getrgb("hsl(180,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%)"))
# floats # floats
self.assertEqual((254, 3, 3), ImageColor.getrgb("hsl(0.1,99.2%,50.3%)")) 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((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 # case insensitivity
self.assertEqual(ImageColor.getrgb("RGB(255,0,0)"), self.assertEqual(ImageColor.getrgb("RGB(255,0,0)"),
ImageColor.getrgb("rgb(255,0,0)")) ImageColor.getrgb("rgb(255,0,0)"))
@ -91,6 +98,8 @@ class TestImageColor(PillowTestCase):
ImageColor.getrgb("rgba(255,0,0,0)")) ImageColor.getrgb("rgba(255,0,0,0)"))
self.assertEqual(ImageColor.getrgb("HSL(0,100%,50%)"), self.assertEqual(ImageColor.getrgb("HSL(0,100%,50%)"),
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%)"))
# space agnosticism # space agnosticism
self.assertEqual((255, 0, 0), self.assertEqual((255, 0, 0),
@ -101,6 +110,8 @@ class TestImageColor(PillowTestCase):
ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )")) ImageColor.getrgb("rgba( 255 , 0 , 0 , 0 )"))
self.assertEqual((255, 0, 0), self.assertEqual((255, 0, 0),
ImageColor.getrgb("hsl( 0 , 100% , 50% )")) ImageColor.getrgb("hsl( 0 , 100% , 50% )"))
self.assertEqual((255, 0, 0),
ImageColor.getrgb("hsv( 0 , 100% , 100% )"))
# wrong number of components # wrong number of components
self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0)") self.assertRaises(ValueError, ImageColor.getrgb, "rgb(255,0)")
@ -120,6 +131,12 @@ class TestImageColor(PillowTestCase):
self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100,50%)") self.assertRaises(ValueError, ImageColor.getrgb, "hsl(0,100,50%)")
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) # look for rounding errors (based on code by Tim Hatch)
def test_rounding_errors(self): def test_rounding_errors(self):

View File

@ -31,6 +31,11 @@ The ImageColor module supports the following string formats:
(black=0%, normal=50%, white=100%). For example, ``hsl(0,100%,50%)`` is pure (black=0%, normal=50%, white=100%). For example, ``hsl(0,100%,50%)`` is pure
red. red.
* 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.
* Common HTML color names. The :py:mod:`~PIL.ImageColor` module provides some * 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 140 standard color names, based on the colors supported by the X Window
system and most web browsers. color names are case insensitive. For example, system and most web browsers. color names are case insensitive. For example,

View File

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