1- # The MIT License (MIT)
1+ # SPDX-FileCopyrightText: 2017 PaintYourDragon for Adafruit Industries
22#
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+
225"""
236`adafruit_fancyled.adafruit_fancyled`
247====================================================
4831class CRGB :
4932 """Color stored in Red, Green, Blue color space.
5033
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.
5639
57- Following statements are equivalent - all return red:
40+ Following statements are equivalent - all return red:
5841
59- .. code-block:: python
42+ .. code-block:: python
6043
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))
6447 """
6548
6649 def __init__ (self , red , green = 0.0 , blue = 0.0 ):
@@ -131,7 +114,7 @@ def __getitem__(self, key):
131114 def pack (self ):
132115 """'Pack' a `CRGB` color into a 24-bit RGB integer.
133116
134- :returns: 24-bit integer a la ``0x00RRGGBB``.
117+ :returns: 24-bit integer a la ``0x00RRGGBB``.
135118 """
136119
137120 return (
@@ -144,21 +127,21 @@ def pack(self):
144127class CHSV :
145128 """Color stored in Hue, Saturation, Value color space.
146129
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.
152135
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.
157140
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+ """
162145
163146 def __init__ (self , h , s = 1.0 , v = 1.0 ):
164147 if isinstance (h , float ):
@@ -197,28 +180,27 @@ def __getitem__(self, key):
197180 def pack (self ):
198181 """'Pack' a `CHSV` color into a 24-bit RGB integer.
199182
200- :returns: 24-bit integer a la ``0x00RRGGBB``.
183+ :returns: 24-bit integer a la ``0x00RRGGBB``.
201184 """
202185
203186 # Convert CHSV to CRGB, return packed result
204187 return CRGB (self ).pack ()
205188
206189
207190def clamp (val , lower , upper ):
208- """Constrain value within a numeric range (inclusive).
209- """
191+ """Constrain value within a numeric range (inclusive)."""
210192 return max (lower , min (val , upper ))
211193
212194
213195def normalize (val , inplace = False ):
214196 """Convert 8-bit (0 to 255) value to normalized (0.0 to 1.0) value.
215197
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).
220202
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).
222204 """
223205
224206 if isinstance (val , int ):
@@ -239,12 +221,12 @@ def normalize(val, inplace=False):
239221def denormalize (val , inplace = False ):
240222 """Convert normalized (0.0 to 1.0) value to 8-bit (0 to 255) value
241223
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).
245227
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).
248230 """
249231
250232 # 'Denormalizing' math varies slightly from normalize(). This is on
@@ -269,9 +251,9 @@ def denormalize(val, inplace=False):
269251def unpack (val ):
270252 """'Unpack' a 24-bit color into a `CRGB` instance.
271253
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
275257 """
276258
277259 # See notes in normalize() for math explanation. Large constants here
@@ -286,10 +268,10 @@ def unpack(val):
286268
287269def mix (color1 , color2 , weight2 = 0.5 ):
288270 """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.
291273
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`.
293275 """
294276
295277 clamp (weight2 , 0.0 , 1.0 )
@@ -337,31 +319,31 @@ def mix(color1, color2, weight2=0.5):
337319
338320def gamma_adjust (val , gamma_value = None , brightness = 1.0 , inplace = False ):
339321 """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.
365347 """
366348 # pylint: disable=too-many-branches
367349
0 commit comments