aboutsummaryrefslogtreecommitdiff
path: root/tests/test_colors.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_colors.py')
-rwxr-xr-xtests/test_colors.py204
1 files changed, 185 insertions, 19 deletions
diff --git a/tests/test_colors.py b/tests/test_colors.py
index bccc06e..505a43d 100755
--- a/tests/test_colors.py
+++ b/tests/test_colors.py
@@ -6,24 +6,18 @@
""" Unit tests for the thcolor color decoding and management module. """
import pytest
-from thcolor.angles import DegreesAngle
-from thcolor.colors import SRGBColor, HSLColor, HWBColor
+from thcolor.angles import * # NOQA
+from thcolor.colors import * # NOQA
+from thcolor.utils import round_half_up
class TestSRGBColors:
""" Test the sRGB color conversions. """
@pytest.mark.parametrize('args', (
- (-.5, 0, 0),
- (1.2, 0, 0),
- (0, -.5, 0),
- (0, 1.2, 0),
- (0, 0, -.5),
- (0, 0, 1.2),
- (0, 0, 0, -.5),
- (0, 0, 0, 1.2),
(None, 0, 0),
- ('1.2', 0, 0),
+ (0, None, 0),
+ (0, 0, None),
))
def test_invalid_values(self, args):
""" Try to instanciate the class using invalid values. """
@@ -96,10 +90,16 @@ class TestSRGBColors:
assert (hue, round(sat, 2), round(lgt, 2)) == hsl
@pytest.mark.parametrize('args,hsv', (
- # TODO
+ ((0, 0, 0), (DegreesAngle(0), 0, 0)),
+ ((255, 255, 255), (DegreesAngle(0), 0, 1)),
+ ((0, 255, 0), (DegreesAngle(120), 1, 1)),
+ ((0, 120, 0), (DegreesAngle(120), 1, .47)),
+ ((73, 33, 86), (DegreesAngle(285), .62, .34)),
+ ((10, 20, 30), (DegreesAngle(210), .67, .12)),
))
def test_hsv(self, args, hsv):
hue, sat, val, *_ = SRGBColor.frombytes(*args).ashsv()
+ hue = DegreesAngle(int(hue.asdegrees().degrees))
assert (hue, round(sat, 2), round(val, 2)) == hsv
@pytest.mark.parametrize('args,hwb', (
@@ -113,23 +113,56 @@ class TestSRGBColors:
assert (hue, round(wht, 2), round(blk, 2)) == hwb
@pytest.mark.parametrize('args,yiq', (
- # TODO
+ ((0, 0, 0), (0, 0, 0)),
+ ((255, 255, 255), (1, 0, 0)),
+ ((0, 255, 0), (.587, -.275, -.523)),
+ ((0, 120, 0), (.276, -.129, -.246)),
+ ((73, 33, 86), (.2, .0271, .0983)),
+ ((10, 20, 30), (.07098, -.03611, .00389)),
))
def test_yiq(self, args, yiq):
y, i, q, *_ = SRGBColor.frombytes(*args).asyiq()
- assert (round(y, 2), round(i, 2), round(q, 2)) == yiq
+ print((y, i, q), yiq)
+
+ yiq2 = tuple(map(lambda x: round_half_up(x, 3), (y, i, q)))
+ yiq = tuple(map(lambda x: round_half_up(x, 3), yiq))
+ assert yiq == yiq2
- @pytest.mark.parametrize('args,yuv', (
- # TODO
+ @pytest.mark.parametrize('args,cmyk', (
+ ((0, 0, 0), (0, 0, 0, 1)),
+ ((255, 255, 255), (0, 0, 0, 0)),
+ ((0, 255, 0), (1, 0, 1, 0)),
+ ((0, 120, 0), (1, 0, 1, .53)),
+ ((73, 33, 86), (.15, .62, 0, .66)),
+ ((10, 20, 30), (.67, .33, 0, .88)),
))
- def test_yuv(self, args, yuv):
- y, u, v, *_ = SRGBColor.frombytes(*args).asyuv()
- assert (round(y, 2), round(u, 2), round(v, 2)) == yuv
+ def test_cmyk(self, args, cmyk):
+ c, m, y, k, *_ = SRGBColor.frombytes(*args).ascmyk()
+ assert (
+ round_half_up(c, 2),
+ round_half_up(m, 2),
+ round_half_up(y, 2),
+ round_half_up(k, 2),
+ ) == cmyk
class TestHSLColors:
""" Test the HSL color conversions. """
+ @pytest.mark.parametrize('args', (
+ (0, 0, 0),
+ ('hello', 0, 0),
+ (DegreesAngle(0), -.5, 0),
+ (DegreesAngle(0), 1.5, 0),
+ (DegreesAngle(0), 0, -.5),
+ (DegreesAngle(0), 0, 1.5),
+ ))
+ def test_invalid_values(self, args):
+ """ Try to instanciate the class using invalid values. """
+
+ with pytest.raises(ValueError):
+ HSLColor(*args)
+
@pytest.mark.parametrize('args,rgb', (
((DegreesAngle(195), 1, .5), (0, 191, 255)),
((DegreesAngle(240), 1, .25), (0, 0, 128)),
@@ -158,6 +191,20 @@ class TestHSLColors:
assert tuple(HSLColor(*args).css()) == expected
+class TestHSVColor:
+ """ Test the HSV color conversions. """
+
+ @pytest.mark.parametrize('args,rgb', (
+ ((DegreesAngle(120), 0, 0), (0, 0, 0)),
+ ((DegreesAngle(120), .5, .5), (64, 128, 64)),
+ ((DegreesAngle(120), 1, .5), (0, 128, 0)),
+ ((DegreesAngle(120), .5, 1), (128, 255, 128)),
+ ((DegreesAngle(355), .2, .8), (204, 163, 167)),
+ ))
+ def test_srgb(self, args, rgb):
+ assert HSVColor(*args).assrgb().asbytes() == rgb
+
+
class TestHWBColors:
""" Test the HWB color conversions. """
@@ -177,4 +224,123 @@ class TestHWBColors:
def test_css(self, args, expected):
assert tuple(HWBColor(*args).css()) == expected
+
+class TestCMYKColors:
+ """ Test the CMYK color conversions. """
+
+ @pytest.mark.parametrize('args,rgb', (
+ ((0, 0, 0, 0), (255, 255, 255)),
+ ((0, 0, 0, 1), (0, 0, 0)),
+ ((0, 0, 0, .45), (140, 140, 140)),
+ ((0, .25, 0, .45), (140, 105, 140)),
+ ((0, .25, .77, .45), (140, 105, 32)),
+ ((.02, .04, .06, .08), (230, 225, 221)),
+ ))
+ def test_srgb(self, args, rgb):
+ assert CMYKColor(*args).assrgb().asbytes() == rgb
+
+
+class TestLABColors:
+ """ Test the LAB color conversions. """
+
+ @pytest.mark.parametrize('args,lch', (
+ # Examples created using the following online converter:
+ # http://www.easyrgb.com/en/convert.php
+
+ ((0, 0, 0), (0, 0, DegreesAngle(0))),
+ ((.5, -128, 128), (.5, 181.019, DegreesAngle(135))),
+ ))
+ def test_lch(self, args, lch):
+ l, c, h, *_ = LABColor(*args).aslch()
+ assert (
+ round_half_up(l, 3),
+ round_half_up(c, 3),
+ h.asdegrees(),
+ ) == lch
+
+
+class TestLCHColors:
+ """ Test the LCH color conversions. """
+
+ @pytest.mark.parametrize('args,lab', (
+ # Examples created using the following online converter:
+ # http://www.easyrgb.com/en/convert.php
+
+ ((0, 0, DegreesAngle(0)), (0, 0, 0)),
+ ((0, 50, DegreesAngle(0)), (0, 50, 0)),
+ ((.5, 50, DegreesAngle(0)), (.5, 50, 0)),
+ ((.5, 50, DegreesAngle(235)), (.5, -28.679, -40.958)),
+ ((.6, 200, DegreesAngle(146)), (.6, -165.808, 111.839)),
+ ((1, 200, DegreesAngle(0)), (1, 200, 0)),
+ ))
+ def test_lab(self, args, lab):
+ l, a, b, *_ = LCHColor(*args).aslab()
+ l2, a2, b2 = lab
+ assert (
+ round_half_up(l, 3),
+ round_half_up(a, 3),
+ round_half_up(b, 3),
+ ) == (
+ round_half_up(l2, 3),
+ round_half_up(a2, 3),
+ round_half_up(b2, 3),
+ )
+
+
+class TestXYZColors:
+ """ Test the XYZ color conversions. """
+
+ @pytest.mark.parametrize('args', (
+ (-.5, 0, 0),
+ (1.5, 0, 0),
+ (0, -.5, 0),
+ (0, 1.5, 0),
+ (0, 0, -.5),
+ (0, 0, 1.5),
+ (None, 0, 0),
+ (0, None, 0),
+ (0, 0, None),
+ ))
+ def test_invalid_values(self, args):
+ """ Try to instanciate the class using invalid values. """
+
+ with pytest.raises(ValueError):
+ XYZColor(*args)
+
+ @pytest.mark.parametrize('args,rgb', (
+ ((0, 0, 0), (0, 0, 0)),
+ ((1, 1, 1), (277, 249, 244)),
+ ((.1, .2, .3), (-438, 147, 145)),
+ ((1, .5, 0), (378, -103, -153)),
+ ))
+ def test_rgb(self, args, rgb):
+ assert XYZColor(*args).assrgb().asbytes() == rgb
+
+ @pytest.mark.parametrize('args,lab', (
+ ((1, .5, 0), (.76069, 111.688, 131.154)),
+ ((0, .5, 1), (.76069, -327.885, -35.666)),
+ ((.2, .3, .4), (.61654, -37.321, -9.353)),
+ ))
+ def test_lab(self, args, lab):
+ l, a, b, *_ = XYZColor(*args).aslab()
+ l2, a2, b2 = lab
+
+ l, a, b = (
+ round_half_up(l, 3),
+ round_half_up(a, 3),
+ round_half_up(b, 3),
+ )
+ l2, a2, b2 = (
+ round_half_up(l2, 3),
+ round_half_up(a2, 3),
+ round_half_up(b2, 3),
+ )
+
+ assert (l, a, b) == (l2, a2, b2)
+
+
+# TODO: add tests for invalid values in constructors.
+# TODO: test YIQ colors.
+# TODO: test YUV colors.
+
# End of file.