Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle yet another color format 🎨 #266

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,18 @@ commonItems::Color commonItems::Color::Factory::getColor(std::istream& theStream
// This is a double list.
auto doubleStream = std::stringstream(questionableList);
auto rgb = getDoubles(doubleStream);
if (rgb.size() == 3) // This is not HSV, this is RGB doubles. Multiply by 255 and round to get normal rgb.
if (rgb.size() == 3)
{
// This is not HSV, this is RGB doubles. Just convert to ints to get normal RGB.
if (rgb[0] > 1 || rgb[1] > 1 || rgb[2] > 1)
return Color(std::array{static_cast<int>(std::round(rgb[0])), static_cast<int>(std::round(rgb[1])), static_cast<int>(std::round(rgb[2]))});

// If all RGB values are in the range 0-1, multiply by 255 and round to get normal RGB.
return Color(std::array{static_cast<int>(std::round(rgb[0] * 255.0)),
static_cast<int>(std::round(rgb[1] * 255.0)),
static_cast<int>(std::round(rgb[2] * 255.0))});
if (rgb.size() == 4) // this is a RGBA double situation. We shouldn't touch alpha.
}
if (rgb.size() == 4) // this is an RGBA double situation. We shouldn't touch alpha.
return Color(std::array{static_cast<int>(std::round(rgb[0] * 255.0)),
static_cast<int>(std::round(rgb[1] * 255.0)),
static_cast<int>(std::round(rgb[2] * 255.0))},
Expand Down
17 changes: 17 additions & 0 deletions tests/ColorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,23 @@ TEST(Color_Tests, ColorCanBeInitializedFromStream)
ASSERT_NEAR(0.5f, v, 0.01);
}

TEST(Color_Tests, ColorIsCorrectlyConstructedFromRGBFloats)
{
std::stringstream input;
input << "= { 49.0 35.0 58.0 }";
const auto testColor = commonItems::Color::Factory{}.getColor(input);

auto [r, g, b] = testColor.getRgbComponents();
ASSERT_EQ(49, r);
ASSERT_EQ(35, g);
ASSERT_EQ(58, b);

auto [h, s, v] = testColor.getHsvComponents();
ASSERT_NEAR(0.7681f, h, 0.01);
ASSERT_NEAR(0.39655f, s, 0.01);
ASSERT_NEAR(0.22745f, v, 0.01);
}

TEST(Color_Tests, ColorRGBDoublesCanBeInitializedFromStream) // Yes, this is a thing.
{
std::stringstream input;
Expand Down
Loading