Add post processor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / helpers / color-conversion.cpp
1 /*
2  * Copyright (c) 2021 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 <dali/devel-api/adaptor-framework/color-controller.h>
22 #include <dali/public-api/math/vector4.h>
23 #include <sstream>
24
25 using Dali::Vector4;
26
27 namespace
28 {
29 /**
30  * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
31  * The Vector4 alpha component will be set to 1.0f
32  * @param hexString The HTML style hex string
33  * @return a Vector4 containing the new color value
34  */
35 Vector4 HexStringToVector4(const char* s)
36 {
37   unsigned int value(0u);
38   std::istringstream(s) >> std::hex >> value;
39   return Vector4(((value >> 16) & 0xff) / 255.0f,
40                  ((value >> 8) & 0xff) / 255.0f,
41                  (value & 0xff) / 255.0f,
42                  1.0f);
43 }
44
45 } // unnamed namespace
46
47 namespace Dali
48 {
49 namespace Toolkit
50 {
51 namespace Internal
52 {
53 bool ConvertStringToColor(const std::string& colorString, Vector4& outColor)
54 {
55   bool success(false);
56
57   if(('#' == colorString[0]) &&
58      (7 == colorString.size()))
59   {
60     const char* cString = colorString.c_str();
61     outColor            = HexStringToVector4(&cString[1]);
62     success             = true;
63   }
64   else
65   {
66     Dali::ColorController controller = Dali::ColorController::Get();
67
68     if(controller)
69     {
70       success = controller.RetrieveColor(colorString, outColor);
71     }
72   }
73
74   return success;
75 }
76
77 bool ConvertPropertyToColor(const Property::Value& colorValue, Vector4& outColor)
78 {
79   bool success(false);
80
81   if(Property::VECTOR4 == colorValue.GetType())
82   {
83     success = colorValue.Get(outColor);
84   }
85   else if(Property::STRING == colorValue.GetType())
86   {
87     std::string colorString;
88     if(colorValue.Get(colorString))
89     {
90       success = ConvertStringToColor(colorString, outColor);
91     }
92   }
93
94   return success;
95 }
96
97 } // namespace Internal
98 } // namespace Toolkit
99 } // namespace Dali