Skip to content

Commit

Permalink
Handle yet another color format 🎨 (#266)
Browse files Browse the repository at this point in the history
* Handle yet another color format

* Fix index
  • Loading branch information
IhateTrains authored Sep 17, 2024
1 parent 191608a commit d1aca58
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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

0 comments on commit d1aca58

Please sign in to comment.