Skip to content

Commit

Permalink
Added tests for gp::Color arithmetic
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavye Mathur <[email protected]>
  • Loading branch information
BhavyeMathur committed Feb 16, 2024
1 parent 3621aeb commit c4a59e8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
Binary file modified binaries/lib-macos/libgoopylib.dylib
Binary file not shown.
40 changes: 36 additions & 4 deletions src/goopylib/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,51 @@ namespace gp {
// Color Operator Overloading
namespace gp {
Color Color::operator+(int value) const {
return {m_Red + value, m_Green + value, m_Blue + value, m_Alpha};
int red = m_Red + value;
int green = m_Green + value;
int blue = m_Blue + value;

red = red > 255 ? 255 : (red < 0 ? 0 : red);
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);

return {red, green, blue, m_Alpha};
}

Color Color::operator+(const Color& value) const {
return {m_Red + value.m_Red, m_Green + value.m_Green, m_Blue + value.m_Blue, m_Alpha};
int red = m_Red + value.m_Red;
int green = m_Green + value.m_Green;
int blue = m_Blue + value.m_Blue;

red = red > 255 ? 255 : (red < 0 ? 0 : red);
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);

return {red, green, blue, m_Alpha};
}

Color Color::operator-(int value) const {
return {m_Red - value, m_Green - value, m_Blue - value, m_Alpha};
int red = m_Red - value;
int green = m_Green - value;
int blue = m_Blue - value;

red = red > 255 ? 255 : (red < 0 ? 0 : red);
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);

return {red, green, blue, m_Alpha};
}

Color Color::operator-(const Color& value) const {
return {m_Red - value.m_Red, m_Green - value.m_Green, m_Blue - value.m_Blue, m_Alpha};
int red = m_Red - value.m_Red;
int green = m_Green - value.m_Green;
int blue = m_Blue - value.m_Blue;

red = red > 255 ? 255 : (red < 0 ? 0 : red);
green = green > 255 ? 255 : (green < 0 ? 0 : green);
blue = blue > 255 ? 255 : (blue < 0 ? 0 : blue);

return {red, green, blue, m_Alpha};
}

Color &Color::operator+=(int value) {
Expand Down
50 changes: 50 additions & 0 deletions tests/color/color_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ TEST(CoreColorTests, ColorAttributes) {

color.setAlpha(0.5);
EXPECT_EQ(color.getAlpha(), 0.5);

EXPECT_EQ(color.getRGBAf(), gp::RGBAf(0.6, 0.4, 0.2, 0.5));
EXPECT_EQ(color.toString(), "Color(153, 102, 51, 0.50)");
}

TEST(CoreColorTests, ColorAttributesError) {
Expand All @@ -142,3 +145,50 @@ TEST(CoreColorTests, ColorAttributesError) {
EXPECT_THROW(color.setAlpha(2), std::invalid_argument);
EXPECT_THROW(color.setAlpha(-0.5), std::invalid_argument);
}

TEST(CoreColorTests, ColorArithmetic) {
gp::Color color1{100, 100, 100};
gp::Color color2{5, 10, 15};

EXPECT_EQ((color1 + 50).toString(), "Color(150, 150, 150, 1.00)");
EXPECT_EQ((color1 - 50).toString(), "Color(50, 50, 50, 1.00)");

EXPECT_EQ((color1 + color2).toString(), "Color(105, 110, 115, 1.00)");
EXPECT_EQ((color1 - color2).toString(), "Color(95, 90, 85, 1.00)");

color1 += 50;
EXPECT_EQ(color1.toString(), "Color(150, 150, 150, 1.00)");

color1 -= 70;
EXPECT_EQ(color1.toString(), "Color(80, 80, 80, 1.00)");

color1 += color2;
EXPECT_EQ(color1.toString(), "Color(85, 90, 95, 1.00)");

color1 -= color2;
EXPECT_EQ(color1.toString(), "Color(80, 80, 80, 1.00)");
}

TEST(CoreColorTests, ColorArithmeticOverflow) {
gp::Color color1{250, 250, 250};
gp::Color color2{5, 10, 15};

EXPECT_EQ((color1 + 50).toString(), "Color(255, 255, 255, 1.00)");
EXPECT_EQ((color1 + color2).toString(), "Color(255, 255, 255, 1.00)");

color1 += 50;
EXPECT_EQ(color1.toString(), "Color(255, 255, 255, 1.00)");
EXPECT_EQ(color1.getRGBAf(), gp::RGBAf(1, 1, 1, 1));
}

TEST(CoreColorTests, ColorArithmeticUnderflow) {
gp::Color color1{5, 5, 5};
gp::Color color2{5, 10, 15};

EXPECT_EQ((color1 - 50).toString(), "Color(0, 0, 0, 1.00)");
EXPECT_EQ((color1 - color2).toString(), "Color(0, 0, 0, 1.00)");

color1 -= 50;
EXPECT_EQ(color1.toString(), "Color(0, 0, 0, 1.00)");
EXPECT_EQ(color1.getRGBAf(), gp::RGBAf(0, 0, 0, 1));
}

0 comments on commit c4a59e8

Please sign in to comment.