Merge "DALi Version 1.9.30" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / helpers / color-conversion.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/helpers/color-conversion.h>
19
20 // EXTERNAL INCLUDES
21 #include <sstream>
22 #include <dali/public-api/math/vector4.h>
23 #include <dali/devel-api/adaptor-framework/color-controller.h>
24
25 using Dali::Vector4;
26
27 namespace
28 {
29
30 /**
31  * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
32  * The Vector4 alpha component will be set to 1.0f
33  * @param hexString The HTML style hex string
34  * @return a Vector4 containing the new color value
35  */
36 Vector4 HexStringToVector4( const char* s )
37 {
38   unsigned int value(0u);
39   std::istringstream( s ) >> std::hex >> value;
40   return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
41                   ((value >> 8 ) & 0xff ) / 255.0f,
42                   (value & 0xff ) / 255.0f,
43                   1.0f );
44 }
45
46 } // unnamed namespace
47
48 namespace Dali
49 {
50 namespace Toolkit
51 {
52 namespace Internal
53 {
54
55 bool ConvertStringToColor( const std::string& colorString, Vector4& outColor )
56 {
57   bool success( false );
58
59   if( ( '#' == colorString[0] ) &&
60       (  7  == colorString.size() ) )
61   {
62     const char* cString = colorString.c_str();
63     outColor = HexStringToVector4( &cString[1] );
64     success = true;
65   }
66   else
67   {
68     Dali::ColorController controller = Dali::ColorController::Get();
69
70     if( controller )
71     {
72       success = controller.RetrieveColor( colorString, outColor );
73     }
74   }
75
76   return success;
77 }
78
79 bool ConvertPropertyToColor( const Property::Value& colorValue, Vector4& outColor )
80 {
81   bool success( false );
82
83   if( Property::VECTOR4 == colorValue.GetType() )
84   {
85     success = colorValue.Get( outColor );
86   }
87   else if( Property::STRING == colorValue.GetType() )
88   {
89     std::string colorString;
90     if( colorValue.Get( colorString ) )
91     {
92       success = ConvertStringToColor( colorString, outColor );
93     }
94   }
95
96   return success;
97 }
98
99 } // Internal
100 } // Toolkit
101 } // Dali