2 * Copyright (c) 2017 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>
22 #include <dali/public-api/math/vector4.h>
23 #include <dali/devel-api/adaptor-framework/color-controller.h>
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
36 Vector4 HexStringToVector4( const char* s )
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,
46 } // unnamed namespace
55 bool ConvertStringToColor( const std::string& colorString, Vector4& outColor )
57 bool success( false );
59 if( ( '#' == colorString[0] ) &&
60 ( 7 == colorString.size() ) )
62 const char* cString = colorString.c_str();
63 outColor = HexStringToVector4( &cString[1] );
68 Dali::ColorController controller = Dali::ColorController::Get();
72 success = controller.RetrieveColor( colorString, outColor );
79 bool ConvertPropertyToColor( const Property::Value& colorValue, Vector4& outColor )
81 bool success( false );
83 if( Property::VECTOR4 == colorValue.GetType() )
85 success = colorValue.Get( outColor );
87 else if( Property::STRING == colorValue.GetType() )
89 std::string colorString;
90 if( colorValue.Get( colorString ) )
92 success = ConvertStringToColor( colorString, outColor );