TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Sun Aug 14 18:11:42 2016 +0000
Revision:
10:db7308876f3e
Parent:
6:5aff0da4b663
Child:
13:2dce64fab69e
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 6:5aff0da4b663 76 };
mutech 6:5aff0da4b663 77
mutech 6:5aff0da4b663 78 //----------------------------------------------------------------------------
mutech 1:0288c920499c 79 struct RGBColor;
mutech 1:0288c920499c 80 struct HSVColor;
mutech 1:0288c920499c 81
mutech 1:0288c920499c 82 void rgb2hsv(RGBColor const& rgb, HSVColor& hsv);
mutech 1:0288c920499c 83 void hsv2rgb(HSVColor const& hsv, RGBColor& rgb);
mutech 0:dff187a80020 84
mutech 0:dff187a80020 85 //----------------------------------------------------------------------------
mutech 0:dff187a80020 86 /**
mutech 0:dff187a80020 87 RGB Color
mutech 0:dff187a80020 88 */
mutech 0:dff187a80020 89 struct RGBColor
mutech 0:dff187a80020 90 {
mutech 1:0288c920499c 91 union
mutech 1:0288c920499c 92 {
mutech 1:0288c920499c 93 struct
mutech 1:0288c920499c 94 {
mutech 1:0288c920499c 95 union {
mutech 1:0288c920499c 96 uint8_t r;
mutech 1:0288c920499c 97 uint8_t red;
mutech 1:0288c920499c 98 };
mutech 1:0288c920499c 99 union {
mutech 1:0288c920499c 100 uint8_t g;
mutech 1:0288c920499c 101 uint8_t green;
mutech 1:0288c920499c 102 };
mutech 1:0288c920499c 103 union {
mutech 1:0288c920499c 104 uint8_t b;
mutech 1:0288c920499c 105 uint8_t blue;
mutech 1:0288c920499c 106 };
mutech 1:0288c920499c 107 };
mutech 1:0288c920499c 108 uint8_t raw[3];
mutech 1:0288c920499c 109 };
mutech 0:dff187a80020 110
mutech 0:dff187a80020 111 /**
mutech 0:dff187a80020 112 Constructor with rgb initializing
mutech 1:0288c920499c 113
mutech 0:dff187a80020 114 @param r - the red byte
mutech 0:dff187a80020 115 @param g - the green byte
mutech 0:dff187a80020 116 @param b - the blue byte
mutech 0:dff187a80020 117 */
mutech 0:dff187a80020 118 RGBColor(uint8_t r, uint8_t g, uint8_t b)
mutech 0:dff187a80020 119 {
mutech 0:dff187a80020 120 red = r;
mutech 0:dff187a80020 121 green = g;
mutech 0:dff187a80020 122 blue = b;
mutech 0:dff187a80020 123 }
mutech 0:dff187a80020 124
mutech 1:0288c920499c 125 RGBColor(int r, int g, int b)
mutech 1:0288c920499c 126 {
mutech 1:0288c920499c 127 red = (r < 0) ? 0 : ((RGB_MAX_VAL < r) ? RGB_MAX_VAL : r);
mutech 1:0288c920499c 128 green = (g < 0) ? 0 : ((RGB_MAX_VAL < g) ? RGB_MAX_VAL : g);
mutech 1:0288c920499c 129 blue = (b < 0) ? 0 : ((RGB_MAX_VAL < b) ? RGB_MAX_VAL : b);
mutech 1:0288c920499c 130 }
mutech 1:0288c920499c 131
mutech 0:dff187a80020 132 RGBColor(int rgb)
mutech 0:dff187a80020 133 {
mutech 0:dff187a80020 134 red = GetRValue(rgb);
mutech 0:dff187a80020 135 green = GetGValue(rgb);
mutech 0:dff187a80020 136 blue = GetBValue(rgb);
mutech 0:dff187a80020 137 }
mutech 0:dff187a80020 138
mutech 1:0288c920499c 139 RGBColor(HSVColor& hsv)
mutech 1:0288c920499c 140 {
mutech 1:0288c920499c 141 hsv2rgb(hsv, *this);
mutech 1:0288c920499c 142 }
mutech 1:0288c920499c 143
mutech 0:dff187a80020 144 /**
mutech 0:dff187a80020 145 Default constructor
mutech 0:dff187a80020 146 */
mutech 0:dff187a80020 147 RGBColor() {}
mutech 0:dff187a80020 148
mutech 0:dff187a80020 149 // allow copy construction
mutech 1:0288c920499c 150 RGBColor(const RGBColor& rhs)
mutech 0:dff187a80020 151 {
mutech 0:dff187a80020 152 red = rhs.red;
mutech 0:dff187a80020 153 green = rhs.green;
mutech 0:dff187a80020 154 blue = rhs.blue;
mutech 0:dff187a80020 155 }
mutech 0:dff187a80020 156
mutech 0:dff187a80020 157 // allow assignment from one RGB struct to another
mutech 1:0288c920499c 158 RGBColor& operator= (const RGBColor& rhs)
mutech 0:dff187a80020 159 {
mutech 0:dff187a80020 160 red = rhs.red;
mutech 0:dff187a80020 161 green = rhs.green;
mutech 0:dff187a80020 162 blue = rhs.blue;
mutech 0:dff187a80020 163 return *this;
mutech 0:dff187a80020 164 }
mutech 0:dff187a80020 165
mutech 1:0288c920499c 166 RGBColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 167 {
mutech 1:0288c920499c 168 hsv2rgb(rhs, *this);
mutech 1:0288c920499c 169 return *this;
mutech 1:0288c920499c 170 }
mutech 1:0288c920499c 171
mutech 1:0288c920499c 172 RGBColor& operator= (const int rgb)
mutech 0:dff187a80020 173 {
mutech 0:dff187a80020 174 red = GetRValue(rgb);
mutech 0:dff187a80020 175 green = GetGValue(rgb);
mutech 0:dff187a80020 176 blue = GetBValue(rgb);
mutech 0:dff187a80020 177 return *this;
mutech 0:dff187a80020 178 }
mutech 0:dff187a80020 179
mutech 3:786b31c65e7a 180 operator int()
mutech 0:dff187a80020 181 {
mutech 0:dff187a80020 182 return COLORREF(red, green, blue);
mutech 0:dff187a80020 183 }
mutech 1:0288c920499c 184
mutech 0:dff187a80020 185 };
mutech 0:dff187a80020 186
mutech 0:dff187a80020 187 //----------------------------------------------------------------------------
mutech 1:0288c920499c 188 /**
mutech 1:0288c920499c 189 HSV Color
mutech 1:0288c920499c 190 */
mutech 1:0288c920499c 191 struct HSVColor
mutech 1:0288c920499c 192 {
mutech 1:0288c920499c 193 union
mutech 1:0288c920499c 194 {
mutech 1:0288c920499c 195 struct
mutech 1:0288c920499c 196 {
mutech 1:0288c920499c 197 union {
mutech 1:0288c920499c 198 int16_t hue;
mutech 1:0288c920499c 199 int16_t h;
mutech 1:0288c920499c 200 };
mutech 1:0288c920499c 201 union {
mutech 1:0288c920499c 202 uint8_t saturation;
mutech 1:0288c920499c 203 uint8_t sat;
mutech 1:0288c920499c 204 uint8_t s;
mutech 1:0288c920499c 205 };
mutech 1:0288c920499c 206 union {
mutech 1:0288c920499c 207 uint8_t value;
mutech 1:0288c920499c 208 uint8_t val;
mutech 1:0288c920499c 209 uint8_t v;
mutech 1:0288c920499c 210 };
mutech 1:0288c920499c 211 };
mutech 10:db7308876f3e 212 uint8_t raw[4];
mutech 10:db7308876f3e 213 uint32_t code;
mutech 1:0288c920499c 214 };
mutech 1:0288c920499c 215
mutech 1:0288c920499c 216 int nomalize_hue(int h)
mutech 1:0288c920499c 217 {
mutech 1:0288c920499c 218 if (h < 0)
mutech 1:0288c920499c 219 h = HSV_MAX_HUE - (-h % HSV_MAX_HUE);
mutech 1:0288c920499c 220 return h % HSV_MAX_HUE;
mutech 1:0288c920499c 221 }
mutech 1:0288c920499c 222
mutech 1:0288c920499c 223 /**
mutech 1:0288c920499c 224 Constructor with hsv initializing
mutech 1:0288c920499c 225
mutech 1:0288c920499c 226 @param h - the hue byte
mutech 1:0288c920499c 227 @param s - the sat byte
mutech 1:0288c920499c 228 @param v - the val byte
mutech 1:0288c920499c 229 */
mutech 3:786b31c65e7a 230 HSVColor(int h, int s, int v)
mutech 1:0288c920499c 231 {
mutech 1:0288c920499c 232 hue = nomalize_hue(h);
mutech 1:0288c920499c 233 sat = (s < 0) ? 0 : ((HSV_MAX_SAT < s) ? HSV_MAX_SAT : s);
mutech 1:0288c920499c 234 val = (v < 0) ? 0 : ((HSV_MAX_VAL < v) ? HSV_MAX_VAL : v);
mutech 1:0288c920499c 235 }
mutech 1:0288c920499c 236
mutech 1:0288c920499c 237 HSVColor(int16_t h, uint8_t s, uint8_t v)
mutech 1:0288c920499c 238 {
mutech 1:0288c920499c 239 hue = nomalize_hue(h);
mutech 1:0288c920499c 240 sat = s;
mutech 1:0288c920499c 241 val = v;
mutech 1:0288c920499c 242 }
mutech 1:0288c920499c 243
mutech 1:0288c920499c 244 HSVColor(RGBColor& rgb)
mutech 1:0288c920499c 245 {
mutech 1:0288c920499c 246 rgb2hsv(rgb, *this);
mutech 1:0288c920499c 247 }
mutech 1:0288c920499c 248
mutech 3:786b31c65e7a 249 HSVColor(int rgbcolor)
mutech 3:786b31c65e7a 250 {
mutech 3:786b31c65e7a 251 RGBColor rgb(rgbcolor);
mutech 3:786b31c65e7a 252 rgb2hsv(rgb, *this);
mutech 3:786b31c65e7a 253 }
mutech 3:786b31c65e7a 254
mutech 1:0288c920499c 255 /**
mutech 1:0288c920499c 256 Default constructor
mutech 1:0288c920499c 257 */
mutech 1:0288c920499c 258 HSVColor() {}
mutech 1:0288c920499c 259
mutech 1:0288c920499c 260 // allow copy construction
mutech 1:0288c920499c 261 HSVColor(const HSVColor& rhs)
mutech 1:0288c920499c 262 {
mutech 1:0288c920499c 263 hue = rhs.hue;
mutech 1:0288c920499c 264 sat = rhs.sat;
mutech 1:0288c920499c 265 val = rhs.val;
mutech 1:0288c920499c 266 }
mutech 1:0288c920499c 267
mutech 1:0288c920499c 268 // allow assignment from one hsv struct to another
mutech 1:0288c920499c 269 HSVColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 270 {
mutech 1:0288c920499c 271 hue = rhs.hue;
mutech 1:0288c920499c 272 sat = rhs.sat;
mutech 1:0288c920499c 273 val = rhs.val;
mutech 1:0288c920499c 274 return *this;
mutech 1:0288c920499c 275 }
mutech 1:0288c920499c 276
mutech 1:0288c920499c 277 HSVColor& operator= (const RGBColor& rhs)
mutech 1:0288c920499c 278 {
mutech 1:0288c920499c 279 rgb2hsv(rhs, *this);
mutech 1:0288c920499c 280 return *this;
mutech 1:0288c920499c 281 }
mutech 1:0288c920499c 282
mutech 3:786b31c65e7a 283 HSVColor& operator= (const int& rhs)
mutech 3:786b31c65e7a 284 {
mutech 3:786b31c65e7a 285 RGBColor rgb(rhs);
mutech 3:786b31c65e7a 286 rgb2hsv(rhs, *this);
mutech 3:786b31c65e7a 287 return *this;
mutech 3:786b31c65e7a 288 }
mutech 3:786b31c65e7a 289
mutech 3:786b31c65e7a 290 operator int()
mutech 3:786b31c65e7a 291 {
mutech 3:786b31c65e7a 292 RGBColor rgb(*this);
mutech 3:786b31c65e7a 293 return (int)rgb;
mutech 3:786b31c65e7a 294 }
mutech 3:786b31c65e7a 295
mutech 1:0288c920499c 296 };
mutech 1:0288c920499c 297
mutech 1:0288c920499c 298 //----------------------------------------------------------------------------
mutech 6:5aff0da4b663 299 extern const uint8_t gamma20_table[256];
mutech 6:5aff0da4b663 300
mutech 6:5aff0da4b663 301 RGBColor GammaColor(RGBColor color);
mutech 6:5aff0da4b663 302
mutech 6:5aff0da4b663 303 //----------------------------------------------------------------------------
mutech 0:dff187a80020 304 #endif // end of COLORLIB_H