Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ColorConverter.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Globalization;
5 using System.Windows.Media;
6 
7 using SiliconStudio.Core.Mathematics;
8 
10 
11 namespace SiliconStudio.Presentation.ValueConverters
12 {
13  /// <summary>
14  /// This converter will convert any known type of color value to the target type, if the conversion is possible. Otherwise, a <see cref="NotSupportedException"/> will be thrown.
15  /// The currently input types supported are <see cref="SiliconStudio.Core.Mathematics.Color"/>, <see cref="Color3"/>, <see cref="Color4"/>.
16  /// The currently output types supported are <see cref="System.Windows.Media.Color"/>, <see cref="SiliconStudio.Core.Mathematics.Color"/>, <see cref="Color3"/>, <see cref="Color4"/>, <see cref="string"/>, <see cref="System.Windows.Media.Brush"/>, <see cref="object"/>.
17  /// </summary>
18  public class ColorConverter : ValueConverterBase<ColorConverter>
19  {
20  /// <inheritdoc/>
21  public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
22  {
23  // TODO: string conversion is not correctly supported
24  if (value is Color)
25  {
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))
30  return 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");
39  }
40  if (value is Color3)
41  {
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))
48  return color;
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");
55  }
56  if (value is Color4)
57  {
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))
66  return color;
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");
71  }
72  if (value is string)
73  {
74  var stringColor = value as string;
75  int intValue;
76  if (!stringColor.StartsWith("#") || !Int32.TryParse(stringColor.Substring(1), NumberStyles.HexNumber, null, out intValue))
77  {
78  intValue = unchecked((int)0xFF000000);
79  }
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))
87  {
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));
93  }
94  if (targetType.IsAssignableFrom(typeof(SolidColorBrush)))
95  {
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)));
101  }
102  if (targetType == typeof(string))
103  return stringColor;
104  }
105  throw new NotSupportedException("Requested conversion is not supported.");
106  }
107 
108  /// <inheritdoc/>
109  public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
110  {
111  if (targetType == typeof(object))
112  return value;
113 
114  var stringColor = value as string;
115  if (stringColor != null)
116  {
117  int intValue;
118  if (!stringColor.StartsWith("#") || !Int32.TryParse(stringColor.Substring(1), NumberStyles.HexNumber, null, out intValue))
119  {
120  intValue = unchecked((int)0xFF000000);
121  }
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);
128  }
129  if (value is SolidColorBrush)
130  {
131  var brush = (SolidColorBrush)value;
132  value = brush.Color;
133  }
134  if (value is System.Windows.Media.Color)
135  {
136  var wpfColor = (System.Windows.Media.Color)value;
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);
143  }
144  if (value is Color)
145  {
146  var color = (Color)value;
147  if (targetType == typeof(Color))
148  return color;
149  if (targetType == typeof(Color3))
150  return color.ToColor3();
151  if (targetType == typeof(Color4))
152  return color.ToColor4();
153  }
154  if (value is Color3)
155  {
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))
160  return color;
161  if (targetType == typeof(Color4))
162  return new Color4(1.0f, color.R, color.G, color.B);
163  }
164  if (value is Color4)
165  {
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))
172  return color;
173  }
174  throw new NotSupportedException("Requested conversion is not supported.");
175  }
176  }
177 }
SiliconStudio.Core.Mathematics.Color Color
Represents a color in the form of rgb.
Definition: Color3.cs:41
override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Represents a color in the form of rgba.
Definition: Color4.cs:42
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Definition: Color.cs:16
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)