From 3679d96ad7f50b4c85f378af53e4342959a9e651 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Thu, 14 Nov 2024 17:07:26 +0100 Subject: [PATCH 1/2] [Type] Consistent string to color conversion --- .../Type/src/sofa/type/RGBAColor.cpp | 45 +++++++++++---- Sofa/framework/Type/src/sofa/type/RGBAColor.h | 56 +++++++++++++++---- Sofa/framework/Type/test/Color_test.cpp | 13 +++++ 3 files changed, 91 insertions(+), 23 deletions(-) diff --git a/Sofa/framework/Type/src/sofa/type/RGBAColor.cpp b/Sofa/framework/Type/src/sofa/type/RGBAColor.cpp index 611ed9d0687..79c4aebe25a 100644 --- a/Sofa/framework/Type/src/sofa/type/RGBAColor.cpp +++ b/Sofa/framework/Type/src/sofa/type/RGBAColor.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -176,6 +177,30 @@ static std::istream& trimInitialSpaces(std::istream& in) return in; } +const std::map stringToColorMap { + {"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) { @@ -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) ; diff --git a/Sofa/framework/Type/src/sofa/type/RGBAColor.h b/Sofa/framework/Type/src/sofa/type/RGBAColor.h index 13e2bbbf70a..44fefc4eacc 100644 --- a/Sofa/framework/Type/src/sofa/type/RGBAColor.h +++ b/Sofa/framework/Type/src/sofa/type/RGBAColor.h @@ -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); @@ -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; } @@ -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 diff --git a/Sofa/framework/Type/test/Color_test.cpp b/Sofa/framework/Type/test/Color_test.cpp index fb30479c4af..1f4d37dc5b1 100644 --- a/Sofa/framework/Type/test/Color_test.cpp +++ b/Sofa/framework/Type/test/Color_test.cpp @@ -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) ) ; From 0a7d5d41a6cccdcac9f75bfe9de26f1a8f9521e1 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Thu, 14 Nov 2024 17:08:14 +0100 Subject: [PATCH 2/2] rename --- Sofa/framework/Type/test/CMakeLists.txt | 2 +- Sofa/framework/Type/test/{Color_test.cpp => RGBAColor_test.cpp} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename Sofa/framework/Type/test/{Color_test.cpp => RGBAColor_test.cpp} (100%) diff --git a/Sofa/framework/Type/test/CMakeLists.txt b/Sofa/framework/Type/test/CMakeLists.txt index 7211d45a336..5aae429ac84 100644 --- a/Sofa/framework/Type/test/CMakeLists.txt +++ b/Sofa/framework/Type/test/CMakeLists.txt @@ -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 diff --git a/Sofa/framework/Type/test/Color_test.cpp b/Sofa/framework/Type/test/RGBAColor_test.cpp similarity index 100% rename from Sofa/framework/Type/test/Color_test.cpp rename to Sofa/framework/Type/test/RGBAColor_test.cpp