3 * Copyright (c) 2007-2010 SlimDX Group
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
24 #include "../InternalHelpers.h"
\r
25 #include "../math/Color4.h"
\r
27 #include "Color4Converter.h"
\r
28 #include "FieldPropertyDescriptor.h"
\r
30 using namespace System;
\r
31 using namespace System::Collections;
\r
32 using namespace System::Drawing;
\r
33 using namespace System::ComponentModel;
\r
34 using namespace System::ComponentModel::Design::Serialization;
\r
35 using namespace System::Globalization;
\r
36 using namespace System::Reflection;
\r
42 Color4Converter::Color4Converter()
\r
44 Type^ type = Color4::typeid;
\r
45 array<PropertyDescriptor^>^ propArray =
\r
47 gcnew FieldPropertyDescriptor(type->GetField("Red")),
\r
48 gcnew FieldPropertyDescriptor(type->GetField("Green")),
\r
49 gcnew FieldPropertyDescriptor(type->GetField("Blue")),
\r
50 gcnew FieldPropertyDescriptor(type->GetField("Alpha")),
\r
53 m_Properties = gcnew PropertyDescriptorCollection(propArray);
\r
56 bool Color4Converter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
\r
58 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
\r
61 return ExpandableObjectConverter::CanConvertTo(context, destinationType);
\r
64 bool Color4Converter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
\r
66 if( sourceType == String::typeid )
\r
69 return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
\r
72 Object^ Color4Converter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
\r
74 if( destinationType == nullptr )
\r
75 throw gcnew ArgumentNullException( "destinationType" );
\r
77 if( culture == nullptr )
\r
78 culture = CultureInfo::CurrentCulture;
\r
80 Color4^ color = dynamic_cast<Color4^>( value );
\r
82 if( destinationType == String::typeid && color != nullptr )
\r
84 String^ separator = culture->TextInfo->ListSeparator + " ";
\r
85 TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);
\r
86 array<String^>^ stringArray = gcnew array<String^>( 4 );
\r
88 stringArray[0] = converter->ConvertToString( context, culture, color->Alpha );
\r
89 stringArray[1] = converter->ConvertToString( context, culture, color->Red );
\r
90 stringArray[2] = converter->ConvertToString( context, culture, color->Green );
\r
91 stringArray[3] = converter->ConvertToString( context, culture, color->Blue );
\r
93 return String::Join( separator, stringArray );
\r
95 else if( destinationType == InstanceDescriptor::typeid && color != nullptr )
\r
97 ConstructorInfo^ info = (Color4::typeid)->GetConstructor( gcnew array<Type^> { float::typeid, float::typeid, float::typeid, float::typeid } );
\r
98 if( info != nullptr )
\r
99 return gcnew InstanceDescriptor( info, gcnew array<Object^> { color->Alpha, color->Red, color->Green, color->Blue } );
\r
102 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
\r
105 Object^ Color4Converter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
\r
107 if( culture == nullptr )
\r
108 culture = CultureInfo::CurrentCulture;
\r
110 String^ string = dynamic_cast<String^>( value );
\r
112 if( string != nullptr )
\r
114 string = string->Trim();
\r
115 TypeConverter^ floatConverter = TypeDescriptor::GetConverter(float::typeid);
\r
116 array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
\r
118 if( stringArray->Length == 1 )
\r
121 if( int::TryParse( string, number ) )
\r
122 return gcnew Color4( number );
\r
124 TypeConverter^ colorConverter = TypeDescriptor::GetConverter(Color::typeid);
\r
125 return gcnew Color4( safe_cast<Color>( colorConverter->ConvertFromString( context, culture, string ) ) );
\r
127 else if( stringArray->Length == 3 )
\r
132 if( int::TryParse( stringArray[0], red ) && int::TryParse( stringArray[1], green ) && int::TryParse( stringArray[2], blue ) )
\r
133 return gcnew Color4( Color::FromArgb( red, green, blue ) );
\r
135 float r = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[0] ) );
\r
136 float g = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[1] ) );
\r
137 float b = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[2] ) );
\r
139 return gcnew Color4( r, g, b );
\r
141 else if( stringArray->Length == 4 )
\r
147 if( int::TryParse( stringArray[0], alpha ) && int::TryParse( stringArray[1], red ) && int::TryParse( stringArray[2], green ) && int::TryParse( stringArray[3], blue ) )
\r
148 return gcnew Color4( Color::FromArgb( alpha, red, green, blue ) );
\r
150 float a = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[0] ) );
\r
151 float r = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[1] ) );
\r
152 float g = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[2] ) );
\r
153 float b = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[3] ) );
\r
155 return gcnew Color4( a, r, g, b );
\r
158 throw gcnew ArgumentException("Invalid color format.");
\r
161 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
\r
164 bool Color4Converter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)
\r
166 SLIMDX_UNREFERENCED_PARAMETER(context);
\r
171 Object^ Color4Converter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)
\r
173 SLIMDX_UNREFERENCED_PARAMETER(context);
\r
175 if( propertyValues == nullptr )
\r
176 throw gcnew ArgumentNullException( "propertyValues" );
\r
178 return gcnew Color4( safe_cast<float>( propertyValues["Alpha"] ), safe_cast<float>( propertyValues["Red"] ),
\r
179 safe_cast<float>( propertyValues["Green"] ), safe_cast<float>( propertyValues["Blue"] ) );
\r
182 bool Color4Converter::GetPropertiesSupported(ITypeDescriptorContext^)
\r
187 PropertyDescriptorCollection^ Color4Converter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)
\r
189 return m_Properties;
\r