TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Wed Jul 27 08:12:04 2016 +0000
Revision:
1:0288c920499c
Parent:
0:dff187a80020
Child:
2:cc8e091fd975
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 1:0288c920499c 39 struct RGBColor;
mutech 1:0288c920499c 40 struct HSVColor;
mutech 1:0288c920499c 41
mutech 1:0288c920499c 42 void rgb2hsv(RGBColor const& rgb, HSVColor& hsv);
mutech 1:0288c920499c 43 void hsv2rgb(HSVColor const& hsv, RGBColor& rgb);
mutech 0:dff187a80020 44
mutech 0:dff187a80020 45 //----------------------------------------------------------------------------
mutech 0:dff187a80020 46 /**
mutech 0:dff187a80020 47 RGB Color
mutech 0:dff187a80020 48 */
mutech 0:dff187a80020 49 struct RGBColor
mutech 0:dff187a80020 50 {
mutech 1:0288c920499c 51 union
mutech 1:0288c920499c 52 {
mutech 1:0288c920499c 53 struct
mutech 1:0288c920499c 54 {
mutech 1:0288c920499c 55 union {
mutech 1:0288c920499c 56 uint8_t r;
mutech 1:0288c920499c 57 uint8_t red;
mutech 1:0288c920499c 58 };
mutech 1:0288c920499c 59 union {
mutech 1:0288c920499c 60 uint8_t g;
mutech 1:0288c920499c 61 uint8_t green;
mutech 1:0288c920499c 62 };
mutech 1:0288c920499c 63 union {
mutech 1:0288c920499c 64 uint8_t b;
mutech 1:0288c920499c 65 uint8_t blue;
mutech 1:0288c920499c 66 };
mutech 1:0288c920499c 67 };
mutech 1:0288c920499c 68 uint8_t raw[3];
mutech 1:0288c920499c 69 };
mutech 0:dff187a80020 70
mutech 0:dff187a80020 71 /**
mutech 0:dff187a80020 72 Constructor with rgb initializing
mutech 1:0288c920499c 73
mutech 0:dff187a80020 74 @param r - the red byte
mutech 0:dff187a80020 75 @param g - the green byte
mutech 0:dff187a80020 76 @param b - the blue byte
mutech 0:dff187a80020 77 */
mutech 0:dff187a80020 78 RGBColor(uint8_t r, uint8_t g, uint8_t b)
mutech 0:dff187a80020 79 {
mutech 0:dff187a80020 80 red = r;
mutech 0:dff187a80020 81 green = g;
mutech 0:dff187a80020 82 blue = b;
mutech 0:dff187a80020 83 }
mutech 0:dff187a80020 84
mutech 1:0288c920499c 85 RGBColor(int r, int g, int b)
mutech 1:0288c920499c 86 {
mutech 1:0288c920499c 87 red = (r < 0) ? 0 : ((RGB_MAX_VAL < r) ? RGB_MAX_VAL : r);
mutech 1:0288c920499c 88 green = (g < 0) ? 0 : ((RGB_MAX_VAL < g) ? RGB_MAX_VAL : g);
mutech 1:0288c920499c 89 blue = (b < 0) ? 0 : ((RGB_MAX_VAL < b) ? RGB_MAX_VAL : b);
mutech 1:0288c920499c 90 }
mutech 1:0288c920499c 91
mutech 0:dff187a80020 92 RGBColor(uint32_t rgb)
mutech 0:dff187a80020 93 {
mutech 0:dff187a80020 94 red = GetRValue(rgb);
mutech 0:dff187a80020 95 green = GetGValue(rgb);
mutech 0:dff187a80020 96 blue = GetBValue(rgb);
mutech 0:dff187a80020 97 }
mutech 0:dff187a80020 98
mutech 0:dff187a80020 99 RGBColor(int rgb)
mutech 0:dff187a80020 100 {
mutech 0:dff187a80020 101 red = GetRValue(rgb);
mutech 0:dff187a80020 102 green = GetGValue(rgb);
mutech 0:dff187a80020 103 blue = GetBValue(rgb);
mutech 0:dff187a80020 104 }
mutech 0:dff187a80020 105
mutech 1:0288c920499c 106 RGBColor(HSVColor& hsv)
mutech 1:0288c920499c 107 {
mutech 1:0288c920499c 108 hsv2rgb(hsv, *this);
mutech 1:0288c920499c 109 }
mutech 1:0288c920499c 110
mutech 0:dff187a80020 111 /**
mutech 0:dff187a80020 112 Default constructor
mutech 0:dff187a80020 113 */
mutech 0:dff187a80020 114 RGBColor() {}
mutech 0:dff187a80020 115
mutech 0:dff187a80020 116 // allow copy construction
mutech 1:0288c920499c 117 RGBColor(const RGBColor& rhs)
mutech 0:dff187a80020 118 {
mutech 0:dff187a80020 119 red = rhs.red;
mutech 0:dff187a80020 120 green = rhs.green;
mutech 0:dff187a80020 121 blue = rhs.blue;
mutech 0:dff187a80020 122 }
mutech 0:dff187a80020 123
mutech 0:dff187a80020 124 // allow assignment from one RGB struct to another
mutech 1:0288c920499c 125 RGBColor& operator= (const RGBColor& rhs)
mutech 0:dff187a80020 126 {
mutech 0:dff187a80020 127 red = rhs.red;
mutech 0:dff187a80020 128 green = rhs.green;
mutech 0:dff187a80020 129 blue = rhs.blue;
mutech 0:dff187a80020 130 return *this;
mutech 0:dff187a80020 131 }
mutech 0:dff187a80020 132
mutech 1:0288c920499c 133 RGBColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 134 {
mutech 1:0288c920499c 135 hsv2rgb(rhs, *this);
mutech 1:0288c920499c 136 return *this;
mutech 1:0288c920499c 137 }
mutech 1:0288c920499c 138
mutech 1:0288c920499c 139 RGBColor& operator= (const uint32_t rgb)
mutech 0:dff187a80020 140 {
mutech 0:dff187a80020 141 red = GetRValue(rgb);
mutech 0:dff187a80020 142 green = GetGValue(rgb);
mutech 0:dff187a80020 143 blue = GetBValue(rgb);
mutech 0:dff187a80020 144 return *this;
mutech 0:dff187a80020 145 }
mutech 0:dff187a80020 146
mutech 1:0288c920499c 147 RGBColor& operator= (const int rgb)
mutech 0:dff187a80020 148 {
mutech 0:dff187a80020 149 red = GetRValue(rgb);
mutech 0:dff187a80020 150 green = GetGValue(rgb);
mutech 0:dff187a80020 151 blue = GetBValue(rgb);
mutech 0:dff187a80020 152 return *this;
mutech 0:dff187a80020 153 }
mutech 0:dff187a80020 154
mutech 0:dff187a80020 155 operator uint32_t()
mutech 0:dff187a80020 156 {
mutech 0:dff187a80020 157 return COLORREF(red, green, blue);
mutech 0:dff187a80020 158 }
mutech 1:0288c920499c 159
mutech 0:dff187a80020 160 operator int()
mutech 0:dff187a80020 161 {
mutech 0:dff187a80020 162 return COLORREF(red, green, blue);
mutech 0:dff187a80020 163 }
mutech 0:dff187a80020 164 };
mutech 0:dff187a80020 165
mutech 0:dff187a80020 166 //----------------------------------------------------------------------------
mutech 1:0288c920499c 167 /**
mutech 1:0288c920499c 168 HSV Color
mutech 1:0288c920499c 169 */
mutech 1:0288c920499c 170 struct HSVColor
mutech 1:0288c920499c 171 {
mutech 1:0288c920499c 172 union
mutech 1:0288c920499c 173 {
mutech 1:0288c920499c 174 struct
mutech 1:0288c920499c 175 {
mutech 1:0288c920499c 176 union {
mutech 1:0288c920499c 177 int16_t hue;
mutech 1:0288c920499c 178 int16_t h;
mutech 1:0288c920499c 179 };
mutech 1:0288c920499c 180 union {
mutech 1:0288c920499c 181 uint8_t saturation;
mutech 1:0288c920499c 182 uint8_t sat;
mutech 1:0288c920499c 183 uint8_t s;
mutech 1:0288c920499c 184 };
mutech 1:0288c920499c 185 union {
mutech 1:0288c920499c 186 uint8_t value;
mutech 1:0288c920499c 187 uint8_t val;
mutech 1:0288c920499c 188 uint8_t v;
mutech 1:0288c920499c 189 };
mutech 1:0288c920499c 190 };
mutech 1:0288c920499c 191 uint8_t raw[4];
mutech 1:0288c920499c 192 };
mutech 1:0288c920499c 193
mutech 1:0288c920499c 194 int nomalize_hue(int h)
mutech 1:0288c920499c 195 {
mutech 1:0288c920499c 196 if (h < 0)
mutech 1:0288c920499c 197 h = HSV_MAX_HUE - (-h % HSV_MAX_HUE);
mutech 1:0288c920499c 198 return h % HSV_MAX_HUE;
mutech 1:0288c920499c 199 }
mutech 1:0288c920499c 200
mutech 1:0288c920499c 201 /**
mutech 1:0288c920499c 202 Constructor with hsv initializing
mutech 1:0288c920499c 203
mutech 1:0288c920499c 204 @param h - the hue byte
mutech 1:0288c920499c 205 @param s - the sat byte
mutech 1:0288c920499c 206 @param v - the val byte
mutech 1:0288c920499c 207 */
mutech 1:0288c920499c 208 HSVColor(int h, int s = HSV_MAX_SAT, int v = HSV_MAX_VAL)
mutech 1:0288c920499c 209 {
mutech 1:0288c920499c 210 hue = nomalize_hue(h);
mutech 1:0288c920499c 211 sat = (s < 0) ? 0 : ((HSV_MAX_SAT < s) ? HSV_MAX_SAT : s);
mutech 1:0288c920499c 212 val = (v < 0) ? 0 : ((HSV_MAX_VAL < v) ? HSV_MAX_VAL : v);
mutech 1:0288c920499c 213 }
mutech 1:0288c920499c 214
mutech 1:0288c920499c 215 HSVColor(int16_t h, uint8_t s, uint8_t v)
mutech 1:0288c920499c 216 {
mutech 1:0288c920499c 217 hue = nomalize_hue(h);
mutech 1:0288c920499c 218 sat = s;
mutech 1:0288c920499c 219 val = v;
mutech 1:0288c920499c 220 }
mutech 1:0288c920499c 221
mutech 1:0288c920499c 222 HSVColor(RGBColor& rgb)
mutech 1:0288c920499c 223 {
mutech 1:0288c920499c 224 rgb2hsv(rgb, *this);
mutech 1:0288c920499c 225 }
mutech 1:0288c920499c 226
mutech 1:0288c920499c 227 /**
mutech 1:0288c920499c 228 Default constructor
mutech 1:0288c920499c 229 */
mutech 1:0288c920499c 230 HSVColor() {}
mutech 1:0288c920499c 231
mutech 1:0288c920499c 232 // allow copy construction
mutech 1:0288c920499c 233 HSVColor(const HSVColor& rhs)
mutech 1:0288c920499c 234 {
mutech 1:0288c920499c 235 hue = rhs.hue;
mutech 1:0288c920499c 236 sat = rhs.sat;
mutech 1:0288c920499c 237 val = rhs.val;
mutech 1:0288c920499c 238 }
mutech 1:0288c920499c 239
mutech 1:0288c920499c 240 // allow assignment from one hsv struct to another
mutech 1:0288c920499c 241 HSVColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 242 {
mutech 1:0288c920499c 243 hue = rhs.hue;
mutech 1:0288c920499c 244 sat = rhs.sat;
mutech 1:0288c920499c 245 val = rhs.val;
mutech 1:0288c920499c 246 return *this;
mutech 1:0288c920499c 247 }
mutech 1:0288c920499c 248
mutech 1:0288c920499c 249 HSVColor& operator= (const RGBColor& rhs)
mutech 1:0288c920499c 250 {
mutech 1:0288c920499c 251 rgb2hsv(rhs, *this);
mutech 1:0288c920499c 252 return *this;
mutech 1:0288c920499c 253 }
mutech 1:0288c920499c 254
mutech 1:0288c920499c 255 operator RGBColor()
mutech 1:0288c920499c 256 {
mutech 1:0288c920499c 257 RGBColor rgb;
mutech 1:0288c920499c 258 hsv2rgb(*this, rgb);
mutech 1:0288c920499c 259 return rgb;
mutech 1:0288c920499c 260 }
mutech 1:0288c920499c 261
mutech 1:0288c920499c 262 };
mutech 1:0288c920499c 263
mutech 1:0288c920499c 264 //----------------------------------------------------------------------------
mutech 0:dff187a80020 265 #endif // end of COLORLIB_H