Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ColorHSV.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 MIT License. See LICENSE.md for details.
3 using System;
4 using System.Globalization;
5 using System.Runtime.InteropServices;
6 using SiliconStudio.Core.Serialization;
7 
8 namespace SiliconStudio.Core.Mathematics
9 {
10  /// <summary>
11  /// Represents a color in the form of Hue, Saturation, Value, Alpha.
12  /// </summary>
13  [DataContract]
14  [StructLayout(LayoutKind.Sequential, Pack = 4)]
15  public struct ColorHSV : IEquatable<ColorHSV>, IFormattable
16  {
17  /// <summary>
18  /// The Hue of the color.
19  /// </summary>
20  [DataMember(0)]
21  public float H;
22 
23  /// <summary>
24  /// The Saturation of the color.
25  /// </summary>
26  [DataMember(1)]
27  public float S;
28 
29  /// <summary>
30  /// The Value of the color.
31  /// </summary>
32  [DataMember(2)]
33  public float V;
34 
35  /// <summary>
36  /// The alpha component of the color.
37  /// </summary>
38  [DataMember(3)]
39  public float A;
40 
41  /// <summary>
42  /// Initializes a new instance of the <see cref="ColorHSV"/> struct.
43  /// </summary>
44  /// <param name="h">The h.</param>
45  /// <param name="s">The s.</param>
46  /// <param name="v">The v.</param>
47  /// <param name="a">A.</param>
48  public ColorHSV(float h, float s, float v, float a)
49  {
50  H = h;
51  S = s;
52  V = v;
53  A = a;
54  }
55 
56  /// <summary>
57  /// Converts the color into a three component vector.
58  /// </summary>
59  /// <returns>A three component vector containing the red, green, and blue components of the color.</returns>
60  public Color4 ToColor()
61  {
62  int hi = Convert.ToInt32(Math.Floor(H / 60)) % 6;
63  float f = H / 60 - (float)Math.Floor(H / 60);
64 
65  float v = V;
66  float p = V * (1 - S);
67  float q = V * (1 - f * S);
68  float t = V * (1 - (1 - f) * S);
69 
70  switch (hi)
71  {
72  case 0:
73  return new Color4(v, t, p, A);
74  case 1:
75  return new Color4(q, v, p, A);
76  case 2:
77  return new Color4(p, v, t, A);
78  case 3:
79  return new Color4(p, q, v, A);
80  case 4:
81  return new Color4(t, p, v, A);
82  default:
83  return new Color4(v, p, q, A);
84  }
85  }
86 
87  /// <summary>
88  /// Converts the color into a HSV color.
89  /// </summary>
90  /// <param name="color">The color.</param>
91  /// <returns>A HSV color</returns>
92  public static ColorHSV FromColor(Color4 color)
93  {
94  float max = Math.Max(color.R, Math.Max(color.G, color.B));
95  float min = Math.Min(color.R, Math.Min(color.G, color.B));
96 
97  float delta = max - min;
98  float h = 0.0f;
99 
100  if (delta > 0.0f)
101  {
102  if (max == color.R && max != color.G)
103  h += (color.G - color.B) / delta;
104  if (max == color.G && max != color.B)
105  h += (2.0f + (color.B - color.R) / delta);
106  if (max == color.B && max != color.R)
107  h += (4.0f + (color.R - color.G) / delta);
108  h *= 60.0f;
109  }
110 
111  return new ColorHSV(h, (max != 0.0f) ? 1.0f - min / max : 0.0f, max, color.A);
112  }
113 
114  public bool Equals(ColorHSV other)
115  {
116  return other.H.Equals(H) && other.S.Equals(S) && other.V.Equals(V) && other.A.Equals(A);
117  }
118 
119  public override bool Equals(object obj)
120  {
121  if (ReferenceEquals(null, obj)) return false;
122  if (obj.GetType() != typeof(ColorHSV)) return false;
123  return Equals((ColorHSV)obj);
124  }
125 
126  public override int GetHashCode()
127  {
128  unchecked
129  {
130  int result = H.GetHashCode();
131  result = (result * 397) ^ S.GetHashCode();
132  result = (result * 397) ^ V.GetHashCode();
133  result = (result * 397) ^ A.GetHashCode();
134  return result;
135  }
136  }
137 
138  private const string ColorHSVFormat = "Hue:{0} Saturation:{1} Value:{2} Alpha:{3}";
139 
140  /// <summary>
141  /// Returns a <see cref="System.String"/> that represents this instance.
142  /// </summary>
143  /// <returns>
144  /// A <see cref="System.String"/> that represents this instance.
145  /// </returns>
146  public override string ToString()
147  {
148  return string.Format(CultureInfo.CurrentCulture, ColorHSVFormat, H, S, V, A);
149  }
150 
151  /// <summary>
152  /// Returns a <see cref="System.String"/> that represents this instance.
153  /// </summary>
154  /// <param name="format">The format.</param>
155  /// <returns>
156  /// A <see cref="System.String"/> that represents this instance.
157  /// </returns>
158  public string ToString(string format)
159  {
160  if (format == null)
161  return ToString();
162 
163  return string.Format(CultureInfo.CurrentCulture, ColorHSVFormat, H.ToString(format, CultureInfo.CurrentCulture),
164  S.ToString(format, CultureInfo.CurrentCulture), V.ToString(format, CultureInfo.CurrentCulture), A.ToString(format, CultureInfo.CurrentCulture));
165  }
166 
167  /// <summary>
168  /// Returns a <see cref="System.String"/> that represents this instance.
169  /// </summary>
170  /// <param name="formatProvider">The format provider.</param>
171  /// <returns>
172  /// A <see cref="System.String"/> that represents this instance.
173  /// </returns>
174  public string ToString(IFormatProvider formatProvider)
175  {
176  return string.Format(formatProvider, ColorHSVFormat, H, S, V, A);
177  }
178 
179  /// <summary>
180  /// Returns a <see cref="System.String"/> that represents this instance.
181  /// </summary>
182  /// <param name="format">The format.</param>
183  /// <param name="formatProvider">The format provider.</param>
184  /// <returns>
185  /// A <see cref="System.String"/> that represents this instance.
186  /// </returns>
187  public string ToString(string format, IFormatProvider formatProvider)
188  {
189  if (format == null)
190  return ToString(formatProvider);
191 
192  return string.Format(formatProvider, ColorHSVFormat, H.ToString(format, formatProvider),
193  S.ToString(format, formatProvider), V.ToString(format, formatProvider), A.ToString(format, formatProvider));
194  }
195  }
196 }
float A
The alpha component of the color.
Definition: Color4.cs:78
float H
The Hue of the color.
Definition: ColorHSV.cs:21
function a
float V
The Value of the color.
Definition: ColorHSV.cs:33
float B
The blue component of the color.
Definition: Color4.cs:72
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: ColorHSV.cs:187
float G
The green component of the color.
Definition: Color4.cs:66
Represents a color in the form of rgba.
Definition: Color4.cs:42
float S
The Saturation of the color.
Definition: ColorHSV.cs:27
string ToString(string format)
Returns a System.String that represents this instance.
Definition: ColorHSV.cs:158
override bool Equals(object obj)
Definition: ColorHSV.cs:119
static ColorHSV FromColor(Color4 color)
Converts the color into a HSV color.
Definition: ColorHSV.cs:92
float R
The red component of the color.
Definition: Color4.cs:60
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: ColorHSV.cs:174
function s(a)
float A
The alpha component of the color.
Definition: ColorHSV.cs:39
ColorHSV(float h, float s, float v, float a)
Initializes a new instance of the ColorHSV struct.
Definition: ColorHSV.cs:48
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
Color4 ToColor()
Converts the color into a three component vector.
Definition: ColorHSV.cs:60
override string ToString()
Returns a System.String that represents this instance.
Definition: ColorHSV.cs:146
Represents a color in the form of Hue, Saturation, Value, Alpha.
Definition: ColorHSV.cs:15