2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/internal/helpers/color-conversion.h>
21 #include <dali/devel-api/adaptor-framework/color-controller.h>
22 #include <dali/public-api/math/vector4.h>
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
35 Vector4 HexStringToVector4(const char* s)
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,
45 } // unnamed namespace
53 bool ConvertStringToColor(const std::string& colorString, Vector4& outColor)
57 if(('#' == colorString[0]) &&
58 (7 == colorString.size()))
60 const char* cString = colorString.c_str();
61 outColor = HexStringToVector4(&cString[1]);
66 Dali::ColorController controller = Dali::ColorController::Get();
70 success = controller.RetrieveColor(colorString, outColor);
77 bool ConvertPropertyToColor(const Property::Value& colorValue, Vector4& outColor)
81 if(Property::VECTOR4 == colorValue.GetType())
83 success = colorValue.Get(outColor);
85 else if(Property::STRING == colorValue.GetType())
87 std::string colorString;
88 if(colorValue.Get(colorString))
90 success = ConvertStringToColor(colorString, outColor);
97 } // namespace Internal
98 } // namespace Toolkit