Skip to content

Commit

Permalink
✨ (coreled): RGB, add overload opertor +, +=
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz authored and ladislas committed Feb 21, 2022
1 parent 4d7fbfe commit 8412ce6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
16 changes: 16 additions & 0 deletions drivers/CoreLED/include/RGB.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef _LEKA_OS_DRIVER_CORE_LED_RGB_H_
#define _LEKA_OS_DRIVER_CORE_LED_RGB_H_

#include <algorithm>
#include <cstdint>

namespace leka {
Expand All @@ -16,6 +17,21 @@ struct RGB {

auto operator<=>(RGB const &rhs) const -> bool = default;

auto operator+=(RGB const &rhs) -> RGB
{
red = std::min(red + rhs.red, 255);
green = std::min(green + rhs.green, 255);
blue = std::min(blue + rhs.blue, 255);
return *this;
}

auto operator+(const RGB &rhs) const -> RGB
{
auto color = *this;
color += rhs;
return color;
}

static const RGB white;
static const RGB black;
static const RGB pure_red;
Expand Down
73 changes: 73 additions & 0 deletions drivers/CoreLED/tests/RGB_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,76 @@ TEST(RGBTest, different)
EXPECT_TRUE((black != RGB {0xFF, 0xFF, 0xFF}));
EXPECT_FALSE((black == RGB {0xFF, 0xFF, 0xFF}));
}

TEST(RGBTest, addTwoColor)
{
RGB color1 = RGB::pure_red;
color1 += RGB::pure_blue;

RGB color2 = RGB::pure_red;
color2 += RGB::pure_green;

RGB color3 = RGB::pure_green;
color3 += RGB::pure_red;

RGB color4 = RGB::pure_green;
color4 += RGB::pure_blue;

RGB color5 = RGB::pure_blue;
color5 += RGB::pure_red;

RGB color6 = RGB::pure_blue;
color6 += RGB::pure_green;

RGB color7 = RGB::pure_blue;
color7 += RGB::pure_green;
color7 += RGB::pure_red;

EXPECT_TRUE(color1 == RGB::magenta);
EXPECT_TRUE(color2 == RGB::yellow);
EXPECT_TRUE(color3 == RGB::yellow);
EXPECT_TRUE(color4 == RGB::cyan);
EXPECT_TRUE(color5 == RGB::magenta);
EXPECT_TRUE(color6 == RGB::cyan);
EXPECT_TRUE(color7 == RGB::white);
}

TEST(RGBTest, addTwoColorOverTheBound1)
{
RGB color = RGB::pure_red;
color += RGB::pure_red;

EXPECT_TRUE(color == RGB::pure_red);
}

TEST(RGBTest, addTwoColorOverTheBound2)
{
RGB color = RGB::white;
color += RGB {10, 7, 120};

EXPECT_TRUE(color == RGB::white);
}

TEST(RGBTest, sumTwoColor)
{
auto magenta1 = RGB::pure_red + RGB::pure_blue;
EXPECT_TRUE(magenta1 == RGB::magenta);

RGB magenta2 = RGB::pure_blue + RGB::pure_red;
EXPECT_TRUE(magenta2 == RGB::magenta);

auto yellow1 = RGB::pure_red + RGB::pure_green;
EXPECT_TRUE(yellow1 == RGB::yellow);

auto yellow2 = RGB::pure_green + RGB::pure_red;
EXPECT_TRUE(yellow2 == RGB::yellow);

auto cyan1 = RGB::pure_green + RGB::pure_blue;
EXPECT_TRUE(cyan1 == RGB::cyan);

auto cyan2 = RGB::pure_blue + RGB::pure_green;
EXPECT_TRUE(cyan2 == RGB::cyan);

auto white = RGB::pure_red + RGB::pure_green + RGB::pure_blue;
EXPECT_TRUE(white == RGB::white);
}

0 comments on commit 8412ce6

Please sign in to comment.