4 using System.Globalization;
5 using System.Windows.Media;
7 using SiliconStudio.Core.Mathematics;
11 namespace SiliconStudio.Presentation.ValueConverters
21 public override object Convert(
object value, Type targetType,
object parameter, CultureInfo culture)
26 var color = (
Color)value;
27 if (targetType == typeof(System.Windows.Media.Color))
28 return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
29 if (targetType == typeof(Color))
31 if (targetType == typeof(
Color3))
32 return color.ToColor3();
33 if (targetType == typeof(
Color4))
34 return color.ToColor4();
35 if (targetType.IsAssignableFrom(typeof(SolidColorBrush)))
36 return new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
37 if (targetType == typeof(
string))
38 return '#' + color.ToRgba().ToString(
"X8");
42 var color = (Color3)value;
43 if (targetType == typeof(System.Windows.Media.Color))
44 return System.Windows.Media.Color.FromScRgb(1.0f, color.R, color.G, color.B);
45 if (targetType == typeof(Color))
46 return new Color(color.R, color.G, color.B);
47 if (targetType == typeof(Color3))
49 if (targetType == typeof(
Color4))
50 return new Color4(color.R, color.G, color.B, 1.0f);
51 if (targetType.IsAssignableFrom(typeof(SolidColorBrush)))
52 return new SolidColorBrush(System.Windows.Media.Color.FromScRgb(1.0f, color.R, color.G, color.B));
53 if (targetType == typeof(
string))
54 return '#' + color.ToRgb().ToString(
"X6");
58 var color = (Color4)value;
59 if (targetType == typeof(System.Windows.Media.Color))
60 return System.Windows.Media.Color.FromScRgb(color.A, color.R, color.G, color.B);
61 if (targetType == typeof(Color))
62 return new Color(color.R, color.G, color.B, color.A);
63 if (targetType == typeof(Color3))
64 return new Color3(color.R, color.G, color.B);
65 if (targetType == typeof(Color4))
67 if (targetType.IsAssignableFrom(typeof(SolidColorBrush)))
68 return new SolidColorBrush(System.Windows.Media.Color.FromScRgb(color.A, color.R, color.G, color.B));
69 if (targetType == typeof(
string))
70 return '#' + color.ToRgba().ToString(
"X8");
74 var stringColor = value as string;
76 if (!stringColor.StartsWith(
"#") || !Int32.TryParse(stringColor.Substring(1), NumberStyles.HexNumber, null, out intValue))
78 intValue = unchecked((
int)0xFF000000);
80 if (targetType == typeof(Color))
81 return Color.FromRgba(intValue);
82 if (targetType == typeof(Color3))
83 return new Color3(intValue);
84 if (targetType == typeof(Color4))
85 return new Color4(intValue);
86 if (targetType == typeof(System.Windows.Media.Color))
88 return System.Windows.Media.Color.FromArgb(
89 (byte)((intValue >> 24) & 255),
90 (byte)(intValue & 255),
91 (byte)((intValue >> 8) & 255),
92 (byte)((intValue >> 16) & 255));
94 if (targetType.IsAssignableFrom(typeof(SolidColorBrush)))
96 return new SolidColorBrush(System.Windows.Media.Color.FromArgb(
97 (byte)((intValue >> 24) & 255),
98 (byte)(intValue & 255),
99 (byte)((intValue >> 8) & 255),
100 (byte)((intValue >> 16) & 255)));
102 if (targetType == typeof(
string))
105 throw new NotSupportedException(
"Requested conversion is not supported.");
109 public override object ConvertBack(
object value, Type targetType,
object parameter, CultureInfo culture)
111 if (targetType == typeof(
object))
114 var stringColor = value as string;
115 if (stringColor != null)
118 if (!stringColor.StartsWith(
"#") || !Int32.TryParse(stringColor.Substring(1), NumberStyles.HexNumber, null, out intValue))
120 intValue = unchecked((
int)0xFF000000);
122 if (targetType == typeof(
Color))
123 return Color.FromRgba(intValue);
124 if (targetType == typeof(
Color3))
125 return new Color3(intValue);
126 if (targetType == typeof(
Color4))
127 return new Color4(intValue);
129 if (value is SolidColorBrush)
131 var brush = (SolidColorBrush)value;
134 if (value is System.Windows.Media.Color)
137 if (targetType == typeof(
Color))
138 return new Color(wpfColor.R, wpfColor.G, wpfColor.B, wpfColor.A);
139 if (targetType == typeof(
Color3))
140 return new Color3(wpfColor.ScR, wpfColor.ScG, wpfColor.ScB);
141 if (targetType == typeof(
Color4))
142 return new Color4(wpfColor.ScR, wpfColor.ScG, wpfColor.ScB, wpfColor.ScA);
146 var color = (
Color)value;
147 if (targetType == typeof(Color))
149 if (targetType == typeof(
Color3))
150 return color.ToColor3();
151 if (targetType == typeof(
Color4))
152 return color.ToColor4();
156 var color = (Color3)value;
157 if (targetType == typeof(Color))
158 return new Color(color.R, color.G, color.B);
159 if (targetType == typeof(Color3))
161 if (targetType == typeof(
Color4))
162 return new Color4(1.0f, color.R, color.G, color.B);
166 var color = (Color4)value;
167 if (targetType == typeof(Color))
168 return new Color(color.R, color.G, color.B, color.A);
169 if (targetType == typeof(Color3))
170 return new Color3(color.R, color.G, color.B);
171 if (targetType == typeof(Color4))
174 throw new NotSupportedException(
"Requested conversion is not supported.");
SiliconStudio.Core.Mathematics.Color Color
Represents a color in the form of rgb.
override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Represents a color in the form of rgba.
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
This converter will convert any known type of color value to the target type, if the conversion is po...
override object Convert(object value, Type targetType, object parameter, CultureInfo culture)