diff --git a/binaries/lib-macos/libgoopylib.dylib b/binaries/lib-macos/libgoopylib.dylib index deea1049..d443977f 100755 Binary files a/binaries/lib-macos/libgoopylib.dylib and b/binaries/lib-macos/libgoopylib.dylib differ diff --git a/src/goopylib/color/Color.cpp b/src/goopylib/color/Color.cpp index 07099668..c751ce6b 100644 --- a/src/goopylib/color/Color.cpp +++ b/src/goopylib/color/Color.cpp @@ -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) { diff --git a/tests/color/color_tests.cpp b/tests/color/color_tests.cpp index adf9fa11..64a830c5 100644 --- a/tests/color/color_tests.cpp +++ b/tests/color/color_tests.cpp @@ -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) { @@ -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)); +}