TLIGHT_PRODUCTS / WS281X
Revision:
0:dff187a80020
Child:
1:0288c920499c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ColorLib.h	Tue Jul 26 18:09:24 2016 +0000
@@ -0,0 +1,122 @@
+/* ColorLib.h
+ * mbed Microcontroller Library
+ * Copyright (c) 2016 muetch, t.kuroki, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+
+#ifndef COLORLIB_H
+#define COLORLIB_H
+
+#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)))
+
+//----------------------------------------------------------------------------
+/**
+    RGB Color
+*/
+struct RGBColor
+{
+    uint8_t red;
+    uint8_t green;
+    uint8_t blue;
+
+    /**
+        Constructor with rgb initializing
+    
+        @param r - the red byte
+        @param g - the green byte
+        @param b - the blue byte
+    */
+    RGBColor(uint8_t r, uint8_t g, uint8_t b)
+    {
+        red   = r;
+        green = g;
+        blue  = b;
+    }
+
+    RGBColor(uint32_t rgb)
+    {
+        red   = GetRValue(rgb);
+        green = GetGValue(rgb);
+        blue  = GetBValue(rgb);
+    }
+
+    RGBColor(int rgb)
+    {
+        red   = GetRValue(rgb);
+        green = GetGValue(rgb);
+        blue  = GetBValue(rgb);
+    }
+
+    /**
+        Default constructor
+    */
+    RGBColor() {}
+
+    // allow copy construction
+    inline RGBColor(const RGBColor& rhs)
+    {
+        red   = rhs.red;
+        green = rhs.green;
+        blue  = rhs.blue;
+    }
+
+    // allow assignment from one RGB struct to another
+    inline RGBColor& operator= (const RGBColor& rhs)
+    {
+        red   = rhs.red;
+        green = rhs.green;
+        blue  = rhs.blue;
+        return *this;
+    }
+
+    inline RGBColor& operator= (const uint32_t rgb)
+    {
+        red   = GetRValue(rgb);
+        green = GetGValue(rgb);
+        blue  = GetBValue(rgb);
+        return *this;
+    }
+
+    inline RGBColor& operator= (const int rgb)
+    {
+        red   = GetRValue(rgb);
+        green = GetGValue(rgb);
+        blue  = GetBValue(rgb);
+        return *this;
+    }
+
+    operator uint32_t()
+    {
+        return COLORREF(red, green, blue);
+    }
+    
+    operator int()
+    {
+        return COLORREF(red, green, blue);
+    }
+};
+
+//----------------------------------------------------------------------------
+#endif      // end of COLORLIB_H