Coverage for /usr/lib/python3/dist-packages/colorzero/types.py: 51%
132 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-02-10 12:38 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-02-10 12:38 +0000
1# vim: set et sw=4 sts=4 fileencoding=utf-8:
2#
3# The colorzero color library
4# Copyright (c) 2016-2018 Dave Jones <dave@waveform.org.uk>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# * Neither the name of the copyright holder nor the
15# names of its contributors may be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
30"Define the tuples used to represent various color systems."
32from __future__ import (
33 unicode_literals,
34 print_function,
35 division,
36 absolute_import,
37)
39from collections import namedtuple, OrderedDict
41from .attr import Red, Green, Blue, Hue, Lightness, Saturation, Luma
44class RGB(tuple):
45 "Named tuple representing red, green, and blue."
46 # NOTE: This source mostly generated by namedtuple(..., verbose=True)
48 __slots__ = ()
50 _fields = ('r', 'g', 'b')
52 def __new__(cls, r, g, b):
53 return tuple.__new__(cls, (r, g, b))
55 def _replace(self, **kw):
56 "Return a new RGB object replacing specified fields with new values"
57 # pylint: disable=bad-builtin
58 result = tuple.__new__(RGB, map(kw.pop, 'rgb', self))
59 if kw:
60 raise ValueError('Got unexpected field names: %r' % list(kw))
61 return result
63 def __repr__(self):
64 "Return a nicely formatted representation string"
65 return self.__class__.__name__ + '(r=%r, g=%r, b=%r)' % self
67 def _asdict(self):
68 "Return a new OrderedDict which maps field names to their values."
69 return OrderedDict(zip(self._fields, self))
71 def __getnewargs__(self):
72 "Return self as a plain tuple. Used by copy and pickle."
73 return tuple(self)
75 @property
76 def r(self):
77 "Return the red value as a :class:`Red` instance"
78 # pylint: disable=invalid-name
79 return Red(self[0])
81 @property
82 def red(self):
83 "Return the red value as a :class:`Red` instance"
84 return Red(self[0])
86 @property
87 def g(self):
88 "Return the green value as a :class:`Green` instance"
89 # pylint: disable=invalid-name
90 return Green(self[1])
92 @property
93 def green(self):
94 "Return the green value as a :class:`Green` instance"
95 return Green(self[1])
97 @property
98 def b(self):
99 "Return the blue value as a :class:`Blue` instance"
100 # pylint: disable=invalid-name
101 return Blue(self[2])
103 @property
104 def blue(self):
105 "Return the blue value as a :class:`Blue` instance"
106 return Blue(self[2])
109class HLS(tuple):
110 "Named tuple representing hue, lightness, and saturation."
111 # NOTE: This source mostly generated by namedtuple(..., verbose=True)
113 __slots__ = ()
115 _fields = ('h', 'l', 's')
117 def __new__(cls, h, l, s):
118 return tuple.__new__(cls, (h, l, s))
120 def _replace(self, **kw):
121 "Return a new HLS object replacing specified fields with new values"
122 # pylint: disable=bad-builtin
123 result = tuple.__new__(HLS, map(kw.pop, 'hls', self))
124 if kw:
125 raise ValueError('Got unexpected field names: %r' % list(kw))
126 return result
128 def __repr__(self):
129 "Return a nicely formatted representation string"
130 return self.__class__.__name__ + '(h=%r, l=%r, s=%r)' % self
132 def _asdict(self):
133 "Return a new OrderedDict which maps field names to their values."
134 return OrderedDict(zip(self._fields, self))
136 def __getnewargs__(self):
137 "Return self as a plain tuple. Used by copy and pickle."
138 return tuple(self)
140 @property
141 def h(self):
142 "Return the hue value as a :class:`Hue` instance"
143 # pylint: disable=invalid-name
144 return Hue(self[0])
146 @property
147 def hue(self):
148 "Return the hue value as a :class:`Hue` instance"
149 return Hue(self[0])
151 @property
152 def l(self):
153 "Return the lightness value as a :class:`Lightness` instance"
154 # pylint: disable=invalid-name
155 return Lightness(self[1])
157 @property
158 def lightness(self):
159 "Return the lightness value as a :class:`Lightness` instance"
160 return Lightness(self[1])
162 @property
163 def s(self):
164 "Return the saturation value as a :class:`Saturation` instance"
165 # pylint: disable=invalid-name
166 return Saturation(self[2])
168 @property
169 def saturation(self):
170 "Return the saturation value as a :class:`Saturation` instance"
171 return Saturation(self[2])
174class HSV(tuple):
175 'Named tuple representing hue, saturation, and value ("brightness").'
177 __slots__ = ()
179 _fields = ('h', 's', 'v')
181 def __new__(cls, h, s, v):
182 return tuple.__new__(cls, (h, s, v))
184 def _replace(self, **kw):
185 "Return a new HSV object replacing specified fields with new values"
186 # pylint: disable=bad-builtin
187 result = tuple.__new__(HSV, map(kw.pop, 'hsv', self))
188 if kw:
189 raise ValueError('Got unexpected field names: %r' % list(kw))
190 return result
192 def __repr__(self):
193 "Return a nicely formatted representation string"
194 return self.__class__.__name__ + '(h=%r, s=%r, v=%r)' % self
196 def _asdict(self):
197 "Return a new OrderedDict which maps field names to their values."
198 return OrderedDict(zip(self._fields, self))
200 def __getnewargs__(self):
201 "Return self as a plain tuple. Used by copy and pickle."
202 return tuple(self)
204 @property
205 def h(self):
206 "Return the hue value as a :class:`Hue` instance"
207 # pylint: disable=invalid-name
208 return Hue(self[0])
210 @property
211 def hue(self):
212 "Return the hue value as a :class:`Hue` instance"
213 return Hue(self[0])
215 @property
216 def s(self):
217 "Return the saturation value as a :class:`Saturation` instance"
218 # pylint: disable=invalid-name
219 return Saturation(self[1])
221 @property
222 def saturation(self):
223 "Return the saturation value as a :class:`Saturation` instance"
224 return Saturation(self[1])
226 @property
227 def v(self):
228 "Return the brightness value"
229 return self[2]
231 @property
232 def value(self):
233 "Return the brightness value"
234 return self[2]
237class YUV(tuple):
238 "Named tuple representing luma and two chroma offsets"
240 __slots__ = ()
242 _fields = ('y', 'u', 'v')
244 def __new__(cls, y, u, v):
245 return tuple.__new__(cls, (y, u, v))
247 def _replace(self, **kw):
248 "Return a new YUV object replacing specified fields with new values"
249 # pylint: disable=bad-builtin
250 result = tuple.__new__(YUV, map(kw.pop, 'yuv', self))
251 if kw:
252 raise ValueError('Got unexpected field names: %r' % list(kw))
253 return result
255 def __repr__(self):
256 "Return a nicely formatted representation string"
257 return self.__class__.__name__ + '(y=%r, u=%r, v=%r)' % self
259 def _asdict(self):
260 "Return a new OrderedDict which maps field names to their values."
261 return OrderedDict(zip(self._fields, self))
263 def __getnewargs__(self):
264 "Return self as a plain tuple. Used by copy and pickle."
265 return tuple(self)
267 @property
268 def y(self):
269 "Return the luma value as a :class:`Luma` instance"
270 # pylint: disable=invalid-name
271 return Luma(self[0])
273 @property
274 def luma(self):
275 "Return the luma value as a :class:`Luma` instance"
276 return Luma(self[0])
278 @property
279 def u(self):
280 "Return the first chroma offset"
281 # pylint: disable=invalid-name
282 return self[1]
284 @property
285 def v(self):
286 "Return the second chroma offset"
287 # pylint: disable=invalid-name
288 return self[2]
291class CMY(namedtuple('CMY', ('c', 'm', 'y'))):
292 "Named tuple representing cyan, magenta, and yellow."
294 @property
295 def cyan(self):
296 # pylint: disable=missing-docstring
297 return self.c
299 @property
300 def magenta(self):
301 # pylint: disable=missing-docstring
302 return self.m
304 @property
305 def yellow(self):
306 # pylint: disable=missing-docstring
307 return self.y
310class CMYK(namedtuple('CMYK', ('c', 'm', 'y', 'k'))):
311 "Named tuple representing cyan, magenta, yellow, and black."
313 @property
314 def cyan(self):
315 # pylint: disable=missing-docstring
316 return self.c
318 @property
319 def magenta(self):
320 # pylint: disable=missing-docstring
321 return self.m
323 @property
324 def yellow(self):
325 # pylint: disable=missing-docstring
326 return self.y
328 @property
329 def black(self):
330 # pylint: disable=missing-docstring
331 return self.k
334YIQ = namedtuple('YIQ', ('y', 'i', 'q'))
335XYZ = namedtuple('XYZ', ('x', 'y', 'z'))
336Luv = namedtuple('Luv', ('l', 'u', 'v'))
337Lab = namedtuple('Lab', ('l', 'a', 'b'))