TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Fri Aug 19 18:02:25 2016 +0000
Revision:
13:2dce64fab69e
Parent:
10:db7308876f3e
Child:
15:cd78625d83b6
WS2811/WS2812 Library

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 1:0288c920499c 29 #define RGB_MAX_VAL 255
mutech 1:0288c920499c 30 #define HSV_MAX_HUE 3600
mutech 1:0288c920499c 31 #define HSV_MAX_SAT 255
mutech 1:0288c920499c 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 13:2dce64fab69e 76 CK_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 0:dff187a80020 134 RGBColor(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 1:0288c920499c 141 RGBColor(HSVColor& hsv)
mutech 1:0288c920499c 142 {
mutech 1:0288c920499c 143 hsv2rgb(hsv, *this);
mutech 1:0288c920499c 144 }
mutech 1:0288c920499c 145
mutech 0:dff187a80020 146 /**
mutech 0:dff187a80020 147 Default constructor
mutech 0:dff187a80020 148 */
mutech 0:dff187a80020 149 RGBColor() {}
mutech 0:dff187a80020 150
mutech 0:dff187a80020 151 // allow copy construction
mutech 1:0288c920499c 152 RGBColor(const RGBColor& rhs)
mutech 0:dff187a80020 153 {
mutech 0:dff187a80020 154 red = rhs.red;
mutech 0:dff187a80020 155 green = rhs.green;
mutech 0:dff187a80020 156 blue = rhs.blue;
mutech 0:dff187a80020 157 }
mutech 0:dff187a80020 158
mutech 0:dff187a80020 159 // allow assignment from one RGB struct to another
mutech 1:0288c920499c 160 RGBColor& operator= (const RGBColor& rhs)
mutech 0:dff187a80020 161 {
mutech 0:dff187a80020 162 red = rhs.red;
mutech 0:dff187a80020 163 green = rhs.green;
mutech 0:dff187a80020 164 blue = rhs.blue;
mutech 0:dff187a80020 165 return *this;
mutech 0:dff187a80020 166 }
mutech 0:dff187a80020 167
mutech 1:0288c920499c 168 RGBColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 169 {
mutech 1:0288c920499c 170 hsv2rgb(rhs, *this);
mutech 1:0288c920499c 171 return *this;
mutech 1:0288c920499c 172 }
mutech 1:0288c920499c 173
mutech 1:0288c920499c 174 RGBColor& operator= (const int rgb)
mutech 0:dff187a80020 175 {
mutech 0:dff187a80020 176 red = GetRValue(rgb);
mutech 0:dff187a80020 177 green = GetGValue(rgb);
mutech 0:dff187a80020 178 blue = GetBValue(rgb);
mutech 0:dff187a80020 179 return *this;
mutech 0:dff187a80020 180 }
mutech 0:dff187a80020 181
mutech 3:786b31c65e7a 182 operator int()
mutech 0:dff187a80020 183 {
mutech 0:dff187a80020 184 return COLORREF(red, green, blue);
mutech 0:dff187a80020 185 }
mutech 1:0288c920499c 186
mutech 0:dff187a80020 187 };
mutech 0:dff187a80020 188
mutech 0:dff187a80020 189 //----------------------------------------------------------------------------
mutech 1:0288c920499c 190 /**
mutech 1:0288c920499c 191 HSV Color
mutech 1:0288c920499c 192 */
mutech 1:0288c920499c 193 struct HSVColor
mutech 1:0288c920499c 194 {
mutech 1:0288c920499c 195 union
mutech 1:0288c920499c 196 {
mutech 1:0288c920499c 197 struct
mutech 1:0288c920499c 198 {
mutech 1:0288c920499c 199 union {
mutech 1:0288c920499c 200 int16_t hue;
mutech 1:0288c920499c 201 int16_t h;
mutech 1:0288c920499c 202 };
mutech 1:0288c920499c 203 union {
mutech 1:0288c920499c 204 uint8_t saturation;
mutech 1:0288c920499c 205 uint8_t sat;
mutech 1:0288c920499c 206 uint8_t s;
mutech 1:0288c920499c 207 };
mutech 1:0288c920499c 208 union {
mutech 1:0288c920499c 209 uint8_t value;
mutech 1:0288c920499c 210 uint8_t val;
mutech 1:0288c920499c 211 uint8_t v;
mutech 1:0288c920499c 212 };
mutech 1:0288c920499c 213 };
mutech 10:db7308876f3e 214 uint8_t raw[4];
mutech 10:db7308876f3e 215 uint32_t code;
mutech 1:0288c920499c 216 };
mutech 1:0288c920499c 217
mutech 1:0288c920499c 218 int nomalize_hue(int h)
mutech 1:0288c920499c 219 {
mutech 1:0288c920499c 220 if (h < 0)
mutech 1:0288c920499c 221 h = HSV_MAX_HUE - (-h % HSV_MAX_HUE);
mutech 1:0288c920499c 222 return h % HSV_MAX_HUE;
mutech 1:0288c920499c 223 }
mutech 1:0288c920499c 224
mutech 1:0288c920499c 225 /**
mutech 1:0288c920499c 226 Constructor with hsv initializing
mutech 1:0288c920499c 227
mutech 1:0288c920499c 228 @param h - the hue byte
mutech 1:0288c920499c 229 @param s - the sat byte
mutech 1:0288c920499c 230 @param v - the val byte
mutech 1:0288c920499c 231 */
mutech 3:786b31c65e7a 232 HSVColor(int h, int s, int v)
mutech 1:0288c920499c 233 {
mutech 1:0288c920499c 234 hue = nomalize_hue(h);
mutech 1:0288c920499c 235 sat = (s < 0) ? 0 : ((HSV_MAX_SAT < s) ? HSV_MAX_SAT : s);
mutech 1:0288c920499c 236 val = (v < 0) ? 0 : ((HSV_MAX_VAL < v) ? HSV_MAX_VAL : v);
mutech 1:0288c920499c 237 }
mutech 1:0288c920499c 238
mutech 1:0288c920499c 239 HSVColor(int16_t h, uint8_t s, uint8_t v)
mutech 1:0288c920499c 240 {
mutech 1:0288c920499c 241 hue = nomalize_hue(h);
mutech 1:0288c920499c 242 sat = s;
mutech 1:0288c920499c 243 val = v;
mutech 1:0288c920499c 244 }
mutech 1:0288c920499c 245
mutech 1:0288c920499c 246 HSVColor(RGBColor& rgb)
mutech 1:0288c920499c 247 {
mutech 1:0288c920499c 248 rgb2hsv(rgb, *this);
mutech 1:0288c920499c 249 }
mutech 1:0288c920499c 250
mutech 3:786b31c65e7a 251 HSVColor(int rgbcolor)
mutech 3:786b31c65e7a 252 {
mutech 3:786b31c65e7a 253 RGBColor rgb(rgbcolor);
mutech 3:786b31c65e7a 254 rgb2hsv(rgb, *this);
mutech 3:786b31c65e7a 255 }
mutech 3:786b31c65e7a 256
mutech 1:0288c920499c 257 /**
mutech 1:0288c920499c 258 Default constructor
mutech 1:0288c920499c 259 */
mutech 1:0288c920499c 260 HSVColor() {}
mutech 1:0288c920499c 261
mutech 1:0288c920499c 262 // allow copy construction
mutech 1:0288c920499c 263 HSVColor(const HSVColor& rhs)
mutech 1:0288c920499c 264 {
mutech 1:0288c920499c 265 hue = rhs.hue;
mutech 1:0288c920499c 266 sat = rhs.sat;
mutech 1:0288c920499c 267 val = rhs.val;
mutech 1:0288c920499c 268 }
mutech 1:0288c920499c 269
mutech 1:0288c920499c 270 // allow assignment from one hsv struct to another
mutech 1:0288c920499c 271 HSVColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 272 {
mutech 1:0288c920499c 273 hue = rhs.hue;
mutech 1:0288c920499c 274 sat = rhs.sat;
mutech 1:0288c920499c 275 val = rhs.val;
mutech 1:0288c920499c 276 return *this;
mutech 1:0288c920499c 277 }
mutech 1:0288c920499c 278
mutech 1:0288c920499c 279 HSVColor& operator= (const RGBColor& rhs)
mutech 1:0288c920499c 280 {
mutech 1:0288c920499c 281 rgb2hsv(rhs, *this);
mutech 1:0288c920499c 282 return *this;
mutech 1:0288c920499c 283 }
mutech 1:0288c920499c 284
mutech 3:786b31c65e7a 285 HSVColor& operator= (const int& rhs)
mutech 3:786b31c65e7a 286 {
mutech 3:786b31c65e7a 287 RGBColor rgb(rhs);
mutech 3:786b31c65e7a 288 rgb2hsv(rhs, *this);
mutech 3:786b31c65e7a 289 return *this;
mutech 3:786b31c65e7a 290 }
mutech 3:786b31c65e7a 291
mutech 3:786b31c65e7a 292 operator int()
mutech 3:786b31c65e7a 293 {
mutech 3:786b31c65e7a 294 RGBColor rgb(*this);
mutech 3:786b31c65e7a 295 return (int)rgb;
mutech 3:786b31c65e7a 296 }
mutech 3:786b31c65e7a 297
mutech 1:0288c920499c 298 };
mutech 1:0288c920499c 299
mutech 1:0288c920499c 300 //----------------------------------------------------------------------------
mutech 6:5aff0da4b663 301 extern const uint8_t gamma20_table[256];
mutech 6:5aff0da4b663 302
mutech 6:5aff0da4b663 303 RGBColor GammaColor(RGBColor color);
mutech 6:5aff0da4b663 304
mutech 6:5aff0da4b663 305 //----------------------------------------------------------------------------
mutech 0:dff187a80020 306 #endif // end of COLORLIB_H