From d978e30ae19fb5769a45115e50053848f0fff89a Mon Sep 17 00:00:00 2001
From: Jens Nolte <jens@nightmarestudio.de>
Date: Sat, 25 Jan 2020 00:09:18 +0100
Subject: [PATCH] Add RGB type

---
 main/color.cpp | 16 +++++-----------
 main/qthing.h  | 39 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/main/color.cpp b/main/color.cpp
index 9fc7c08..f2b2a16 100644
--- a/main/color.cpp
+++ b/main/color.cpp
@@ -3,11 +3,9 @@
 
 namespace qthing {
   // TODO: should return RGB once that can be casted to RGBW
-  RGBW hue(const float hue, const float value) {
-      RGBW color;
-
+  RGB hue(const float hue, const float value) {
       if (value <= 0) {
-          return color;
+          return RGB();
       }
 
       auto h = fmod(hue, 1);
@@ -18,19 +16,15 @@ namespace qthing {
       h = h * 3;
       auto v = value > 1 ? 1 : value;
       if (h < 1) {
-          color.r = (1 - h) * v;
-          color.g = h * v;
+          return RGB((1 - h) * v, h * v, 0);
       }
       else if (h < 2) {
           h -= 1;
-          color.g = (1 - h) * v;
-          color.b = h * v;
+          return RGB(0, (1 - h) * v, h * v);
       }
       else {
           h -= 2;
-          color.b = (1 - h) * v;
-          color.r = h * v;
+          return RGB(h * v, 0, (1 - h) * v);
       }
-      return color;
   }
 }
diff --git a/main/qthing.h b/main/qthing.h
index 4ae3b57..755ef20 100644
--- a/main/qthing.h
+++ b/main/qthing.h
@@ -165,6 +165,43 @@ namespace qthing {
         }
     };
 
+    struct RGB {
+        float r;
+        float g;
+        float b;
+        RGB() : r(0), g(0), b(0) {
+        }
+        RGB(float r, float g, float b) : r(r), g(g), b(b) {
+        }
+        RGB operator+(const RGB& other) {
+            RGB result = *this;
+            result.r += other.r;
+            result.g += other.g;
+            result.b += other.b;
+            return result;
+        }
+        RGB operator*(const float scale) {
+            RGB result = *this;
+            result.r *= scale;
+            result.g *= scale;
+            result.b *= scale;
+            return result;
+        }
+        void operator*=(const float scale) {
+            this->r *= scale;
+            this->g *= scale;
+            this->b *= scale;
+        }
+
+        operator RGBW() {
+            return RGBW(this->r, this->g, this->b, 0);
+        }
+
+        operator led_color_t() {
+            return led_color_t(this->r * 255, this->g * 255, this->b * 255, 0);
+        }
+    };
+
     struct GammaCorrection {
         float gamma_r;
         float gamma_g;
@@ -190,7 +227,7 @@ namespace qthing {
         }
     };
 
-    RGBW hue(const float hue, const float value = 1);
+    RGB hue(const float hue, const float value = 1);
 
     typedef std::function<led_color_t(uint8_t, uint16_t, led_color_t)> led_color_handler_t;
 
-- 
GitLab