Provided color-conversion helpers 13/115613/9
authorPaul Wisbey <p.wisbey@samsung.com>
Mon, 20 Feb 2017 15:54:48 +0000 (15:54 +0000)
committerPaul Wisbey <p.wisbey@samsung.com>
Mon, 20 Feb 2017 17:55:17 +0000 (17:55 +0000)
Change-Id: I95e2b4c338e32c012d05f8a63237a419283222c5

automated-tests/src/dali-toolkit-internal/CMakeLists.txt
automated-tests/src/dali-toolkit-internal/utc-Dali-ColorConversion.cpp [new file with mode: 0644]
dali-toolkit/internal/builder/builder-set-property.cpp
dali-toolkit/internal/file.list
dali-toolkit/internal/helpers/color-conversion.cpp [new file with mode: 0644]
dali-toolkit/internal/helpers/color-conversion.h [new file with mode: 0644]

index f07653b..023d4bc 100755 (executable)
@@ -24,6 +24,7 @@ SET(TC_SOURCES
  utc-Dali-DebugRendering.cpp
  utc-Dali-ItemView-internal.cpp
  utc-Dali-PropertyHelper.cpp
  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)
 )
 
 # 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 (file)
index 0000000..14cef1c
--- /dev/null
@@ -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 <dali-toolkit-test-suite-utils.h>
+#include <dali-toolkit/internal/helpers/color-conversion.h>
+
+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;
+}
index d64a04c..93a1fbe 100644 (file)
 #include <sstream>
 #include <dali/public-api/object/property-array.h>
 #include <dali/public-api/object/property-map.h>
 #include <sstream>
 #include <dali/public-api/object/property-array.h>
 #include <dali/public-api/object/property-map.h>
-#include <dali/devel-api/adaptor-framework/color-controller.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/builder/builder-impl.h>
 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
 #include <dali-toolkit/internal/builder/replacement.h>
 #include <dali-toolkit/internal/builder/builder-set-property.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/builder/builder-impl.h>
 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
 #include <dali-toolkit/internal/builder/replacement.h>
 #include <dali-toolkit/internal/builder/builder-set-property.h>
+#include <dali-toolkit/internal/helpers/color-conversion.h>
 
 namespace Dali
 {
 
 namespace Dali
 {
@@ -36,29 +36,6 @@ namespace Toolkit
 namespace Internal
 {
 
 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
 /**
  * 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) )
       {
       }
       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() )
       {
       }
       else if( TreeNode::OBJECT == node.GetType() )
       {
index 6ec0d0c..b787acd 100755 (executable)
@@ -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)/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 \
    $(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 (file)
index 0000000..6789cee
--- /dev/null
@@ -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 <dali-toolkit/internal/helpers/color-conversion.h>
+
+// EXTERNAL INCLUDES
+#include <sstream>
+#include <dali/public-api/math/vector4.h>
+#include <dali/devel-api/adaptor-framework/color-controller.h>
+
+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 (file)
index 0000000..dcd51bd
--- /dev/null
@@ -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 <string>
+#include <dali/public-api/object/property.h>
+
+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