From c2c627ed4dd3cb13301e044b8dc5d114b9d19de4 Mon Sep 17 00:00:00 2001 From: Hans-Christian Ebke Date: Mon, 9 Nov 2015 18:51:00 +0100 Subject: [PATCH] C++11: Added _htmlColor literal operator. Allows inline specification of colors in HTML syntax. --- src/OpenMesh/Core/Geometry/VectorT.hh | 20 ++++++++++++++++++++ src/Unittests/unittests_vector_type.cc | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/OpenMesh/Core/Geometry/VectorT.hh b/src/OpenMesh/Core/Geometry/VectorT.hh index 9c3db706..9dad0795 100644 --- a/src/OpenMesh/Core/Geometry/VectorT.hh +++ b/src/OpenMesh/Core/Geometry/VectorT.hh @@ -412,6 +412,26 @@ typedef VectorT Vec6d; //============================================================================= } // namespace OpenMesh //============================================================================= + + +#ifdef CPP11_ENABLED +/** + * Literal operator for inline specification of colors in HTML syntax. + * + * Example: + * \code{.cpp} + * OpenMesh::Vec4f light_blue = 0x1FCFFFFF_htmlColor; + * \endcode + */ +constexpr OpenMesh::Vec4f operator"" _htmlColor(unsigned long long raw_color) { + return OpenMesh::Vec4f( + ((raw_color >> 24) & 0xFF) / 255.0f, + ((raw_color >> 16) & 0xFF) / 255.0f, + ((raw_color >> 8) & 0xFF) / 255.0f, + ((raw_color >> 0) & 0xFF) / 255.0f); +} +#endif + #endif // OPENMESH_VECTOR_HH defined //============================================================================= #endif // DOXYGEN diff --git a/src/Unittests/unittests_vector_type.cc b/src/Unittests/unittests_vector_type.cc index 0d157766..58172537 100644 --- a/src/Unittests/unittests_vector_type.cc +++ b/src/Unittests/unittests_vector_type.cc @@ -111,6 +111,18 @@ TEST_F(OpenMeshVectorTest, cpp11_constructors) { EXPECT_EQ(1.23, vec4d[2]); EXPECT_EQ(1.23, vec4d[3]); } + +TEST_F(OpenMeshVectorTest, cpp11_htmlColorLiteral) { + const OpenMesh::Vec4f light_blue = 0x1FCFFFFF_htmlColor; + EXPECT_LE((OpenMesh::Vec4f(0.1215686274f, 0.8117647058f, 1.0f, 1.0f) + - light_blue).sqrnorm(), 1e-10); + + const auto light_blue_2 = 0x1FCFFFFF_htmlColor; + // Check whether auto type deduction works as expected. + static_assert(std::is_same + ::value, "Bad type deduced from _htmlColor literal."); + EXPECT_EQ(light_blue, light_blue_2); +} #endif }