aboutsummaryrefslogtreecommitdiff
path: root/tests/test_colors.py
blob: bccc06e98ec2e5f099e1a40423198e7ccfd7777b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
# *****************************************************************************
# Copyright (C) 2019-2021 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
# *****************************************************************************
""" Unit tests for the thcolor color decoding and management module. """

import pytest
from thcolor.angles import DegreesAngle
from thcolor.colors import SRGBColor, HSLColor, HWBColor


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),
    ))
    def test_invalid_values(self, args):
        """ Try to instanciate the class using invalid values. """

        with pytest.raises(ValueError):
            SRGBColor(*args)

    @pytest.mark.parametrize('args,bytes_', (
        ((0, 0, 0), (0, 0, 0)),
        ((.0019, .002, .005), (0, 1, 1)),
        ((.1, .2, .3), (26, 51, 77)),
    ))
    def test_to_bytes(self, args, bytes_):
        """ Try converting to bytes and test the rounding up. """

        assert SRGBColor(*args).asbytes() == bytes_

    @pytest.mark.parametrize('args,bytes_', (
        ((0, .003921, .003925), (0, 1, 1)),
    ))
    def test_from_bytes(self, args, bytes_):
        """ Try converting from bytes and test the rounding up. """

        assert SRGBColor(*args) == SRGBColor.frombytes(*bytes_)

    @pytest.mark.parametrize('name,bytes_', (
        # https://stackoverflow.com/q/8318911
        ('chucknorris', (192, 0, 0)),
        ('ninjaturtle', (0, 160, 0)),
        ('crap', (192, 160, 0)),
        ('grass', (0, 160, 0)),

        # https://bugzilla.mozilla.org/show_bug.cgi?id=121738
        ('navyblue', (160, 176, 224)),

        # http://web.archive.org/web/20050403162633/http://www.jgc.org:80/tsc/index.htm
        # See the "Flex Hex" technique.
        ('FxFxFx', (240, 240, 240)),
        ('#FxFxFx', (240, 240, 240)),
        ('FqFeFm', (240, 254, 240)),

        # http://scrappy-do.blogspot.com/2004/08/little-rant-about-microsoft-internet.html
        ('zqbttv', (0, 176, 0)),
        ('6db6ec49efd278cd0bc92d1e5e072d680', (110, 205, 224)),
    ))
    def test_netscape_decoding(self, name, bytes_):
        """ Try decoding colors using Netscape color parsing. """

        assert SRGBColor.fromnetscapecolorname(name).asbytes() == bytes_

    @pytest.mark.parametrize('args,expected', (
        ((0, 0, 255), ('#0000FF',)),
        ((1, 22, 242, .5), (
            '#0116F2',
            'rgba(1, 22, 242, 50%)',
        )),
    ))
    def test_css(self, args, expected):
        assert tuple(SRGBColor.frombytes(*args).css()) == expected

    @pytest.mark.parametrize('args,hsl', (
        ((0, 0, 0), (DegreesAngle(0), 0, 0)),
        ((1, 2, 3), (DegreesAngle(210), .5, .01)),
        ((50, 100, 150), (DegreesAngle(210), .5, .39)),
        ((255, 255, 255), (DegreesAngle(0), 0, 1)),
        ((82, 122, 122), (DegreesAngle(180), .2, .4)),
    ))
    def test_hsl(self, args, hsl):
        hue, sat, lgt, *_ = SRGBColor.frombytes(*args).ashsl()
        assert (hue, round(sat, 2), round(lgt, 2)) == hsl

    @pytest.mark.parametrize('args,hsv', (
        # TODO
    ))
    def test_hsv(self, args, hsv):
        hue, sat, val, *_ = SRGBColor.frombytes(*args).ashsv()
        assert (hue, round(sat, 2), round(val, 2)) == hsv

    @pytest.mark.parametrize('args,hwb', (
        ((82, 122, 122), (DegreesAngle(180), .32, .52)),
        ((13, 157, 230), (DegreesAngle(200), .05, .1)),
        ((212, 212, 212), (DegreesAngle(0), .83, .17)),
    ))
    def test_hwb(self, args, hwb):
        hue, wht, blk, *_ = SRGBColor.frombytes(*args).ashwb()
        hue = DegreesAngle(int(hue.asdegrees().degrees))
        assert (hue, round(wht, 2), round(blk, 2)) == hwb

    @pytest.mark.parametrize('args,yiq', (
        # TODO
    ))
    def test_yiq(self, args, yiq):
        y, i, q, *_ = SRGBColor.frombytes(*args).asyiq()
        assert (round(y, 2), round(i, 2), round(q, 2)) == yiq

    @pytest.mark.parametrize('args,yuv', (
        # TODO
    ))
    def test_yuv(self, args, yuv):
        y, u, v, *_ = SRGBColor.frombytes(*args).asyuv()
        assert (round(y, 2), round(u, 2), round(v, 2)) == yuv


class TestHSLColors:
    """ Test the HSL color conversions. """

    @pytest.mark.parametrize('args,rgb', (
        ((DegreesAngle(195), 1, .5), (0, 191, 255)),
        ((DegreesAngle(240), 1, .25), (0, 0, 128)),
        ((DegreesAngle(0), 1, .5), (255, 0, 0)),
        ((DegreesAngle(0), 0, 0), (0, 0, 0)),
        ((DegreesAngle(0), 0, 1), (255, 255, 255)),
        ((DegreesAngle(0), 0, .01), (3, 3, 3)),
        ((DegreesAngle(145), .30, .60), (122, 184, 148)),
    ))
    def test_srgb(self, args, rgb):
        r, g, b = HSLColor(*args).assrgb().asbytes()
        assert (r, g, b) == rgb

    @pytest.mark.parametrize('args,expected', (
        ((DegreesAngle(0), 1, .4), (
            '#CC0000',
            'hsl(0deg, 100%, 40%)',
        )),
        ((DegreesAngle(0), .5, 1, .2), (
            '#FFFFFF',
            'rgba(255, 255, 255, 20%)',
            'hsla(0deg, 50%, 100%, 20%)',
        )),
    ))
    def test_css(self, args, expected):
        assert tuple(HSLColor(*args).css()) == expected


class TestHWBColors:
    """ Test the HWB color conversions. """

    @pytest.mark.parametrize('args,rgb', (
        ((DegreesAngle(145), .48, .28), (122, 184, 148)),
    ))
    def test_srgb(self, args, rgb):
        r, g, b = HWBColor(*args).assrgb().asbytes()
        assert (r, g, b) == rgb

    @pytest.mark.parametrize('args,expected', (
        ((DegreesAngle(127), 0, .5), (
            '#00800F',
            'hwb(127deg, 0%, 50%)',
        )),
    ))
    def test_css(self, args, expected):
        assert tuple(HWBColor(*args).css()) == expected

# End of file.