mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
Merge pull request #3148 from radarhere/hsv
Added getrgb HSB/HSV color strings
This commit is contained in:
commit
e514a60d82
|
@ -78,10 +78,21 @@ class TestImageColor(PillowTestCase):
|
|||
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 @@ class TestImageColor(PillowTestCase):
|
|||
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 @@ class TestImageColor(PillowTestCase):
|
|||
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 @@ 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, "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):
|
||||
|
||||
|
|
|
@ -31,6 +31,13 @@ The ImageColor module supports the following string formats:
|
|||
(black=0%, normal=50%, white=100%). For example, ``hsl(0,100%,50%)`` is pure
|
||||
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. 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
|
||||
system and most web browsers. color names are case insensitive. For example,
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue
Block a user