IS31FL3733 Async Driver 1.0.0
Asynchronous DMA-driven IS31FL3733 LED driver for Arduino SAMD
Loading...
Searching...
No Matches
is31fl3733_color_utils.hpp
1/*---------------------------------------------------------------------------------------------
2 * This file incorporates code from the Adafruit_NeoPixel library:
3 * https://github.com/adafruit/Adafruit_NeoPixel
4 *
5 * Portions derived from Adafruit_NeoPixel are Copyright (c) Adafruit Industries.
6 * Licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0).
7 * See: https://www.gnu.org/licenses/lgpl-3.0.html
8 *--------------------------------------------------------------------------------------------*/
9#pragma once
10
11#ifdef ARDUINO
12#include <Arduino.h>
13#else
14#include <cstdint>
15#endif
16
17namespace IS31FL3733 {
18namespace ColorUtils {
19enum class LedColor : uint8_t {
20 Green,
21 Red,
22 Blue,
23};
24
25#ifdef ARDUINO
26#define IS31FL3733_PROGMEM PROGMEM
27#else
28#define IS31FL3733_PROGMEM
29#endif
30
31static const uint8_t IS31FL3733_PROGMEM kGammaTable[256] = {
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
34 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
35 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11,
36 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19,
37 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29, 30, 31,
38 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46,
39 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
40 66, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89,
41 90, 92, 93, 94, 96, 97, 99, 100, 102, 103, 105, 106, 108, 109, 111, 112, 114, 115, 117,
42 119, 120, 122, 124, 125, 127, 129, 130, 132, 134, 136, 137, 139, 141, 143, 145, 146, 148, 150,
43 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188,
44 191, 193, 195, 197, 199, 202, 204, 206, 209, 211, 213, 215, 218, 220, 223, 225, 227, 230, 232,
45 235, 237, 240, 242, 245, 247, 250, 252, 255};
46
47inline uint8_t Gamma8(uint8_t x) {
48#ifdef ARDUINO
49 return pgm_read_byte(&kGammaTable[x]);
50#else
51 return kGammaTable[x];
52#endif
53}
54
55inline uint32_t Gamma32(uint32_t x) {
56 uint8_t *bytes = reinterpret_cast<uint8_t *>(&x);
57 for (uint8_t i = 0; i < 4; i++)
58 bytes[i] = Gamma8(bytes[i]);
59
60 return x;
61}
62
63inline uint32_t PackRGB(uint8_t r, uint8_t g, uint8_t b) {
64 return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
65}
66
67inline uint32_t ColorHSV(uint16_t hue, uint8_t sat = 255, uint8_t val = 255) {
68 uint8_t r;
69 uint8_t g;
70 uint8_t b;
71
72 hue = (uint32_t)(hue * 1530UL + 32768UL) / 65536UL;
73
74 if (hue < 510) {
75 b = 0;
76 if (hue < 255) {
77 r = 255;
78 g = hue;
79 } else {
80 r = 510 - hue;
81 g = 255;
82 }
83 } else if (hue < 1020) {
84 r = 0;
85 if (hue < 765) {
86 g = 255;
87 b = hue - 510;
88 } else {
89 g = 1020 - hue;
90 b = 255;
91 }
92 } else if (hue < 1530) {
93 g = 0;
94 if (hue < 1275) {
95 r = hue - 1020;
96 b = 255;
97 } else {
98 r = 255;
99 b = 1530 - hue;
100 }
101 } else {
102 r = 255;
103 g = 0;
104 b = 0;
105 }
106
107 const uint32_t v1 = 1 + val;
108 const uint16_t s1 = 1 + sat;
109 const uint8_t s2 = 255 - sat;
110
111 return (((((r * s1) >> 8) + s2) * v1) & 0xff00) << 8 |
112 (((((g * s1) >> 8) + s2) * v1) & 0xff00) | (((((b * s1) >> 8) + s2) * v1) >> 8);
113}
114
115#undef IS31FL3733_PROGMEM
116} // namespace ColorUtils
117} // namespace IS31FL3733