From: Paul Wisbey Date: Mon, 20 Feb 2017 15:54:48 +0000 (+0000) Subject: Provided color-conversion helpers X-Git-Tag: dali_1.2.28~9^2 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=commitdiff_plain;h=e2916ba0e2e9c92f2254d19f0890589cf77b2221 Provided color-conversion helpers Change-Id: I95e2b4c338e32c012d05f8a63237a419283222c5 --- diff --git a/automated-tests/src/dali-toolkit-internal/CMakeLists.txt b/automated-tests/src/dali-toolkit-internal/CMakeLists.txt index f07653b..023d4bc 100755 --- a/automated-tests/src/dali-toolkit-internal/CMakeLists.txt +++ b/automated-tests/src/dali-toolkit-internal/CMakeLists.txt @@ -24,6 +24,7 @@ SET(TC_SOURCES utc-Dali-DebugRendering.cpp utc-Dali-ItemView-internal.cpp utc-Dali-PropertyHelper.cpp + utc-Dali-ColorConversion.cpp ) # Append list of test harness files (Won't get parsed for test cases) diff --git a/automated-tests/src/dali-toolkit-internal/utc-Dali-ColorConversion.cpp b/automated-tests/src/dali-toolkit-internal/utc-Dali-ColorConversion.cpp new file mode 100644 index 0000000..14cef1c --- /dev/null +++ b/automated-tests/src/dali-toolkit-internal/utc-Dali-ColorConversion.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +using namespace Dali; +using namespace Dali::Toolkit; + +void dali_color_conversion_startup(void) +{ + test_return_value = TET_UNDEF; +} + +void dali_color_conversion_cleanup(void) +{ + test_return_value = TET_PASS; +} + +int UtcDaliPropertyHelperConvertHtmlStringToColor(void) +{ + tet_infoline( "Test to check whether An HTML style hex string can be converted" ); + + const std::string stringColor( "#FF0000" ); + + Vector4 result; + DALI_TEST_CHECK( Toolkit::Internal::ConvertStringToColor( stringColor, result ) ); + + DALI_TEST_EQUALS( result, Color::RED, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliPropertyHelperConvertStringPropertyToColor(void) +{ + tet_infoline( "Test to check whether A Property value containing a string can be converted" ); + + const std::string stringColor( "#00FF00" ); + Property::Value colorProperty( stringColor ); + + Vector4 result; + DALI_TEST_CHECK( Toolkit::Internal::ConvertPropertyToColor( colorProperty, result ) ); + + DALI_TEST_EQUALS( result, Color::GREEN, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliPropertyHelperConvertVector4PropertyToColor(void) +{ + tet_infoline( "Test to check whether A Property value containing a string can be converted" ); + + const Vector4 color( 0.0, 0.0, 1.0, 1.0 ); + Property::Value colorProperty( color ); + + Vector4 result; + DALI_TEST_CHECK( Toolkit::Internal::ConvertPropertyToColor( colorProperty, result ) ); + + DALI_TEST_EQUALS( result, Color::BLUE, TEST_LOCATION ); + + END_TEST; +} diff --git a/dali-toolkit/internal/builder/builder-set-property.cpp b/dali-toolkit/internal/builder/builder-set-property.cpp index d64a04c..93a1fbe 100644 --- a/dali-toolkit/internal/builder/builder-set-property.cpp +++ b/dali-toolkit/internal/builder/builder-set-property.cpp @@ -19,13 +19,13 @@ #include #include #include -#include // INTERNAL INCLUDES #include #include #include #include +#include namespace Dali { @@ -36,29 +36,6 @@ namespace Toolkit namespace Internal { - -namespace -{ - -/** - * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4. - * The Vector4 alpha component will be set to 1.0f - * @param hexString The HTML style hex string - * @return a Vector4 containing the new color value - */ -Vector4 HexStringToVector4( const char* s ) -{ - unsigned int value(0u); - std::istringstream( s ) >> std::hex >> value; - return Vector4( ((value >> 16 ) & 0xff ) / 255.0f, - ((value >> 8 ) & 0xff ) / 255.0f, - (value & 0xff ) / 255.0f, - 1.0f ); -} - -} // anon namespace - - /** * A property value type can be forced when its unknown by a disambiguation convention in the json * ie "myarray": [1,2,3,4] ; would be a vector but @@ -194,17 +171,9 @@ bool DeterminePropertyFromNode( const TreeNode& node, Property::Type type, Prope } else if( OptionalString s = replacer.IsString(node) ) { - if( (*s)[0] == '#' && 7 == (*s).size() ) - { - value = HexStringToVector4( &(*s)[1] ); - done = true; - } - else if( Dali::ColorController::Get() ) - { - Vector4 color; - done = Dali::ColorController::Get().RetrieveColor( *s, color ); - value = color; - } + Vector4 color; + done = ConvertStringToColor( *s, color ); + value = color; } else if( TreeNode::OBJECT == node.GetType() ) { diff --git a/dali-toolkit/internal/file.list b/dali-toolkit/internal/file.list index 6ec0d0c..b787acd 100755 --- a/dali-toolkit/internal/file.list +++ b/dali-toolkit/internal/file.list @@ -88,6 +88,7 @@ toolkit_src_files = \ \ $(toolkit_src_dir)/focus-manager/keyboard-focus-manager-impl.cpp \ $(toolkit_src_dir)/focus-manager/keyinput-focus-manager-impl.cpp \ + $(toolkit_src_dir)/helpers/color-conversion.cpp \ $(toolkit_src_dir)/helpers/property-helper.cpp \ $(toolkit_src_dir)/filters/blur-two-pass-filter.cpp \ $(toolkit_src_dir)/filters/emboss-filter.cpp \ diff --git a/dali-toolkit/internal/helpers/color-conversion.cpp b/dali-toolkit/internal/helpers/color-conversion.cpp new file mode 100644 index 0000000..6789cee --- /dev/null +++ b/dali-toolkit/internal/helpers/color-conversion.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// CLASS HEADER +#include + +// EXTERNAL INCLUDES +#include +#include +#include + +using Dali::Vector4; + +namespace +{ + +/** + * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4. + * The Vector4 alpha component will be set to 1.0f + * @param hexString The HTML style hex string + * @return a Vector4 containing the new color value + */ +Vector4 HexStringToVector4( const char* s ) +{ + unsigned int value(0u); + std::istringstream( s ) >> std::hex >> value; + return Vector4( ((value >> 16 ) & 0xff ) / 255.0f, + ((value >> 8 ) & 0xff ) / 255.0f, + (value & 0xff ) / 255.0f, + 1.0f ); +} + +} // unnamed namespace + +namespace Dali +{ +namespace Toolkit +{ +namespace Internal +{ + +bool ConvertStringToColor( const std::string& colorString, Vector4& outColor ) +{ + bool success( false ); + + if( ( '#' == colorString[0] ) && + ( 7 == colorString.size() ) ) + { + const char* cString = colorString.c_str(); + outColor = HexStringToVector4( &cString[1] ); + success = true; + } + else + { + Dali::ColorController controller = Dali::ColorController::Get(); + + if( controller ) + { + success = controller.RetrieveColor( colorString, outColor ); + } + } + + return success; +} + +bool ConvertPropertyToColor( const Property::Value& colorValue, Vector4& outColor ) +{ + bool success( false ); + + if( Property::VECTOR4 == colorValue.GetType() ) + { + success = colorValue.Get( outColor ); + } + else if( Property::STRING == colorValue.GetType() ) + { + std::string colorString; + if( colorValue.Get( colorString ) ) + { + success = ConvertStringToColor( colorString, outColor ); + } + } + + return success; +} + +} // Internal +} // Toolkit +} // Dali diff --git a/dali-toolkit/internal/helpers/color-conversion.h b/dali-toolkit/internal/helpers/color-conversion.h new file mode 100644 index 0000000..dcd51bd --- /dev/null +++ b/dali-toolkit/internal/helpers/color-conversion.h @@ -0,0 +1,61 @@ +#ifndef DALI_TOOLKIT_INTERNAL_COLOR_CONVERSION_H +#define DALI_TOOLKIT_INTERNAL_COLOR_CONVERSION_H + +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// EXTERNAL INCLUDES +#include +#include + +namespace Dali +{ + +class Vector4; + +namespace Toolkit +{ +namespace Internal +{ + +/* + * @brief Convert the string representation of a color into a Vector4. + * + * The supported string formats are: + * 1) An HTML style 'color' hex string ("#FF0000" for bright red). + * 2) An ID referring to the color palette of the current theme e.g. "B018" + * + * @param[in] colorString The color in string format. + * @param[out] outColor The color if found. + * @return True if the conversion was successful. + */ +bool ConvertStringToColor( const std::string& colorString, Vector4& outColor ); + +/* + * @brief Convert a variety of different color representations into a Vector4. + * + * @param[in] colorValue The color in Vector4 or string format. + * @param[out] outColor The color if found. + * @return True if the conversion was successful. + */ +bool ConvertPropertyToColor( const Property::Value& colorValue, Vector4& outColor ); + +} // Internal +} // Toolkit +} // Dali + + +#endif // DALI_TOOLKIT_INTERNAL_COLOR_CONVERSION_H