ESPectro32 Library
Library for using ESPectro32 board
 All Classes Functions Variables Enumerations Enumerator Pages
RgbLedColor.h
1 /*
2  * RgbLedColor.h
3  *
4  * Created on: Aug 8, 2017
5  * Author: andri
6  */
7 
8 #ifndef COMPONENTS_WS281X_SRC_RGBLEDCOLOR_H_
9 #define COMPONENTS_WS281X_SRC_RGBLEDCOLOR_H_
10 
11 #include <stdint.h>
12 #include <math.h>
13 
17 //typedef struct {
18 // uint8_t red;
19 // uint8_t green;
20 // uint8_t blue;
21 //} pixel_t;
22 
23 #define min(a,b) ((a)<(b)?(a):(b))
24 
26 {
27  union
28  {
29  struct
30  {
31  uint8_t red;
32  uint8_t green;
33  uint8_t blue;
34  };
35  uint8_t raw[3];
36  };
37 
38  inline RgbLedColor_t() __attribute__((always_inline))
39  {
40  }
41 
42  // allow construction from R, G, B
43  inline RgbLedColor_t( uint8_t ir, uint8_t ig, uint8_t ib) __attribute__((always_inline))
44  : red(ir), green(ig), blue(ib)
45  {
46  }
47 
48  // allow construction from 32-bit (really 24-bit) bit 0xRRGGBB color code
49  inline RgbLedColor_t( uint32_t colorcode) __attribute__((always_inline))
50  : red((colorcode >> 16) & 0xFF), green((colorcode >> 8) & 0xFF), blue((colorcode >> 0) & 0xFF)
51  {
52  }
53 
54  void setColor(uint32_t colorcode) {
55  red = ((colorcode >> 16) & 0xFF);
56  green = ((colorcode >> 8) & 0xFF);
57  blue = ((colorcode >> 0) & 0xFF);
58  }
59 
60  bool operator == (const RgbLedColor_t &c) const
61  {
62  return(this->red == c.red and this->green == c.green and this->blue == c.blue);
63  }
64 /*
65  ESPectro_LED_Color getPixel(AlaColor maxOut)
66  {
67  return ESPectro_LED_Color(r*maxOut.r/255, g*maxOut.g/255, b*maxOut.b/255);
68  }
69 */
70  RgbLedColor_t sum(RgbLedColor_t color)
71  {
72  int r0 = min(color.red + red, 255);
73  int g0 = min(color.green + green, 255);
74  int b0 = min(color.blue + blue, 255);
75  return RgbLedColor_t(r0, g0, b0);
76  }
77 
78  RgbLedColor_t interpolate(RgbLedColor_t color, float x)
79  {
80  int r0 = x*(color.red - red) + red;
81  int g0 = x*(color.green - green) + green;
82  int b0 = x*(color.blue - blue) + blue;
83  return RgbLedColor_t(r0, g0, b0);
84  }
85 
86  RgbLedColor_t scale(float k)
87  {
88  int r0 = min(red*k, 255);
89  int g0 = min(green*k, 255);
90  int b0 = min(blue*k, 255);
91  return RgbLedColor_t(r0, g0, b0);
92  }
93 };
94 
95 
97 {
98  int numColors;
99  RgbLedColor_t *colors;
100 
106  {
107  int i0 = (int)i%(numColors);
108  int i1 = (int)(i+1)%(numColors);
109 
110  // decimal part is used to interpolate between the two colors
111  float t0 = i - trunc(i);
112  //float t0 = i - (int)i;
113 
114  return colors[i0].interpolate(colors[i1], t0);
115  }
116 
117  bool operator == (const RgbLedPalette_t &c) const
118  {
119  if (!(this->numColors == c.numColors))
120  return false;
121 
122  for(int i=0; i<numColors; i++)
123  {
124  if (!(this->colors[i] == c.colors[i]))
125  return false;
126  }
127  return true;
128  }
129 };
130 
131 
132 #endif /* COMPONENTS_WS281X_SRC_RGBLEDCOLOR_H_ */
RgbLedColor_t getPaletteColor(float i)
Definition: RgbLedColor.h:105
Definition: RgbLedColor.h:96
Definition: RgbLedColor.h:25