color.py
  1  # SPDX-FileCopyrightText: 2019 Kattni Rembor for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  """
  6  `adafruit_led_animation.color`
  7  ================================================================================
  8  
  9  Color variables assigned to RGB values made available for import.
 10  
 11  * Author(s): Kattni Rembor
 12  
 13  Implementation Notes
 14  --------------------
 15  
 16  **Hardware:**
 17  
 18  * `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_
 19  * `Adafruit DotStars <https://www.adafruit.com/category/885>`_
 20  
 21  **Software and Dependencies:**
 22  
 23  * Adafruit CircuitPython firmware for the supported boards:
 24    https://circuitpython.org/downloads
 25  """
 26  RED = (255, 0, 0)
 27  """Red."""
 28  YELLOW = (255, 150, 0)
 29  """Yellow."""
 30  ORANGE = (255, 40, 0)
 31  """Orange."""
 32  GREEN = (0, 255, 0)
 33  """Green."""
 34  TEAL = (0, 255, 120)
 35  """Teal."""
 36  CYAN = (0, 255, 255)
 37  """Cyan."""
 38  BLUE = (0, 0, 255)
 39  """Blue."""
 40  PURPLE = (180, 0, 255)
 41  """Purple."""
 42  MAGENTA = (255, 0, 20)
 43  """Magenta."""
 44  WHITE = (255, 255, 255)
 45  """White."""
 46  BLACK = (0, 0, 0)
 47  """Black or off."""
 48  
 49  GOLD = (255, 222, 30)
 50  """Gold."""
 51  PINK = (242, 90, 255)
 52  """Pink."""
 53  AQUA = (50, 255, 255)
 54  """Aqua."""
 55  JADE = (0, 255, 40)
 56  """Jade."""
 57  AMBER = (255, 100, 0)
 58  """Amber."""
 59  OLD_LACE = (253, 245, 230)  # Warm white.
 60  """Old lace or warm white."""
 61  
 62  RGBW_WHITE_RGB = (255, 255, 255, 0)
 63  """RGBW_WHITE_RGB is for RGBW strips to illuminate only the RGB diodes."""
 64  RGBW_WHITE_W = (0, 0, 0, 255)
 65  """RGBW_WHITE_W is for RGBW strips to illuminate only White diode."""
 66  RGBW_WHITE_RGBW = (255, 255, 255, 255)
 67  """RGBW_WHITE_RGBW is for RGBW strips to illuminate the RGB and White diodes."""
 68  
 69  RAINBOW = (RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE)
 70  """RAINBOW is a list of colors to use for cycling through.
 71  Includes, in order: red, orange, yellow, green, blue, and purple."""
 72  
 73  try:
 74      try:
 75          # Backwards compatibility for 5.3.0 and prior
 76          from _pixelbuf import colorwheel  # pylint: disable=unused-import
 77      except ImportError:
 78          from _pixelbuf import wheel as colorwheel  # pylint: disable=unused-import
 79  except ImportError:
 80  
 81      def colorwheel(pos):
 82          """Colorwheel is built into CircuitPython's _pixelbuf. A separate colorwheel is included
 83          here for use with CircuitPython builds that do not include _pixelbuf, as with some of the
 84          SAMD21 builds. To use: input a value 0 to 255 to get a color value.
 85          The colours are a transition from red to green to blue and back to red."""
 86          if pos < 0 or pos > 255:
 87              return 0, 0, 0
 88          if pos < 85:
 89              return int(255 - pos * 3), int(pos * 3), 0
 90          if pos < 170:
 91              pos -= 85
 92              return 0, int(255 - pos * 3), int(pos * 3)
 93          pos -= 170
 94          return int(pos * 3), 0, int(255 - (pos * 3))
 95  
 96  
 97  def calculate_intensity(color, intensity=1.0):
 98      """
 99      Takes a RGB[W] color tuple and adjusts the intensity.
100      :param float intensity:
101      :param color: color value (tuple, list or int)
102      :return: color
103      """
104      # Note: This code intentionally avoids list comprehensions and intermediate variables
105      # for an approximately 2x performance gain.
106      if isinstance(color, int):
107          return (
108              (int((color & 0xFF0000) * intensity) & 0xFF0000)
109              | (int((color & 0xFF00) * intensity) & 0xFF00)
110              | (int((color & 0xFF) * intensity) & 0xFF)
111          )
112  
113      if len(color) == 3:
114          return (
115              int(color[0] * intensity),
116              int(color[1] * intensity),
117              int(color[2] * intensity),
118          )
119      if len(color) == 4 and isinstance(color[3], float):
120          return (
121              int(color[0] * intensity),
122              int(color[1] * intensity),
123              int(color[2] * intensity),
124              color[3],
125          )
126      return (
127          int(color[0] * intensity),
128          int(color[1] * intensity),
129          int(color[2] * intensity),
130          int(color[3] * intensity),
131      )