Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_as_hsl():
assert Color('bad').as_hsl() == 'hsl(260, 43%, 77%)'
assert Color((1, 2, 3, 0.123456)).as_hsl() == 'hsl(210, 50%, 1%, 0.12)'
assert Color('hsl(260, 43%, 77%)').as_hsl() == 'hsl(260, 43%, 77%)'
def test_color_fail(color):
with pytest.raises(ColorError):
Color(color)
def test_as_hsl():
assert Color('bad').as_hsl() == 'hsl(260, 43%, 77%)'
assert Color((1, 2, 3, 0.123456)).as_hsl() == 'hsl(210, 50%, 1%, 0.12)'
assert Color('hsl(260, 43%, 77%)').as_hsl() == 'hsl(260, 43%, 77%)'
def test_str_repr():
assert str(Color('red')) == 'red'
assert repr(Color('red')) == "Color('red', rgb=(255, 0, 0))"
assert str(Color((1, 2, 3))) == '#010203'
assert repr(Color((1, 2, 3))) == "Color('#010203', rgb=(1, 2, 3))"
def test_as_named():
assert Color((0, 255, 255)).as_named() == 'cyan'
assert Color('#808000').as_named() == 'olive'
assert Color('hsl(180, 100%, 50%)').as_named() == 'cyan'
assert Color((240, 248, 255)).as_named() == 'aliceblue'
with pytest.raises(ValueError) as exc_info:
Color((1, 2, 3)).as_named()
assert exc_info.value.args[0] == 'no named color found, use fallback=True, as_hex() or as_rgb()'
assert Color((1, 2, 3)).as_named(fallback=True) == '#010203'
assert Color((1, 2, 3, 0.1)).as_named(fallback=True) == '#0102031a'
def test_str_repr():
assert str(Color('red')) == 'red'
assert repr(Color('red')) == "Color('red', rgb=(255, 0, 0))"
assert str(Color((1, 2, 3))) == '#010203'
assert repr(Color((1, 2, 3))) == "Color('#010203', rgb=(1, 2, 3))"
(Color('#000'), '"black"'),
(Color((1, 12, 123)), '"#010c7b"'),
(SecretStr('abcd'), '"**********"'),
(SecretStr(''), '""'),
(SecretBytes(b'xyz'), '"**********"'),
(SecretBytes(b''), '""'),
(IPv6Address('::1:0:1'), '"::1:0:1"'),
(IPv4Interface('192.168.0.0/24'), '"192.168.0.0/24"'),
(IPv6Interface('2001:db00::/120'), '"2001:db00::/120"'),
(IPv4Network('192.168.0.0/24'), '"192.168.0.0/24"'),
(IPv6Network('2001:db00::/120'), '"2001:db00::/120"'),
(datetime.datetime(2032, 1, 1, 1, 1), '"2032-01-01T01:01:00"'),
(datetime.datetime(2032, 1, 1, 1, 1, tzinfo=datetime.timezone.utc), '"2032-01-01T01:01:00+00:00"'),
(datetime.datetime(2032, 1, 1), '"2032-01-01T00:00:00"'),
(datetime.time(12, 34, 56), '"12:34:56"'),
(datetime.timedelta(days=12, seconds=34, microseconds=56), '1036834.000056'),
({1, 2, 3}, '[1, 2, 3]'),
def test_as_hsl():
assert Color('bad').as_hsl() == 'hsl(260, 43%, 77%)'
assert Color((1, 2, 3, 0.123456)).as_hsl() == 'hsl(210, 50%, 1%, 0.12)'
assert Color('hsl(260, 43%, 77%)').as_hsl() == 'hsl(260, 43%, 77%)'
from pydantic import BaseModel, ValidationError
from pydantic.color import Color
c = Color('ff00ff')
print(c.as_named())
print(c.as_hex())
c2 = Color('green')
print(c2.as_rgb_tuple())
print(c2.original())
print(repr(Color('hsl(180, 100%, 50%)')))
class Model(BaseModel):
color: Color
print(Model(color='purple'))
try:
Model(color='hello')
except ValidationError as e:
print(e)
from pydantic import BaseModel, ValidationError
from pydantic.color import Color
c = Color('ff00ff')
print(c.as_named())
print(c.as_hex())
c2 = Color('green')
print(c2.as_rgb_tuple())
print(c2.original())
print(repr(Color('hsl(180, 100%, 50%)')))
class Model(BaseModel):
color: Color
print(Model(color='purple'))
try:
Model(color='hello')
except ValidationError as e:
print(e)