TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Sat Dec 17 00:23:44 2016 +0000
Revision:
45:1ec0f097fa0d
Parent:
37:598d7a15192a
Child:
46:2374900f8845
WS2811/WS2812 Driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mutech 0:dff187a80020 1 /* ColorLib.h
mutech 0:dff187a80020 2 * mbed Microcontroller Library
mutech 0:dff187a80020 3 * Copyright (c) 2016 muetch, t.kuroki, MIT License
mutech 0:dff187a80020 4 *
mutech 0:dff187a80020 5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mutech 0:dff187a80020 6 * and associated documentation files (the "Software"), to deal in the Software without restriction,
mutech 0:dff187a80020 7 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
mutech 0:dff187a80020 8 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
mutech 0:dff187a80020 9 * furnished to do so, subject to the following conditions:
mutech 0:dff187a80020 10 *
mutech 0:dff187a80020 11 * The above copyright notice and this permission notice shall be included in all copies or
mutech 0:dff187a80020 12 * substantial portions of the Software.
mutech 0:dff187a80020 13 *
mutech 0:dff187a80020 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mutech 0:dff187a80020 15 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mutech 0:dff187a80020 16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mutech 0:dff187a80020 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mutech 0:dff187a80020 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mutech 0:dff187a80020 19 */
mutech 0:dff187a80020 20
mutech 0:dff187a80020 21 #pragma once
mutech 0:dff187a80020 22
mutech 0:dff187a80020 23 #ifndef COLORLIB_H
mutech 0:dff187a80020 24 #define COLORLIB_H
mutech 0:dff187a80020 25
mutech 0:dff187a80020 26 #include <stdint.h>
mutech 0:dff187a80020 27
mutech 0:dff187a80020 28 //----------------------------------------------------------------------------
mutech 29:a362df191524 29 #define RGB_MAX_VAL 255
mutech 29:a362df191524 30 #define HSV_MAX_HUE 3600
mutech 29:a362df191524 31 #define HSV_MAX_SAT 255
mutech 29:a362df191524 32 #define HSV_MAX_VAL 255
mutech 1:0288c920499c 33
mutech 1:0288c920499c 34 #define GetRValue(rgb) ((uint8_t)(rgb))
mutech 1:0288c920499c 35 #define GetGValue(rgb) ((uint8_t)((rgb) >> 8))
mutech 1:0288c920499c 36 #define GetBValue(rgb) ((uint8_t)((rgb) >> 16))
mutech 1:0288c920499c 37 #define COLORREF(r, g ,b) ((uint32_t)((uint8_t)(r) | ((uint8_t)(g) << 8) | ((uint8_t)(b) << 16)))
mutech 1:0288c920499c 38
mutech 6:5aff0da4b663 39 enum SystemColor
mutech 6:5aff0da4b663 40 {
mutech 6:5aff0da4b663 41 CL_BLACK = COLORREF(0x00, 0x00, 0x00),
mutech 6:5aff0da4b663 42 CL_RED = COLORREF(0xFF, 0x00, 0x00),
mutech 6:5aff0da4b663 43 CL_LIME = COLORREF(0x00, 0xFF, 0x00),
mutech 6:5aff0da4b663 44 CL_YELLOW = COLORREF(0xFF, 0xFF, 0x00),
mutech 6:5aff0da4b663 45 CL_BLUE = COLORREF(0x00, 0x00, 0xFF),
mutech 6:5aff0da4b663 46 CL_FUCHSIA = COLORREF(0xFF, 0x00, 0xFF),
mutech 6:5aff0da4b663 47 CL_MAGENTA = COLORREF(0xFF, 0x00, 0xFF),
mutech 6:5aff0da4b663 48 CL_AQUA = COLORREF(0x00, 0xFF, 0xFF),
mutech 6:5aff0da4b663 49 CL_CYAN = COLORREF(0x00, 0xFF, 0xFF),
mutech 6:5aff0da4b663 50 CL_WHITE = COLORREF(0xFF, 0xFF, 0xFF),
mutech 6:5aff0da4b663 51
mutech 6:5aff0da4b663 52 CL_MAROON = COLORREF(0x80, 0x00, 0x00),
mutech 6:5aff0da4b663 53 CL_GREEN = COLORREF(0x00, 0x80, 0x00),
mutech 6:5aff0da4b663 54 CL_OLIVE = COLORREF(0x80, 0x80, 0x00),
mutech 6:5aff0da4b663 55 CL_NAVY = COLORREF(0x00, 0x00, 0x80),
mutech 6:5aff0da4b663 56 CL_PURPLE = COLORREF(0x80, 0x00, 0x80),
mutech 6:5aff0da4b663 57 CL_TEAL = COLORREF(0x00, 0x80, 0x80),
mutech 6:5aff0da4b663 58 CL_GRAY = COLORREF(0x80, 0x80, 0x80),
mutech 6:5aff0da4b663 59
mutech 6:5aff0da4b663 60 CL_GOLD = COLORREF(0xFF, 0xD7, 0x00),
mutech 6:5aff0da4b663 61 CL_SILVER = COLORREF(0xC0, 0xC0, 0xC0),
mutech 6:5aff0da4b663 62
mutech 6:5aff0da4b663 63 CL_IVORY = COLORREF(0xFF, 0xFF, 0xF0),
mutech 6:5aff0da4b663 64 CL_LIGHTBLUE = COLORREF(0xAD, 0xD8, 0xE6),
mutech 6:5aff0da4b663 65 CL_LIGHTCYAN = COLORREF(0xE0, 0xFF, 0xFF),
mutech 6:5aff0da4b663 66 CL_LIGHTGREEN = COLORREF(0x90, 0xEE, 0x90),
mutech 6:5aff0da4b663 67 CL_LIGHTGRAY = COLORREF(0xD3, 0xD3, 0xD3),
mutech 6:5aff0da4b663 68 CL_LIGHTPINK = COLORREF(0xFF, 0xB6, 0xC1),
mutech 6:5aff0da4b663 69 CL_LIGHTYELLOW = COLORREF(0xFF, 0xFF, 0xE0),
mutech 6:5aff0da4b663 70 CL_PINK = COLORREF(0xFF, 0xC0, 0xCB),
mutech 6:5aff0da4b663 71 CL_SKYBLUE = COLORREF(0x87, 0xCE, 0xEB),
mutech 6:5aff0da4b663 72 CL_VIOLET = COLORREF(0xEE, 0x82, 0xEE),
mutech 6:5aff0da4b663 73 CL_YELLOWGREEN = COLORREF(0x9A, 0xCD, 0x32),
mutech 6:5aff0da4b663 74 CL_GREENYELLOW = COLORREF(0xAD, 0xFF, 0x2F),
mutech 6:5aff0da4b663 75 CL_CANDLE = COLORREF(255, 147, 41), /* 1900 K */
mutech 15:cd78625d83b6 76 CL_TUNGSTEN = COLORREF(255, 197, 143), /* 2600 K */
mutech 13:2dce64fab69e 77 CL_HALOGEN = COLORREF(255, 241, 224), /* 3200 K */
mutech 6:5aff0da4b663 78 };
mutech 6:5aff0da4b663 79
mutech 6:5aff0da4b663 80 //----------------------------------------------------------------------------
mutech 1:0288c920499c 81 struct RGBColor;
mutech 1:0288c920499c 82 struct HSVColor;
mutech 1:0288c920499c 83
mutech 1:0288c920499c 84 void rgb2hsv(RGBColor const& rgb, HSVColor& hsv);
mutech 1:0288c920499c 85 void hsv2rgb(HSVColor const& hsv, RGBColor& rgb);
mutech 0:dff187a80020 86
mutech 0:dff187a80020 87 //----------------------------------------------------------------------------
mutech 0:dff187a80020 88 /**
mutech 0:dff187a80020 89 RGB Color
mutech 0:dff187a80020 90 */
mutech 0:dff187a80020 91 struct RGBColor
mutech 0:dff187a80020 92 {
mutech 1:0288c920499c 93 union
mutech 1:0288c920499c 94 {
mutech 1:0288c920499c 95 struct
mutech 1:0288c920499c 96 {
mutech 1:0288c920499c 97 union {
mutech 1:0288c920499c 98 uint8_t r;
mutech 1:0288c920499c 99 uint8_t red;
mutech 1:0288c920499c 100 };
mutech 1:0288c920499c 101 union {
mutech 1:0288c920499c 102 uint8_t g;
mutech 1:0288c920499c 103 uint8_t green;
mutech 1:0288c920499c 104 };
mutech 1:0288c920499c 105 union {
mutech 1:0288c920499c 106 uint8_t b;
mutech 1:0288c920499c 107 uint8_t blue;
mutech 1:0288c920499c 108 };
mutech 1:0288c920499c 109 };
mutech 1:0288c920499c 110 uint8_t raw[3];
mutech 1:0288c920499c 111 };
mutech 0:dff187a80020 112
mutech 0:dff187a80020 113 /**
mutech 0:dff187a80020 114 Constructor with rgb initializing
mutech 1:0288c920499c 115
mutech 0:dff187a80020 116 @param r - the red byte
mutech 0:dff187a80020 117 @param g - the green byte
mutech 0:dff187a80020 118 @param b - the blue byte
mutech 0:dff187a80020 119 */
mutech 0:dff187a80020 120 RGBColor(uint8_t r, uint8_t g, uint8_t b)
mutech 0:dff187a80020 121 {
mutech 0:dff187a80020 122 red = r;
mutech 0:dff187a80020 123 green = g;
mutech 0:dff187a80020 124 blue = b;
mutech 0:dff187a80020 125 }
mutech 0:dff187a80020 126
mutech 1:0288c920499c 127 RGBColor(int r, int g, int b)
mutech 1:0288c920499c 128 {
mutech 1:0288c920499c 129 red = (r < 0) ? 0 : ((RGB_MAX_VAL < r) ? RGB_MAX_VAL : r);
mutech 1:0288c920499c 130 green = (g < 0) ? 0 : ((RGB_MAX_VAL < g) ? RGB_MAX_VAL : g);
mutech 1:0288c920499c 131 blue = (b < 0) ? 0 : ((RGB_MAX_VAL < b) ? RGB_MAX_VAL : b);
mutech 1:0288c920499c 132 }
mutech 1:0288c920499c 133
mutech 27:bc79f444883b 134 RGBColor(const int rgb)
mutech 0:dff187a80020 135 {
mutech 0:dff187a80020 136 red = GetRValue(rgb);
mutech 0:dff187a80020 137 green = GetGValue(rgb);
mutech 0:dff187a80020 138 blue = GetBValue(rgb);
mutech 0:dff187a80020 139 }
mutech 0:dff187a80020 140
mutech 27:bc79f444883b 141 RGBColor(const HSVColor& hsv)
mutech 27:bc79f444883b 142 {
mutech 27:bc79f444883b 143 hsv2rgb(hsv, *this);
mutech 27:bc79f444883b 144 }
mutech 27:bc79f444883b 145
mutech 1:0288c920499c 146 RGBColor(HSVColor& hsv)
mutech 1:0288c920499c 147 {
mutech 1:0288c920499c 148 hsv2rgb(hsv, *this);
mutech 1:0288c920499c 149 }
mutech 1:0288c920499c 150
mutech 0:dff187a80020 151 /**
mutech 0:dff187a80020 152 Default constructor
mutech 0:dff187a80020 153 */
mutech 0:dff187a80020 154 RGBColor() {}
mutech 0:dff187a80020 155
mutech 0:dff187a80020 156 // allow copy construction
mutech 1:0288c920499c 157 RGBColor(const RGBColor& rhs)
mutech 0:dff187a80020 158 {
mutech 0:dff187a80020 159 red = rhs.red;
mutech 0:dff187a80020 160 green = rhs.green;
mutech 0:dff187a80020 161 blue = rhs.blue;
mutech 0:dff187a80020 162 }
mutech 0:dff187a80020 163
mutech 0:dff187a80020 164 // allow assignment from one RGB struct to another
mutech 1:0288c920499c 165 RGBColor& operator= (const RGBColor& rhs)
mutech 0:dff187a80020 166 {
mutech 0:dff187a80020 167 red = rhs.red;
mutech 0:dff187a80020 168 green = rhs.green;
mutech 0:dff187a80020 169 blue = rhs.blue;
mutech 0:dff187a80020 170 return *this;
mutech 0:dff187a80020 171 }
mutech 0:dff187a80020 172
mutech 1:0288c920499c 173 RGBColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 174 {
mutech 1:0288c920499c 175 hsv2rgb(rhs, *this);
mutech 1:0288c920499c 176 return *this;
mutech 1:0288c920499c 177 }
mutech 1:0288c920499c 178
mutech 1:0288c920499c 179 RGBColor& operator= (const int rgb)
mutech 0:dff187a80020 180 {
mutech 0:dff187a80020 181 red = GetRValue(rgb);
mutech 0:dff187a80020 182 green = GetGValue(rgb);
mutech 0:dff187a80020 183 blue = GetBValue(rgb);
mutech 0:dff187a80020 184 return *this;
mutech 0:dff187a80020 185 }
mutech 0:dff187a80020 186
mutech 3:786b31c65e7a 187 operator int()
mutech 0:dff187a80020 188 {
mutech 0:dff187a80020 189 return COLORREF(red, green, blue);
mutech 0:dff187a80020 190 }
mutech 1:0288c920499c 191
mutech 0:dff187a80020 192 };
mutech 0:dff187a80020 193
mutech 0:dff187a80020 194 //----------------------------------------------------------------------------
mutech 1:0288c920499c 195 /**
mutech 1:0288c920499c 196 HSV Color
mutech 1:0288c920499c 197 */
mutech 1:0288c920499c 198 struct HSVColor
mutech 1:0288c920499c 199 {
mutech 1:0288c920499c 200 union
mutech 1:0288c920499c 201 {
mutech 1:0288c920499c 202 struct
mutech 1:0288c920499c 203 {
mutech 1:0288c920499c 204 union {
mutech 1:0288c920499c 205 int16_t hue;
mutech 1:0288c920499c 206 int16_t h;
mutech 1:0288c920499c 207 };
mutech 1:0288c920499c 208 union {
mutech 1:0288c920499c 209 uint8_t saturation;
mutech 1:0288c920499c 210 uint8_t sat;
mutech 1:0288c920499c 211 uint8_t s;
mutech 1:0288c920499c 212 };
mutech 1:0288c920499c 213 union {
mutech 1:0288c920499c 214 uint8_t value;
mutech 1:0288c920499c 215 uint8_t val;
mutech 1:0288c920499c 216 uint8_t v;
mutech 1:0288c920499c 217 };
mutech 1:0288c920499c 218 };
mutech 10:db7308876f3e 219 uint8_t raw[4];
mutech 10:db7308876f3e 220 uint32_t code;
mutech 1:0288c920499c 221 };
mutech 1:0288c920499c 222
mutech 1:0288c920499c 223 int nomalize_hue(int h)
mutech 1:0288c920499c 224 {
mutech 1:0288c920499c 225 if (h < 0)
mutech 1:0288c920499c 226 h = HSV_MAX_HUE - (-h % HSV_MAX_HUE);
mutech 1:0288c920499c 227 return h % HSV_MAX_HUE;
mutech 1:0288c920499c 228 }
mutech 1:0288c920499c 229
mutech 1:0288c920499c 230 /**
mutech 1:0288c920499c 231 Constructor with hsv initializing
mutech 1:0288c920499c 232
mutech 1:0288c920499c 233 @param h - the hue byte
mutech 1:0288c920499c 234 @param s - the sat byte
mutech 1:0288c920499c 235 @param v - the val byte
mutech 1:0288c920499c 236 */
mutech 3:786b31c65e7a 237 HSVColor(int h, int s, int v)
mutech 1:0288c920499c 238 {
mutech 16:01e073c662d7 239 // hue = nomalize_hue(h);
mutech 16:01e073c662d7 240 hue = h;
mutech 1:0288c920499c 241 sat = (s < 0) ? 0 : ((HSV_MAX_SAT < s) ? HSV_MAX_SAT : s);
mutech 1:0288c920499c 242 val = (v < 0) ? 0 : ((HSV_MAX_VAL < v) ? HSV_MAX_VAL : v);
mutech 1:0288c920499c 243 }
mutech 1:0288c920499c 244
mutech 1:0288c920499c 245 HSVColor(int16_t h, uint8_t s, uint8_t v)
mutech 1:0288c920499c 246 {
mutech 16:01e073c662d7 247 // hue = nomalize_hue(h);
mutech 16:01e073c662d7 248 hue = h;
mutech 1:0288c920499c 249 sat = s;
mutech 1:0288c920499c 250 val = v;
mutech 1:0288c920499c 251 }
mutech 1:0288c920499c 252
mutech 27:bc79f444883b 253 HSVColor(const RGBColor& rgb)
mutech 27:bc79f444883b 254 {
mutech 27:bc79f444883b 255 rgb2hsv(rgb, *this);
mutech 27:bc79f444883b 256 }
mutech 27:bc79f444883b 257
mutech 1:0288c920499c 258 HSVColor(RGBColor& rgb)
mutech 1:0288c920499c 259 {
mutech 1:0288c920499c 260 rgb2hsv(rgb, *this);
mutech 1:0288c920499c 261 }
mutech 1:0288c920499c 262
mutech 27:bc79f444883b 263 HSVColor(const int rgbcolor)
mutech 3:786b31c65e7a 264 {
mutech 3:786b31c65e7a 265 RGBColor rgb(rgbcolor);
mutech 3:786b31c65e7a 266 rgb2hsv(rgb, *this);
mutech 3:786b31c65e7a 267 }
mutech 3:786b31c65e7a 268
mutech 1:0288c920499c 269 /**
mutech 1:0288c920499c 270 Default constructor
mutech 1:0288c920499c 271 */
mutech 1:0288c920499c 272 HSVColor() {}
mutech 1:0288c920499c 273
mutech 1:0288c920499c 274 // allow copy construction
mutech 1:0288c920499c 275 HSVColor(const HSVColor& rhs)
mutech 1:0288c920499c 276 {
mutech 1:0288c920499c 277 hue = rhs.hue;
mutech 1:0288c920499c 278 sat = rhs.sat;
mutech 1:0288c920499c 279 val = rhs.val;
mutech 1:0288c920499c 280 }
mutech 1:0288c920499c 281
mutech 1:0288c920499c 282 // allow assignment from one hsv struct to another
mutech 1:0288c920499c 283 HSVColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 284 {
mutech 1:0288c920499c 285 hue = rhs.hue;
mutech 1:0288c920499c 286 sat = rhs.sat;
mutech 1:0288c920499c 287 val = rhs.val;
mutech 1:0288c920499c 288 return *this;
mutech 1:0288c920499c 289 }
mutech 1:0288c920499c 290
mutech 1:0288c920499c 291 HSVColor& operator= (const RGBColor& rhs)
mutech 1:0288c920499c 292 {
mutech 1:0288c920499c 293 rgb2hsv(rhs, *this);
mutech 1:0288c920499c 294 return *this;
mutech 1:0288c920499c 295 }
mutech 1:0288c920499c 296
mutech 3:786b31c65e7a 297 HSVColor& operator= (const int& rhs)
mutech 3:786b31c65e7a 298 {
mutech 3:786b31c65e7a 299 RGBColor rgb(rhs);
mutech 3:786b31c65e7a 300 rgb2hsv(rhs, *this);
mutech 3:786b31c65e7a 301 return *this;
mutech 3:786b31c65e7a 302 }
mutech 3:786b31c65e7a 303
mutech 3:786b31c65e7a 304 operator int()
mutech 3:786b31c65e7a 305 {
mutech 3:786b31c65e7a 306 RGBColor rgb(*this);
mutech 3:786b31c65e7a 307 return (int)rgb;
mutech 3:786b31c65e7a 308 }
mutech 3:786b31c65e7a 309
mutech 1:0288c920499c 310 };
mutech 1:0288c920499c 311
mutech 1:0288c920499c 312 //----------------------------------------------------------------------------
mutech 6:5aff0da4b663 313 extern const uint8_t gamma20_table[256];
mutech 6:5aff0da4b663 314
mutech 6:5aff0da4b663 315 RGBColor GammaColor(RGBColor color);
mutech 30:59b70f91b471 316 RGBColor GammaColor(RGBColor color, int brightness);
mutech 45:1ec0f097fa0d 317 RGBColor* GammaCorrection(RGBColor *color, int size);
mutech 45:1ec0f097fa0d 318 HSVColor* GammaCorrection(HSVColor *color, int size);
mutech 6:5aff0da4b663 319
mutech 6:5aff0da4b663 320 //----------------------------------------------------------------------------
mutech 0:dff187a80020 321 #endif // end of COLORLIB_H