Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: ColorLib.h
- Revision:
- 1:0288c920499c
- Parent:
- 0:dff187a80020
- Child:
- 2:cc8e091fd975
diff -r dff187a80020 -r 0288c920499c ColorLib.h
--- a/ColorLib.h Tue Jul 26 18:09:24 2016 +0000
+++ b/ColorLib.h Wed Jul 27 08:12:04 2016 +0000
@@ -26,10 +26,21 @@
#include <stdint.h>
//----------------------------------------------------------------------------
-#define GetRValue(c) ((uint8_t)(c))
-#define GetGValue(c) ((uint8_t)((c) >> 8))
-#define GetBValue(c) ((uint8_t)((c) >> 16))
-#define COLORREF(r, g ,b) ((uint32_t)((uint8_t)(r) | ((uint8_t)(g) << 8) | ((uint8_t)(b) << 16)))
+#define RGB_MAX_VAL 255
+#define HSV_MAX_HUE 3600
+#define HSV_MAX_SAT 255
+#define HSV_MAX_VAL 255
+
+#define GetRValue(rgb) ((uint8_t)(rgb))
+#define GetGValue(rgb) ((uint8_t)((rgb) >> 8))
+#define GetBValue(rgb) ((uint8_t)((rgb) >> 16))
+#define COLORREF(r, g ,b) ((uint32_t)((uint8_t)(r) | ((uint8_t)(g) << 8) | ((uint8_t)(b) << 16)))
+
+struct RGBColor;
+struct HSVColor;
+
+void rgb2hsv(RGBColor const& rgb, HSVColor& hsv);
+void hsv2rgb(HSVColor const& hsv, RGBColor& rgb);
//----------------------------------------------------------------------------
/**
@@ -37,13 +48,29 @@
*/
struct RGBColor
{
- uint8_t red;
- uint8_t green;
- uint8_t blue;
+ union
+ {
+ struct
+ {
+ union {
+ uint8_t r;
+ uint8_t red;
+ };
+ union {
+ uint8_t g;
+ uint8_t green;
+ };
+ union {
+ uint8_t b;
+ uint8_t blue;
+ };
+ };
+ uint8_t raw[3];
+ };
/**
Constructor with rgb initializing
-
+
@param r - the red byte
@param g - the green byte
@param b - the blue byte
@@ -55,6 +82,13 @@
blue = b;
}
+ RGBColor(int r, int g, int b)
+ {
+ red = (r < 0) ? 0 : ((RGB_MAX_VAL < r) ? RGB_MAX_VAL : r);
+ green = (g < 0) ? 0 : ((RGB_MAX_VAL < g) ? RGB_MAX_VAL : g);
+ blue = (b < 0) ? 0 : ((RGB_MAX_VAL < b) ? RGB_MAX_VAL : b);
+ }
+
RGBColor(uint32_t rgb)
{
red = GetRValue(rgb);
@@ -69,13 +103,18 @@
blue = GetBValue(rgb);
}
+ RGBColor(HSVColor& hsv)
+ {
+ hsv2rgb(hsv, *this);
+ }
+
/**
Default constructor
*/
RGBColor() {}
// allow copy construction
- inline RGBColor(const RGBColor& rhs)
+ RGBColor(const RGBColor& rhs)
{
red = rhs.red;
green = rhs.green;
@@ -83,7 +122,7 @@
}
// allow assignment from one RGB struct to another
- inline RGBColor& operator= (const RGBColor& rhs)
+ RGBColor& operator= (const RGBColor& rhs)
{
red = rhs.red;
green = rhs.green;
@@ -91,7 +130,13 @@
return *this;
}
- inline RGBColor& operator= (const uint32_t rgb)
+ RGBColor& operator= (const HSVColor& rhs)
+ {
+ hsv2rgb(rhs, *this);
+ return *this;
+ }
+
+ RGBColor& operator= (const uint32_t rgb)
{
red = GetRValue(rgb);
green = GetGValue(rgb);
@@ -99,7 +144,7 @@
return *this;
}
- inline RGBColor& operator= (const int rgb)
+ RGBColor& operator= (const int rgb)
{
red = GetRValue(rgb);
green = GetGValue(rgb);
@@ -111,7 +156,7 @@
{
return COLORREF(red, green, blue);
}
-
+
operator int()
{
return COLORREF(red, green, blue);
@@ -119,4 +164,102 @@
};
//----------------------------------------------------------------------------
+/**
+ HSV Color
+*/
+struct HSVColor
+{
+ union
+ {
+ struct
+ {
+ union {
+ int16_t hue;
+ int16_t h;
+ };
+ union {
+ uint8_t saturation;
+ uint8_t sat;
+ uint8_t s;
+ };
+ union {
+ uint8_t value;
+ uint8_t val;
+ uint8_t v;
+ };
+ };
+ uint8_t raw[4];
+ };
+
+ int nomalize_hue(int h)
+ {
+ if (h < 0)
+ h = HSV_MAX_HUE - (-h % HSV_MAX_HUE);
+ return h % HSV_MAX_HUE;
+ }
+
+ /**
+ Constructor with hsv initializing
+
+ @param h - the hue byte
+ @param s - the sat byte
+ @param v - the val byte
+ */
+ HSVColor(int h, int s = HSV_MAX_SAT, int v = HSV_MAX_VAL)
+ {
+ hue = nomalize_hue(h);
+ sat = (s < 0) ? 0 : ((HSV_MAX_SAT < s) ? HSV_MAX_SAT : s);
+ val = (v < 0) ? 0 : ((HSV_MAX_VAL < v) ? HSV_MAX_VAL : v);
+ }
+
+ HSVColor(int16_t h, uint8_t s, uint8_t v)
+ {
+ hue = nomalize_hue(h);
+ sat = s;
+ val = v;
+ }
+
+ HSVColor(RGBColor& rgb)
+ {
+ rgb2hsv(rgb, *this);
+ }
+
+ /**
+ Default constructor
+ */
+ HSVColor() {}
+
+ // allow copy construction
+ HSVColor(const HSVColor& rhs)
+ {
+ hue = rhs.hue;
+ sat = rhs.sat;
+ val = rhs.val;
+ }
+
+ // allow assignment from one hsv struct to another
+ HSVColor& operator= (const HSVColor& rhs)
+ {
+ hue = rhs.hue;
+ sat = rhs.sat;
+ val = rhs.val;
+ return *this;
+ }
+
+ HSVColor& operator= (const RGBColor& rhs)
+ {
+ rgb2hsv(rhs, *this);
+ return *this;
+ }
+
+ operator RGBColor()
+ {
+ RGBColor rgb;
+ hsv2rgb(*this, rgb);
+ return rgb;
+ }
+
+};
+
+//----------------------------------------------------------------------------
#endif // end of COLORLIB_H