TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Thu Jul 28 05:43:06 2016 +0000
Revision:
3:786b31c65e7a
Parent:
2:cc8e091fd975
Child:
6:5aff0da4b663
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(int 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 1:0288c920499c 99 RGBColor(HSVColor& hsv)
mutech 1:0288c920499c 100 {
mutech 1:0288c920499c 101 hsv2rgb(hsv, *this);
mutech 1:0288c920499c 102 }
mutech 1:0288c920499c 103
mutech 0:dff187a80020 104 /**
mutech 0:dff187a80020 105 Default constructor
mutech 0:dff187a80020 106 */
mutech 0:dff187a80020 107 RGBColor() {}
mutech 0:dff187a80020 108
mutech 0:dff187a80020 109 // allow copy construction
mutech 1:0288c920499c 110 RGBColor(const RGBColor& rhs)
mutech 0:dff187a80020 111 {
mutech 0:dff187a80020 112 red = rhs.red;
mutech 0:dff187a80020 113 green = rhs.green;
mutech 0:dff187a80020 114 blue = rhs.blue;
mutech 0:dff187a80020 115 }
mutech 0:dff187a80020 116
mutech 0:dff187a80020 117 // allow assignment from one RGB struct to another
mutech 1:0288c920499c 118 RGBColor& operator= (const RGBColor& rhs)
mutech 0:dff187a80020 119 {
mutech 0:dff187a80020 120 red = rhs.red;
mutech 0:dff187a80020 121 green = rhs.green;
mutech 0:dff187a80020 122 blue = rhs.blue;
mutech 0:dff187a80020 123 return *this;
mutech 0:dff187a80020 124 }
mutech 0:dff187a80020 125
mutech 1:0288c920499c 126 RGBColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 127 {
mutech 1:0288c920499c 128 hsv2rgb(rhs, *this);
mutech 1:0288c920499c 129 return *this;
mutech 1:0288c920499c 130 }
mutech 1:0288c920499c 131
mutech 1:0288c920499c 132 RGBColor& operator= (const 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 return *this;
mutech 0:dff187a80020 138 }
mutech 0:dff187a80020 139
mutech 3:786b31c65e7a 140 operator int()
mutech 0:dff187a80020 141 {
mutech 0:dff187a80020 142 return COLORREF(red, green, blue);
mutech 0:dff187a80020 143 }
mutech 1:0288c920499c 144
mutech 0:dff187a80020 145 };
mutech 0:dff187a80020 146
mutech 0:dff187a80020 147 //----------------------------------------------------------------------------
mutech 1:0288c920499c 148 /**
mutech 1:0288c920499c 149 HSV Color
mutech 1:0288c920499c 150 */
mutech 1:0288c920499c 151 struct HSVColor
mutech 1:0288c920499c 152 {
mutech 1:0288c920499c 153 union
mutech 1:0288c920499c 154 {
mutech 1:0288c920499c 155 struct
mutech 1:0288c920499c 156 {
mutech 1:0288c920499c 157 union {
mutech 1:0288c920499c 158 int16_t hue;
mutech 1:0288c920499c 159 int16_t h;
mutech 1:0288c920499c 160 };
mutech 1:0288c920499c 161 union {
mutech 1:0288c920499c 162 uint8_t saturation;
mutech 1:0288c920499c 163 uint8_t sat;
mutech 1:0288c920499c 164 uint8_t s;
mutech 1:0288c920499c 165 };
mutech 1:0288c920499c 166 union {
mutech 1:0288c920499c 167 uint8_t value;
mutech 1:0288c920499c 168 uint8_t val;
mutech 1:0288c920499c 169 uint8_t v;
mutech 1:0288c920499c 170 };
mutech 1:0288c920499c 171 };
mutech 1:0288c920499c 172 uint8_t raw[4];
mutech 1:0288c920499c 173 };
mutech 1:0288c920499c 174
mutech 1:0288c920499c 175 int nomalize_hue(int h)
mutech 1:0288c920499c 176 {
mutech 1:0288c920499c 177 if (h < 0)
mutech 1:0288c920499c 178 h = HSV_MAX_HUE - (-h % HSV_MAX_HUE);
mutech 1:0288c920499c 179 return h % HSV_MAX_HUE;
mutech 1:0288c920499c 180 }
mutech 1:0288c920499c 181
mutech 1:0288c920499c 182 /**
mutech 1:0288c920499c 183 Constructor with hsv initializing
mutech 1:0288c920499c 184
mutech 1:0288c920499c 185 @param h - the hue byte
mutech 1:0288c920499c 186 @param s - the sat byte
mutech 1:0288c920499c 187 @param v - the val byte
mutech 1:0288c920499c 188 */
mutech 3:786b31c65e7a 189 HSVColor(int h, int s, int v)
mutech 1:0288c920499c 190 {
mutech 1:0288c920499c 191 hue = nomalize_hue(h);
mutech 1:0288c920499c 192 sat = (s < 0) ? 0 : ((HSV_MAX_SAT < s) ? HSV_MAX_SAT : s);
mutech 1:0288c920499c 193 val = (v < 0) ? 0 : ((HSV_MAX_VAL < v) ? HSV_MAX_VAL : v);
mutech 1:0288c920499c 194 }
mutech 1:0288c920499c 195
mutech 1:0288c920499c 196 HSVColor(int16_t h, uint8_t s, uint8_t v)
mutech 1:0288c920499c 197 {
mutech 1:0288c920499c 198 hue = nomalize_hue(h);
mutech 1:0288c920499c 199 sat = s;
mutech 1:0288c920499c 200 val = v;
mutech 1:0288c920499c 201 }
mutech 1:0288c920499c 202
mutech 1:0288c920499c 203 HSVColor(RGBColor& rgb)
mutech 1:0288c920499c 204 {
mutech 1:0288c920499c 205 rgb2hsv(rgb, *this);
mutech 1:0288c920499c 206 }
mutech 1:0288c920499c 207
mutech 3:786b31c65e7a 208 HSVColor(int rgbcolor)
mutech 3:786b31c65e7a 209 {
mutech 3:786b31c65e7a 210 RGBColor rgb(rgbcolor);
mutech 3:786b31c65e7a 211 rgb2hsv(rgb, *this);
mutech 3:786b31c65e7a 212 }
mutech 3:786b31c65e7a 213
mutech 1:0288c920499c 214 /**
mutech 1:0288c920499c 215 Default constructor
mutech 1:0288c920499c 216 */
mutech 1:0288c920499c 217 HSVColor() {}
mutech 1:0288c920499c 218
mutech 1:0288c920499c 219 // allow copy construction
mutech 1:0288c920499c 220 HSVColor(const HSVColor& rhs)
mutech 1:0288c920499c 221 {
mutech 1:0288c920499c 222 hue = rhs.hue;
mutech 1:0288c920499c 223 sat = rhs.sat;
mutech 1:0288c920499c 224 val = rhs.val;
mutech 1:0288c920499c 225 }
mutech 1:0288c920499c 226
mutech 1:0288c920499c 227 // allow assignment from one hsv struct to another
mutech 1:0288c920499c 228 HSVColor& operator= (const HSVColor& rhs)
mutech 1:0288c920499c 229 {
mutech 1:0288c920499c 230 hue = rhs.hue;
mutech 1:0288c920499c 231 sat = rhs.sat;
mutech 1:0288c920499c 232 val = rhs.val;
mutech 1:0288c920499c 233 return *this;
mutech 1:0288c920499c 234 }
mutech 1:0288c920499c 235
mutech 1:0288c920499c 236 HSVColor& operator= (const RGBColor& rhs)
mutech 1:0288c920499c 237 {
mutech 1:0288c920499c 238 rgb2hsv(rhs, *this);
mutech 1:0288c920499c 239 return *this;
mutech 1:0288c920499c 240 }
mutech 1:0288c920499c 241
mutech 3:786b31c65e7a 242 HSVColor& operator= (const int& rhs)
mutech 3:786b31c65e7a 243 {
mutech 3:786b31c65e7a 244 RGBColor rgb(rhs);
mutech 3:786b31c65e7a 245 rgb2hsv(rhs, *this);
mutech 3:786b31c65e7a 246 return *this;
mutech 3:786b31c65e7a 247 }
mutech 3:786b31c65e7a 248
mutech 3:786b31c65e7a 249 operator int()
mutech 3:786b31c65e7a 250 {
mutech 3:786b31c65e7a 251 RGBColor rgb(*this);
mutech 3:786b31c65e7a 252 return (int)rgb;
mutech 3:786b31c65e7a 253 }
mutech 3:786b31c65e7a 254
mutech 1:0288c920499c 255 };
mutech 1:0288c920499c 256
mutech 1:0288c920499c 257 //----------------------------------------------------------------------------
mutech 0:dff187a80020 258 #endif // end of COLORLIB_H