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

[Type] Consistent string to color conversion #5121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 33 additions & 12 deletions Sofa/framework/Type/src/sofa/type/RGBAColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <sstream>
#include <locale>
#include <map>

#include <sofa/type/fixed_array.h>
#include <sofa/type/fixed_array_algorithms.h>
Expand Down Expand Up @@ -176,6 +177,30 @@ static std::istream& trimInitialSpaces(std::istream& in)
return in;
}

const std::map<std::string, RGBAColor> stringToColorMap {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't constexpr be used on a map ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it is not possible currently, but serge-sans-paille did it ! https://github.com/serge-sans-paille/frozen

{"white", g_white},
{"black", g_black},
{"red", g_red},
{"green", g_green},
{"blue", g_blue},
{"cyan", g_cyan},
{"magenta", g_magenta},
{"yellow", g_yellow},
{"gray", g_gray},
{"darkgray", g_darkgray},
{"lightgray", g_lightgray},
{"orange", g_orange},
{"purple", g_purple},
{"pink", g_pink},
{"brown", g_brown},
{"lime", g_lime},
{"teal", g_teal},
{"navy", g_navy},
{"olive", g_olive},
{"maroon", g_maroon},
{"silver", g_silver},
{"gold", g_gold}
};

SOFA_TYPE_API std::istream& operator>>(std::istream& i, RGBAColor& t)
{
Expand Down Expand Up @@ -230,18 +255,14 @@ SOFA_TYPE_API std::istream& operator>>(std::istream& i, RGBAColor& t)

/// if end of line is returned before encountering ' ' or 7... is it fine
/// so we can clear the failure bitset.

/// Compare the resulting string with supported colors.
/// If you add more colors... please also add them in the test file.
if (str == "white") { r = 1.0f; g = 1.0f; b = 1.0f; }
else if (str == "black") { r = 0.0f; g = 0.0f; b = 0.0f; }
else if (str == "red") { r = 1.0f; g = 0.0f; b = 0.0f; }
else if (str == "green") { r = 0.0f; g = 1.0f; b = 0.0f; }
else if (str == "blue") { r = 0.0f; g = 0.0f; b = 1.0f; }
else if (str == "cyan") { r = 0.0f; g = 1.0f; b = 1.0f; }
else if (str == "magenta") { r = 1.0f; g = 0.0f; b = 1.0f; }
else if (str == "yellow") { r = 1.0f; g = 1.0f; b = 0.0f; }
else if (str == "gray") { r = 0.5f; g = 0.5f; b = 0.5f; }
if (const auto it = stringToColorMap.find(str);
it != stringToColorMap.end())
{
r = it->second.r();
g = it->second.g();
b = it->second.b();
a = it->second.a();
}
else {
/// If we cannot parse the field we returns that with the fail bit.
i.setstate(std::ios_base::failbit) ;
Expand Down
56 changes: 45 additions & 11 deletions Sofa/framework/Type/src/sofa/type/RGBAColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ class SOFA_TYPE_API RGBAColor
constexpr static const RGBAColor& gray();
constexpr static const RGBAColor& darkgray();
constexpr static const RGBAColor& lightgray();
constexpr static const RGBAColor& orange();
constexpr static const RGBAColor& purple();
constexpr static const RGBAColor& pink();
constexpr static const RGBAColor& brown();
constexpr static const RGBAColor& lime();
constexpr static const RGBAColor& teal();
constexpr static const RGBAColor& navy();
constexpr static const RGBAColor& olive();
constexpr static const RGBAColor& maroon();
constexpr static const RGBAColor& silver();
constexpr static const RGBAColor& gold();

/// @brief enlight a color by a given factor.
static RGBAColor lighten(const RGBAColor& in, const SReal factor);
Expand Down Expand Up @@ -226,17 +237,28 @@ constexpr RGBAColor operator/(const RGBAColor& l, const float div)
}


constexpr RGBAColor g_white {1.0f,1.0f,1.0f,1.0f};
constexpr RGBAColor g_black {0.0f,0.0f,0.0f,1.0f};
constexpr RGBAColor g_red {1.0f,0.0f,0.0f,1.0f};
constexpr RGBAColor g_green {0.0f,1.0f,0.0f,1.0f};
constexpr RGBAColor g_blue {0.0f,0.0f,1.0f,1.0f};
constexpr RGBAColor g_cyan {0.0f,1.0f,1.0f,1.0f};
constexpr RGBAColor g_magenta {1.0f,0.0f,1.0f,1.0f};
constexpr RGBAColor g_yellow {1.0f,1.0f,0.0f,1.0f};
constexpr RGBAColor g_gray {0.5f,0.5f,0.5f,1.0f};
constexpr RGBAColor g_darkgray {0.25f,0.25f,0.25f,1.0f};
constexpr RGBAColor g_lightgray {0.75f,0.75f,0.75f,1.0f};
constexpr RGBAColor g_white {1.0f, 1.0f, 1.0f, 1.0f};
constexpr RGBAColor g_black {0.0f, 0.0f, 0.0f, 1.0f};
constexpr RGBAColor g_red {1.0f, 0.0f, 0.0f, 1.0f};
constexpr RGBAColor g_green {0.0f, 1.0f, 0.0f, 1.0f};
constexpr RGBAColor g_blue {0.0f, 0.0f, 1.0f, 1.0f};
constexpr RGBAColor g_cyan {0.0f, 1.0f, 1.0f, 1.0f};
constexpr RGBAColor g_magenta {1.0f, 0.0f, 1.0f, 1.0f};
constexpr RGBAColor g_yellow {1.0f, 1.0f, 0.0f, 1.0f};
constexpr RGBAColor g_gray {0.5f, 0.5f, 0.5f, 1.0f};
constexpr RGBAColor g_darkgray {0.25f, 0.25f,0.25f, 1.0f};
constexpr RGBAColor g_lightgray {0.75f, 0.75f,0.75f, 1.0f};
constexpr RGBAColor g_orange { 1.0f, 0.5f, 0.0f, 1.0f };
constexpr RGBAColor g_purple { 0.5f, 0.0f, 0.5f, 1.0f };
constexpr RGBAColor g_pink { 1.0f, 0.0f, 0.5f, 1.0f };
constexpr RGBAColor g_brown { 0.6f, 0.3f, 0.0f, 1.0f };
constexpr RGBAColor g_lime { 0.5f, 1.0f, 0.0f, 1.0f };
constexpr RGBAColor g_teal { 0.0f, 0.5f, 0.5f, 1.0f };
constexpr RGBAColor g_navy { 0.0f, 0.0f, 0.5f, 1.0f };
constexpr RGBAColor g_olive { 0.5f, 0.5f, 0.0f, 1.0f };
constexpr RGBAColor g_maroon { 0.5f, 0.0f, 0.0f, 1.0f };
constexpr RGBAColor g_silver { 0.75f, 0.75f, 0.75f, 1.0f };
constexpr RGBAColor g_gold { 1.0f, 0.84f, 0.0f, 1.0f };

constexpr const RGBAColor& RGBAColor::white() { return g_white; }
constexpr const RGBAColor& RGBAColor::black() { return g_black; }
Expand All @@ -249,5 +271,17 @@ constexpr const RGBAColor& RGBAColor::yellow() { return g_yellow; }
constexpr const RGBAColor& RGBAColor::gray() { return g_gray; }
constexpr const RGBAColor& RGBAColor::darkgray() { return g_darkgray; }
constexpr const RGBAColor& RGBAColor::lightgray(){ return g_lightgray; }
constexpr const RGBAColor& RGBAColor::orange() { return g_orange; }
constexpr const RGBAColor& RGBAColor::purple() { return g_purple; }
constexpr const RGBAColor& RGBAColor::pink() { return g_pink ; }
constexpr const RGBAColor& RGBAColor::brown() { return g_brown ; }
constexpr const RGBAColor& RGBAColor::lime() { return g_lime ; }
constexpr const RGBAColor& RGBAColor::teal() { return g_teal ; }
constexpr const RGBAColor& RGBAColor::navy() { return g_navy ; }
constexpr const RGBAColor& RGBAColor::olive() { return g_olive ; }
constexpr const RGBAColor& RGBAColor::maroon() { return g_maroon; }
constexpr const RGBAColor& RGBAColor::silver() { return g_silver; }
constexpr const RGBAColor& RGBAColor::gold() { return g_gold ; }


} // namespace sofa::type
2 changes: 1 addition & 1 deletion Sofa/framework/Type/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ cmake_minimum_required(VERSION 3.22)
project(Sofa.Type_test)

set(SOURCE_FILES
Color_test.cpp
MatSym_test.cpp
MatTypes_test.cpp
Material_test.cpp
Quater_test.cpp
RGBAColor_test.cpp
SVector_test.cpp
VecTypes_test.cpp
fixed_array_test.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ void Color_Test::checkCreateFromString()
EXPECT_EQ( RGBAColor::fromString("magenta"), RGBAColor(1.0,0.0,1.0,1.0) ) ;
EXPECT_EQ( RGBAColor::fromString("yellow"), RGBAColor(1.0,1.0,0.0,1.0) ) ;
EXPECT_EQ( RGBAColor::fromString("gray"), RGBAColor(0.5,0.5,0.5,1.0) ) ;
EXPECT_EQ( RGBAColor::fromString("darkgray"), RGBAColor(0.25f, 0.25f,0.25f, 1.0f ) );
EXPECT_EQ( RGBAColor::fromString("lightgray"), RGBAColor(0.75f, 0.75f,0.75f, 1.0f ) );
EXPECT_EQ( RGBAColor::fromString("orange"), RGBAColor(1.0f, 0.5f, 0.0f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("purple"), RGBAColor(0.5f, 0.0f, 0.5f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("pink"), RGBAColor(1.0f, 0.0f, 0.5f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("brown"), RGBAColor(0.6f, 0.3f, 0.0f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("lime"), RGBAColor(0.5f, 1.0f, 0.0f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("teal"), RGBAColor(0.0f, 0.5f, 0.5f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("navy"), RGBAColor(0.0f, 0.0f, 0.5f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("olive"), RGBAColor(0.5f, 0.5f, 0.0f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("maroon"), RGBAColor(0.5f, 0.0f, 0.0f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("silver"), RGBAColor(0.75f, 0.75f, 0.75f, 1.0f ));
EXPECT_EQ( RGBAColor::fromString("gold"), RGBAColor(1.0f, 0.84f, 0.0f, 1.0f ));

RGBAColor color;
EXPECT_TRUE( RGBAColor::read("white", color) ) ;
Expand Down
Loading