Browse Source

bracket operator for color

Kajetan Johannes Hammerle 3 years ago
parent
commit
43b70c7d12
2 changed files with 20 additions and 11 deletions
  1. 10 10
      tests/ColorTests.cpp
  2. 10 1
      utils/Color.h

+ 10 - 10
tests/ColorTests.cpp

@@ -6,23 +6,23 @@ const float eps = 0.0001f;
 
 static void testColor1(Test& test) {
     Color1 c = Color1(36);
-    test.checkEqual(static_cast<ColorChannel>(36), c.data[0], "build 1 | 1");
+    test.checkEqual(static_cast<ColorChannel>(36), c[0], "build 1 | 1");
     test.checkFloat(36.0f / 255.0f, c.asFloat(0), eps, "build 1 | 2");
 }
 
 static void testColor2(Test& test) {
     Color2 c = Color2(36, 100);
-    test.checkEqual(static_cast<ColorChannel>(36), c.data[0], "build 2 | 1");
-    test.checkEqual(static_cast<ColorChannel>(100), c.data[1], "build 2 | 2");
+    test.checkEqual(static_cast<ColorChannel>(36), c[0], "build 2 | 1");
+    test.checkEqual(static_cast<ColorChannel>(100), c[1], "build 2 | 2");
     test.checkFloat(36.0f / 255.0f, c.asFloat(0), eps, "build 2 | 3");
     test.checkFloat(100.0f / 255.0f, c.asFloat(1), eps, "build 2 | 4");
 }
 
 static void testColor3(Test& test) {
     Color3 c = Color3(36, 100, 200);
-    test.checkEqual(static_cast<ColorChannel>(36), c.data[0], "build 3 | 1");
-    test.checkEqual(static_cast<ColorChannel>(100), c.data[1], "build 3 | 2");
-    test.checkEqual(static_cast<ColorChannel>(200), c.data[2], "build 3 | 3");
+    test.checkEqual(static_cast<ColorChannel>(36), c[0], "build 3 | 1");
+    test.checkEqual(static_cast<ColorChannel>(100), c[1], "build 3 | 2");
+    test.checkEqual(static_cast<ColorChannel>(200), c[2], "build 3 | 3");
     test.checkFloat(36.0f / 255.0f, c.asFloat(0), eps, "build 3 | 4");
     test.checkFloat(100.0f / 255.0f, c.asFloat(1), eps, "build 3 | 5");
     test.checkFloat(200.0f / 255.0f, c.asFloat(2), eps, "build 3 | 6");
@@ -30,10 +30,10 @@ static void testColor3(Test& test) {
 
 static void testColor4(Test& test) {
     Color4 c = Color4(36, 100, 200, 142);
-    test.checkEqual(static_cast<ColorChannel>(36), c.data[0], "build 4 | 1");
-    test.checkEqual(static_cast<ColorChannel>(100), c.data[1], "build 4 | 2");
-    test.checkEqual(static_cast<ColorChannel>(200), c.data[2], "build 4 | 3");
-    test.checkEqual(static_cast<ColorChannel>(142), c.data[3], "build 4 | 4");
+    test.checkEqual(static_cast<ColorChannel>(36), c[0], "build 4 | 1");
+    test.checkEqual(static_cast<ColorChannel>(100), c[1], "build 4 | 2");
+    test.checkEqual(static_cast<ColorChannel>(200), c[2], "build 4 | 3");
+    test.checkEqual(static_cast<ColorChannel>(142), c[3], "build 4 | 4");
     test.checkFloat(36.0f / 255.0f, c.asFloat(0), eps, "build 4 | 5");
     test.checkFloat(100.0f / 255.0f, c.asFloat(1), eps, "build 4 | 6");
     test.checkFloat(200.0f / 255.0f, c.asFloat(2), eps, "build 4 | 7");

+ 10 - 1
utils/Color.h

@@ -4,11 +4,12 @@
 typedef unsigned char ColorChannel;
 
 template<int N>
-struct Color {
+class Color {
     static_assert(N <= 4, "a color can have at most 4 channels");
 
     ColorChannel data[N];
 
+public:
     Color() = default;
 
     template<typename... Args>
@@ -24,6 +25,14 @@ struct Color {
     float asFloat(int index) const {
         return data[index] * (1.0f / 255.0f);
     }
+
+    ColorChannel& operator[](int index) {
+        return data[index];
+    }
+    
+    const ColorChannel& operator[](int index) const {
+        return data[index];
+    }
 };
 
 typedef Color<4> Color4;