iron.py
 1  # SPDX-FileCopyrightText: Copyright (c) 2022 JG for Cedar Grove Maker Studios
 2  #
 3  # SPDX-License-Identifier: MIT
 4  """
 5  `cedargrove_rgb_spectrumtools.iron`
 6  ================================================================================
 7  
 8  Temperature Index to Iron Pseudocolor Spectrum RGB Converter Helper
 9  
10  * Author(s): JG
11  
12  Implementation Notes
13  --------------------
14  
15  **Hardware:**
16  
17  **Software and Dependencies:**
18  
19  * Adafruit CircuitPython firmware for the supported boards:
20    https://circuitpython.org/downloads
21  """
22  
23  __version__ = "0.0.0+auto.0"
24  __repo__ = "https://github.com/CedarGroveStudios/CircuitPython_RGB_SpectrumTools.git"
25  
26  
27  def map_range(x, in_min, in_max, out_min, out_max):
28      """
29      Maps and constrains an input value from one range of values to another.
30      (from adafruit_simpleio)
31  
32      :param float x: The value to be mapped. No default.
33      :param float in_min: The beginning of the input range. No default.
34      :param float in_max: The end of the input range. No default.
35      :param float out_min: The beginning of the output range. No default.
36      :param float out_max: The end of the output range. No default.
37  
38      :return: Returns value mapped to new range
39      :rtype: float
40      """
41      in_range = in_max - in_min
42      in_delta = x - in_min
43      if in_range != 0:
44          mapped = in_delta / in_range
45      elif in_delta != 0:
46          mapped = in_delta
47      else:
48          mapped = 0.5
49      mapped *= out_max - out_min
50      mapped += out_min
51      if out_min <= out_max:
52          return max(min(mapped, out_max), out_min)
53      return min(max(mapped, out_max), out_min)
54  
55  
56  def index_to_rgb(index=0, gamma=0.5):
57      """
58      Converts a temperature index to an iron thermographic pseudocolor spectrum
59      RGB value. Temperature index in range of 0.0 to 1.0. Gamma in range of
60      0.0 to 1.0 (1.0=linear), default 0.5 for color TFT displays.
61  
62      :param float index: The normalized index value, range 0 to 1.0. Defaults to 0.
63      :param float gamma: The gamma color perception value. Defaults to 0.5.
64  
65      :return: Returns a 24-bit RGB value
66      :rtype: integer
67      """
68  
69      band = index * 600  # an arbitrary spectrum band index; 0 to 600
70  
71      if band < 70:  # dark gray to blue
72          red = 0.1
73          grn = 0.1
74          blu = (0.2 + (0.8 * map_range(band, 0, 70, 0.0, 1.0))) ** gamma
75      if 70 <= band < 200:  # blue to violet
76          red = map_range(band, 70, 200, 0.0, 0.6) ** gamma
77          grn = 0.0
78          blu = 1.0**gamma
79      if 200 <= band < 300:  # violet to red
80          red = map_range(band, 200, 300, 0.6, 1.0) ** gamma
81          grn = 0.0
82          blu = map_range(band, 200, 300, 1.0, 0.0) ** gamma
83      if 300 <= band < 400:  # red to orange
84          red = 1.0**gamma
85          grn = map_range(band, 300, 400, 0.0, 0.5) ** gamma
86          blu = 0.0
87      if 400 <= band < 500:  # orange to yellow
88          red = 1.0**gamma
89          grn = map_range(band, 400, 500, 0.5, 1.0) ** gamma
90          blu = 0.0
91      if band >= 500:  # yellow to white
92          red = 1.0**gamma
93          grn = 1.0**gamma
94          blu = map_range(band, 500, 580, 0.0, 1.0) ** gamma
95  
96      return (int(red * 255) << 16) + (int(grn * 255) << 8) + int(blu * 255)