From 4f308f38dd7d00335c67c0a522ee0c859a757dfc Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 16 Aug 2018 23:18:42 +0200 Subject: [PATCH] [DEV] add multiple color format --- fluorine/Variant.cpp | 50 +++++++++++++++++++++++++++++++--------- fluorine/Variant.hpp | 35 ++++++++++++++++++++++++---- fluorine/variantType.cpp | 7 ++++-- fluorine/variantType.hpp | 3 ++- test/testAll.cpp | 41 ++++++++++++++++++++++++++++---- 5 files changed, 113 insertions(+), 23 deletions(-) diff --git a/fluorine/Variant.cpp b/fluorine/Variant.cpp index 3ed432a..0740f4f 100644 --- a/fluorine/Variant.cpp +++ b/fluorine/Variant.cpp @@ -40,8 +40,11 @@ etk::Stream& fluorine::operator <<(etk::Stream& _os, const fluorine::Variant& _o case fluorine::variantType::String: _os << _obj.getString(); break; - case fluorine::variantType::Color: - _os << _obj.getColor(); + case fluorine::variantType::ColorFloat: + _os << _obj.getColorFloat(); + break; + case fluorine::variantType::ColorInt: + _os << _obj.getColorInt(); break; case fluorine::variantType::Vec2: _os << _obj.getVec2(); @@ -114,8 +117,10 @@ fluorine::Variant fluorine::Variant::clone() const { return fluorine::Variant{getRawPointer()}; case fluorine::variantType::String: return fluorine::Variant{getString()}; - case fluorine::variantType::Color: - return fluorine::Variant{getColor()}; + case fluorine::variantType::ColorFloat: + return fluorine::Variant{getColorFloat()}; + case fluorine::variantType::ColorInt: + return fluorine::Variant{getColorInt()}; case fluorine::variantType::Vec2: return fluorine::Variant{getVec2()}; case fluorine::variantType::IVec2: @@ -141,8 +146,11 @@ void fluorine::Variant::clear() { case fluorine::variantType::String: ETK_DELETE(etk::String, m_dataUnion.m_string); break; - case fluorine::variantType::Color: - ETK_DELETE(etk::Color<>, m_dataUnion.m_color); + case fluorine::variantType::ColorFloat: + ETK_DELETE(etk::Color, m_dataUnion.m_colorFloat); + break; + case fluorine::variantType::ColorInt: + ETK_DELETE(etk::Color<>, m_dataUnion.m_colorInt); break; case fluorine::variantType::Vec2: ETK_DELETE(vec2, m_dataUnion.m_vec2); @@ -250,9 +258,13 @@ fluorine::Variant::Variant(const ivec3& _value): m_dataUnion.m_ivec3 = ETK_NEW(ivec3, _value); } +fluorine::Variant::Variant(const etk::Color& _value): + m_dataType(fluorine::variantType::ColorFloat) { + m_dataUnion.m_colorFloat = ETK_NEW(etk::Color, _value); +} fluorine::Variant::Variant(const etk::Color<>& _value): - m_dataType(fluorine::variantType::Color) { - m_dataUnion.m_color = ETK_NEW(etk::Color<>, _value); + m_dataType(fluorine::variantType::ColorInt) { + m_dataUnion.m_colorInt = ETK_NEW(etk::Color<>, _value); } fluorine::Variant::~Variant() { @@ -307,12 +319,28 @@ bool fluorine::Variant::isString() const { return m_dataType == fluorine::variantType::String; } -etk::Color<> fluorine::Variant::getColor() const { - return *(m_dataUnion.m_color); +etk::Color fluorine::Variant::getColorFloat() const { + if (m_dataType == fluorine::variantType::ColorFloat) { + return *(m_dataUnion.m_colorFloat); + } + return *(m_dataUnion.m_colorInt); +} +etk::Color<> fluorine::Variant::getColorInt() const { + if (m_dataType == fluorine::variantType::ColorFloat) { + return *(m_dataUnion.m_colorFloat); + } + return *(m_dataUnion.m_colorInt); } bool fluorine::Variant::isColor() const { - return m_dataType == fluorine::variantType::Color; + return m_dataType == fluorine::variantType::ColorFloat + || m_dataType == fluorine::variantType::ColorInt; +} +bool fluorine::Variant::isColorFloat() const { + return m_dataType == fluorine::variantType::ColorFloat; +} +bool fluorine::Variant::isColorInt() const { + return m_dataType == fluorine::variantType::ColorInt; } vec2 fluorine::Variant::getVec2() const { diff --git a/fluorine/Variant.hpp b/fluorine/Variant.hpp index 9cdff3e..21d095b 100644 --- a/fluorine/Variant.hpp +++ b/fluorine/Variant.hpp @@ -26,7 +26,8 @@ namespace fluorine { vec2* m_vec2; ivec3* m_ivec3; vec3* m_vec3; - etk::Color<>* m_color; + etk::Color* m_colorFloat; + etk::Color<>* m_colorInt; }; /** * @brief Basic main object of all json elements. @@ -120,6 +121,10 @@ namespace fluorine { * @brief Constuctor that generate a ivec3 value */ Variant(const ivec3& _value); + /** + * @brief Constuctor that generate a etk::Color value + */ + Variant(const etk::Color& _value); /** * @brief Constuctor that generate a etk::Color<> value */ @@ -200,12 +205,34 @@ namespace fluorine { * @brief Get the etk::Color Value. * @return requested value if compatible. */ - etk::Color<> getColor() const; + etk::Color getColorFloat() const; /** - * @brief check if the node is a fluorine::Color - * @return true if the node is a fluorine::Color + * @brief Get the etk::Color Value. + * @return requested value if compatible. + */ + etk::Color<> getColorInt() const; + /** + * @brief Get the etk::Color Value (generic etk type). + * @return requested value if compatible. + */ + etk::Color<> getColor() const { + return getColorInt(); + } + /** + * @brief check if the node is a fluorine::ColorFloat or fluorine::ColorInt + * @return true if the node is a fluorine::ColorFloat or fluorine::ColorInt */ bool isColor() const; + /** + * @brief check if the node is a fluorine::ColorFloat + * @return true if the node is a fluorine::ColorFloat + */ + bool isColorFloat() const; + /** + * @brief check if the node is a fluorine::ColorInt + * @return true if the node is a fluorine::ColorInt + */ + bool isColorInt() const; public: /** * @brief Get the vec2 Value. diff --git a/fluorine/variantType.cpp b/fluorine/variantType.cpp index 02b193f..d530012 100644 --- a/fluorine/variantType.cpp +++ b/fluorine/variantType.cpp @@ -35,8 +35,11 @@ etk::Stream& fluorine::operator <<(etk::Stream& _os, enum fluorine::variantType case fluorine::variantType::Boolean: _os << "fluorine::variantType::Boolean"; break; - case fluorine::variantType::Color: - _os << "fluorine::variantType::Color"; + case fluorine::variantType::ColorFloat: + _os << "fluorine::variantType::ColorFloat"; + break; + case fluorine::variantType::ColorInt: + _os << "fluorine::variantType::ColorInt"; break; case fluorine::variantType::Vec2: _os << "fluorine::variantType::Vec2"; diff --git a/fluorine/variantType.hpp b/fluorine/variantType.hpp index b1195d8..afcd3d1 100644 --- a/fluorine/variantType.hpp +++ b/fluorine/variantType.hpp @@ -20,7 +20,8 @@ namespace fluorine { RawPointer, //!< Void * pointer String, //!< String element Boolean, //!< Boolean element - Color, //!< Color RGBA float element + ColorFloat, //!< Color RGBA float element + ColorInt, //!< Color RGBA int element Vec2, //!< 2D vector Position (float) IVec2, //!< 2D vector Position (integer) Vec3, //!< 3D vector Position (float) diff --git a/test/testAll.cpp b/test/testAll.cpp index 68ed4fd..ac4febf 100644 --- a/test/testAll.cpp +++ b/test/testAll.cpp @@ -146,7 +146,25 @@ TEST(constructor, testString_2) { EXPECT_EQ(element.isNull(), false); } -TEST(constructor, testColor) { +TEST(constructor, testColorFloat) { + fluorine::Variant element(etk::Color(1.0,0.5,0.7,0.6)); + EXPECT_EQ(element.isUndefined(), false); + EXPECT_EQ(element.isBoolean(), false); + EXPECT_EQ(element.isInt(), false); + EXPECT_EQ(element.isUInt(), false); + EXPECT_EQ(element.isFloat(), false); + EXPECT_EQ(element.isRawPointer(), false); + EXPECT_EQ(element.isString(), false); + EXPECT_EQ(element.isColor(), true); + EXPECT_EQ(element.isVec2(), false); + EXPECT_EQ(element.isIVec2(), false); + EXPECT_EQ(element.isVec3(), false); + EXPECT_EQ(element.isIVec3(), false); + EXPECT_EQ(element.isNull(), false); + EXPECT_EQ(element.isColorFloat(), true); + EXPECT_EQ(element.isColorInt(), false); +} +TEST(constructor, testColorInt) { fluorine::Variant element(etk::Color<>(55,55,55,65)); EXPECT_EQ(element.isUndefined(), false); EXPECT_EQ(element.isBoolean(), false); @@ -161,6 +179,8 @@ TEST(constructor, testColor) { EXPECT_EQ(element.isVec3(), false); EXPECT_EQ(element.isIVec3(), false); EXPECT_EQ(element.isNull(), false); + EXPECT_EQ(element.isColorFloat(), false); + EXPECT_EQ(element.isColorInt(), true); } TEST(constructor, testVec2) { @@ -294,9 +314,13 @@ TEST(constructor, testString_2Value) { EXPECT_EQ(element.getString(), "HGFDSQ"); } -TEST(constructor, testColorValue) { +TEST(constructor, testColorValueFloat) { + fluorine::Variant element(etk::Color(1.0,0.5,0.7,0.6)); + EXPECT_EQ(element.getColorFloat(), etk::Color(1.0,0.5,0.7,0.6)); +} +TEST(constructor, testColorValueInt) { fluorine::Variant element(etk::Color<>(55,55,55,65)); - EXPECT_EQ(element.getColor(), etk::Color<>(55,55,55,65)); + EXPECT_EQ(element.getColorInt(), etk::Color<>(55,55,55,65)); } TEST(constructor, testVec2Value) { @@ -379,11 +403,18 @@ TEST(constructor, testString_2ValueOperator) { EXPECT_EQ(element.getString(), "HGFDSQ"); } -TEST(constructor, testColorValueOperator) { +TEST(constructor, testColorValueOperatorFloat) { + fluorine::Variant element; + EXPECT_EQ(element.isUndefined(), true); + element = etk::Color(1.0,0.5,0.7,0.6); + EXPECT_EQ(element.getColorFloat(), etk::Color(1.0,0.5,0.7,0.6)); +} + +TEST(constructor, testColorValueOperatorInt) { fluorine::Variant element; EXPECT_EQ(element.isUndefined(), true); element = etk::Color<>(55,55,55,65); - EXPECT_EQ(element.getColor(), etk::Color<>(55,55,55,65)); + EXPECT_EQ(element.getColorInt(), etk::Color<>(55,55,55,65)); } TEST(constructor, testVec2ValueOperator) {