1
- # The MIT License (MIT)
1
+ # SPDX-FileCopyrightText: 2017 PaintYourDragon for Adafruit Industries
2
2
#
3
- # Copyright (c) 2017 PaintYourDragon for Adafruit Industries
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # SPDX-License-Identifier: MIT
4
+
22
5
"""
23
6
`adafruit_fancyled.adafruit_fancyled`
24
7
====================================================
48
31
class CRGB :
49
32
"""Color stored in Red, Green, Blue color space.
50
33
51
- One of two ways: separate red, gren, blue values (either as integers
52
- (0 to 255 range) or floats (0.0 to 1.0 range), either type is
53
- 'clamped' to valid range and stored internally in the normalized
54
- (float) format), OR can accept a CHSV color as input, which will be
55
- converted and stored in RGB format.
34
+ One of two ways: separate red, gren, blue values (either as integers
35
+ (0 to 255 range) or floats (0.0 to 1.0 range), either type is
36
+ 'clamped' to valid range and stored internally in the normalized
37
+ (float) format), OR can accept a CHSV color as input, which will be
38
+ converted and stored in RGB format.
56
39
57
- Following statements are equivalent - all return red:
40
+ Following statements are equivalent - all return red:
58
41
59
- .. code-block:: python
42
+ .. code-block:: python
60
43
61
- c = CRGB(255, 0, 0)
62
- c = CRGB(1.0, 0.0, 0.0)
63
- c = CRGB(CHSV(0.0, 1.0, 1.0))
44
+ c = CRGB(255, 0, 0)
45
+ c = CRGB(1.0, 0.0, 0.0)
46
+ c = CRGB(CHSV(0.0, 1.0, 1.0))
64
47
"""
65
48
66
49
def __init__ (self , red , green = 0.0 , blue = 0.0 ):
@@ -131,7 +114,7 @@ def __getitem__(self, key):
131
114
def pack (self ):
132
115
"""'Pack' a `CRGB` color into a 24-bit RGB integer.
133
116
134
- :returns: 24-bit integer a la ``0x00RRGGBB``.
117
+ :returns: 24-bit integer a la ``0x00RRGGBB``.
135
118
"""
136
119
137
120
return (
@@ -144,21 +127,21 @@ def pack(self):
144
127
class CHSV :
145
128
"""Color stored in Hue, Saturation, Value color space.
146
129
147
- Accepts hue as float (any range) or integer (0-256 -> 0.0-1.0) with
148
- no clamping performed (hue can 'wrap around'), saturation and value
149
- as float (0.0 to 1.0) or integer (0 to 255), both are clamped and
150
- stored internally in the normalized (float) format. Latter two are
151
- optional, can pass juse hue and saturation/value will default to 1.0.
130
+ Accepts hue as float (any range) or integer (0-256 -> 0.0-1.0) with
131
+ no clamping performed (hue can 'wrap around'), saturation and value
132
+ as float (0.0 to 1.0) or integer (0 to 255), both are clamped and
133
+ stored internally in the normalized (float) format. Latter two are
134
+ optional, can pass juse hue and saturation/value will default to 1.0.
152
135
153
- Unlike `CRGB` (which can take a `CHSV` as input), there's currently
154
- no equivalent RGB-to-HSV conversion, mostly because it's a bit like
155
- trying to reverse a hash...there may be multiple HSV solutions for a
156
- given RGB input.
136
+ Unlike `CRGB` (which can take a `CHSV` as input), there's currently
137
+ no equivalent RGB-to-HSV conversion, mostly because it's a bit like
138
+ trying to reverse a hash...there may be multiple HSV solutions for a
139
+ given RGB input.
157
140
158
- This might be OK as long as conversion precedence is documented,
159
- but otherwise (and maybe still) could cause confusion as certain
160
- HSV->RGB->HSV translations won't have the same input and output.
161
- """
141
+ This might be OK as long as conversion precedence is documented,
142
+ but otherwise (and maybe still) could cause confusion as certain
143
+ HSV->RGB->HSV translations won't have the same input and output.
144
+ """
162
145
163
146
def __init__ (self , h , s = 1.0 , v = 1.0 ):
164
147
if isinstance (h , float ):
@@ -197,28 +180,27 @@ def __getitem__(self, key):
197
180
def pack (self ):
198
181
"""'Pack' a `CHSV` color into a 24-bit RGB integer.
199
182
200
- :returns: 24-bit integer a la ``0x00RRGGBB``.
183
+ :returns: 24-bit integer a la ``0x00RRGGBB``.
201
184
"""
202
185
203
186
# Convert CHSV to CRGB, return packed result
204
187
return CRGB (self ).pack ()
205
188
206
189
207
190
def clamp (val , lower , upper ):
208
- """Constrain value within a numeric range (inclusive).
209
- """
191
+ """Constrain value within a numeric range (inclusive)."""
210
192
return max (lower , min (val , upper ))
211
193
212
194
213
195
def normalize (val , inplace = False ):
214
196
"""Convert 8-bit (0 to 255) value to normalized (0.0 to 1.0) value.
215
197
216
- Accepts integer, 0 to 255 range (input is clamped) or a list or tuple
217
- of integers. In list case, 'inplace' can be used to control whether
218
- the original list is modified (True) or a new list is generated and
219
- returned (False).
198
+ Accepts integer, 0 to 255 range (input is clamped) or a list or tuple
199
+ of integers. In list case, 'inplace' can be used to control whether
200
+ the original list is modified (True) or a new list is generated and
201
+ returned (False).
220
202
221
- Returns float, 0.0 to 1.0 range, or list of floats (or None if inplace).
203
+ Returns float, 0.0 to 1.0 range, or list of floats (or None if inplace).
222
204
"""
223
205
224
206
if isinstance (val , int ):
@@ -239,12 +221,12 @@ def normalize(val, inplace=False):
239
221
def denormalize (val , inplace = False ):
240
222
"""Convert normalized (0.0 to 1.0) value to 8-bit (0 to 255) value
241
223
242
- Accepts float, 0.0 to 1.0 range or a list or tuple of floats. In
243
- list case, 'inplace' can be used to control whether the original list
244
- is modified (True) or a new list is generated and returned (False).
224
+ Accepts float, 0.0 to 1.0 range or a list or tuple of floats. In
225
+ list case, 'inplace' can be used to control whether the original list
226
+ is modified (True) or a new list is generated and returned (False).
245
227
246
- Returns integer, 0 to 255 range, or list of integers (or None if
247
- inplace).
228
+ Returns integer, 0 to 255 range, or list of integers (or None if
229
+ inplace).
248
230
"""
249
231
250
232
# 'Denormalizing' math varies slightly from normalize(). This is on
@@ -269,9 +251,9 @@ def denormalize(val, inplace=False):
269
251
def unpack (val ):
270
252
"""'Unpack' a 24-bit color into a `CRGB` instance.
271
253
272
- :param int val: 24-bit integer a la ``0x00RRGGBB``.
273
- :returns: CRGB color.
274
- :rtype: CRGB
254
+ :param int val: 24-bit integer a la ``0x00RRGGBB``.
255
+ :returns: CRGB color.
256
+ :rtype: CRGB
275
257
"""
276
258
277
259
# See notes in normalize() for math explanation. Large constants here
@@ -286,10 +268,10 @@ def unpack(val):
286
268
287
269
def mix (color1 , color2 , weight2 = 0.5 ):
288
270
"""Blend between two colors using given ratio. Accepts two colors (each
289
- may be `CRGB`, `CHSV` or packed integer), and weighting (0.0 to 1.0)
290
- of second color.
271
+ may be `CRGB`, `CHSV` or packed integer), and weighting (0.0 to 1.0)
272
+ of second color.
291
273
292
- :returns: `CRGB` color in most cases, `CHSV` if both inputs are `CHSV`.
274
+ :returns: `CRGB` color in most cases, `CHSV` if both inputs are `CHSV`.
293
275
"""
294
276
295
277
clamp (weight2 , 0.0 , 1.0 )
@@ -337,31 +319,31 @@ def mix(color1, color2, weight2=0.5):
337
319
338
320
def gamma_adjust (val , gamma_value = None , brightness = 1.0 , inplace = False ):
339
321
"""Provides gamma adjustment for single values, `CRGB` and `CHSV` types
340
- and lists of any of these.
341
-
342
- Works in one of three ways:
343
- 1. Accepts a single normalized level (0.0 to 1.0) and optional
344
- gamma-adjustment factor (float usu. > 1.0, default if
345
- unspecified is GFACTOR) and brightness (float 0.0 to 1.0,
346
- default is 1.0). Returns a single normalized gamma-corrected
347
- brightness level (0.0 to 1.0).
348
- 2. Accepts a single `CRGB` or `CHSV` type, optional single gamma
349
- factor OR a (R,G,B) gamma tuple (3 values usu. > 1.0), optional
350
- single brightness factor OR a (R,G,B) brightness tuple. The
351
- input tuples are RGB even when a `CHSV` color is passed. Returns
352
- a normalized gamma-corrected `CRGB` type (NOT `CHSV`!).
353
- 3. Accept a list or tuple of normalized levels, `CRGB` or `CHSV`
354
- types (and optional gamma and brightness levels or tuples
355
- applied to all). Returns a list of gamma-corrected values or
356
- `CRGB` types (NOT `CHSV`!).
357
-
358
- In cases 2 and 3, if the input is a list (NOT a tuple!), the 'inplace'
359
- flag determines whether a new tuple/list is calculated and returned,
360
- or the existing value is modified in-place. By default this is
361
- 'False'. If you try to inplace-modify a tuple, an exception is raised.
362
-
363
- In cases 2 and 3, there is NO return value if 'inplace' is True --
364
- the original values are modified.
322
+ and lists of any of these.
323
+
324
+ Works in one of three ways:
325
+ 1. Accepts a single normalized level (0.0 to 1.0) and optional
326
+ gamma-adjustment factor (float usu. > 1.0, default if
327
+ unspecified is GFACTOR) and brightness (float 0.0 to 1.0,
328
+ default is 1.0). Returns a single normalized gamma-corrected
329
+ brightness level (0.0 to 1.0).
330
+ 2. Accepts a single `CRGB` or `CHSV` type, optional single gamma
331
+ factor OR a (R,G,B) gamma tuple (3 values usu. > 1.0), optional
332
+ single brightness factor OR a (R,G,B) brightness tuple. The
333
+ input tuples are RGB even when a `CHSV` color is passed. Returns
334
+ a normalized gamma-corrected `CRGB` type (NOT `CHSV`!).
335
+ 3. Accept a list or tuple of normalized levels, `CRGB` or `CHSV`
336
+ types (and optional gamma and brightness levels or tuples
337
+ applied to all). Returns a list of gamma-corrected values or
338
+ `CRGB` types (NOT `CHSV`!).
339
+
340
+ In cases 2 and 3, if the input is a list (NOT a tuple!), the 'inplace'
341
+ flag determines whether a new tuple/list is calculated and returned,
342
+ or the existing value is modified in-place. By default this is
343
+ 'False'. If you try to inplace-modify a tuple, an exception is raised.
344
+
345
+ In cases 2 and 3, there is NO return value if 'inplace' is True --
346
+ the original values are modified.
365
347
"""
366
348
# pylint: disable=too-many-branches
367
349
0 commit comments