2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System.Globalization;
23 /// The Color is a struct to record Check's state.
41 /// Gets a default Color instance.
44 /// In default Color instance,Mode type is Default,RGBA all set as -1.
46 public static Color Default
48 get { return new Color(-1, -1, -1, -1, Mode.Default); }
52 /// Gets whether the Color instance's mode is default or not.
53 /// Return type is bool.
57 get { return _mode == Mode.Default; }
61 /// Gets A value of RGBA.
62 /// A means the Alpha in color.
70 /// Gets R value of RGBA.
71 /// R means the Red in color.
79 /// Gets G value of RGBA.
80 /// G means the Green in color.
88 /// Gets B value of RGBA.
89 /// B means the Blue in color.
97 /// Creates and initializes a new instance of the Color class.
98 /// With RGB parameters.
100 /// <param name="r">Red of RGB</param>
101 /// <param name="g">Green of RGB</param>
102 /// <param name="b">Blue of RGB</param>
103 public Color(int r, int g, int b) : this(r, g, b, 255)
108 /// Creates and initializes a new instance of the Color class.
109 /// With RGBA parameters.
111 /// <param name="r">Red of RGBA</param>
112 /// <param name="g">Green of RGBA</param>
113 /// <param name="b">Blue of RGBA</param>
114 /// <param name="a">Alpha of RGBA</param>
115 public Color(int r, int g, int b, int a) : this(r, g, b, a, Mode.Rgb)
119 Color(int r, int g, int b, int a, Mode mode)
122 if (mode == Mode.Rgb)
124 _r = Clamp(r, 0, 255);
125 _g = Clamp(g, 0, 255);
126 _b = Clamp(b, 0, 255);
127 _a = Clamp(a, 0, 255);
131 _r = _g = _b = _a = -1;
136 /// Returns the hash code for this instance.
138 /// <returns>A 32-bit signed integer hash code.</returns>
139 public override int GetHashCode()
141 int hashcode = _r.GetHashCode();
142 hashcode = (hashcode * 397) ^ _g.GetHashCode();
143 hashcode = (hashcode * 397) ^ _b.GetHashCode();
144 hashcode = (hashcode * 397) ^ _a.GetHashCode();
149 /// Indicates whether this instance and a specified object are equal.
151 /// <param name="obj">The object to compare with the current instance.</param>
153 /// true if obj and this instance are the same type and represent the same value.
154 /// otherwise, false.
156 public override bool Equals(object obj)
160 return EqualsInner(this, (Color)obj);
162 return base.Equals(obj);
166 /// Compare whether two Color instance is same or not.
168 /// <param name="a">A Color instance.</param>
169 /// <param name="b">A Color instance.</param>
170 /// <returns>The result whether two instance is same or not.
171 /// Return type is bool.If they are same, return true.
173 public static bool operator ==(Color a, Color b)
175 if (ReferenceEquals(a, b))
178 if ((object)a == null || (object)b == null)
181 return EqualsInner(a, b);
185 /// Compare whether two Color instance is different or not.
187 /// <param name="a">A Color instance.</param>
188 /// <param name="b">A Color instance.</param>
189 /// <returns>The result whether two instance is different or not.
190 /// Return type is bool.If they are different, return true.
192 public static bool operator !=(Color a, Color b)
197 static bool EqualsInner(Color color1, Color color2)
199 if (color1._mode == Mode.Default && color2._mode == Mode.Default)
201 return color1._r == color2._r && color1._g == color2._g && color1._b == color2._b && color1._a == color2._a;
205 /// Returns the fully qualified type name of this instance.
207 /// <returns>The fully qualified type name.</returns>
208 public override string ToString()
210 return string.Format(CultureInfo.InvariantCulture, "[Color: R={0}, G={1}, B={2}, A={3}]", R, G, B, A);
214 /// Gets a Color instance with a hexadecimal string parameter.
216 /// <param name="hex">Hexadecimal string.</param>
217 /// <returns>New instance of Color struct.</returns>
218 public static Color FromHex(string hex)
220 hex = hex.Replace("#", "");
223 case 3: //#rgb => ffrrggbb
224 hex = string.Format("ff{0}{1}{2}{3}{4}{5}", hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]);
226 case 4: //#argb => aarrggbb
227 hex = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", hex[0], hex[0], hex[1], hex[1], hex[2], hex[2], hex[3], hex[3]);
229 case 6: //#rrggbb => ffrrggbb
230 hex = string.Format("ff{0}", hex);
233 return FromUint(Convert.ToUInt32(hex.Replace("#", ""), 16));
237 /// Gets a Color instance with a Unsigned integer parameter.
239 /// <param name="argb">Unsigned integer indicates RGBA.</param>
240 /// <returns>New instance of Color struct.</returns>
241 public static Color FromUint(uint argb)
243 return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
247 /// Gets a Color instance with R,G,B,A parameters.
249 /// <param name="r">Red in RGBA.</param>
250 /// <param name="g">Green in RGBA.</param>
251 /// <param name="b">Blue in RGBA.</param>
252 /// <param name="a">Alpha in RGBA.</param>
253 /// <returns>New instance of Color struct.</returns>
254 public static Color FromRgba(int r, int g, int b, int a)
256 return new Color(r, g, b, a);
260 /// Gets a Color instance with R,G,B,A parameters.
262 /// <param name="r">Red in RGB.</param>
263 /// <param name="g">Green in RGB.</param>
264 /// <param name="b">Blue in RGB.</param>
265 /// <returns>New instance of Color struct.</returns>
266 public static Color FromRgb(int r, int g, int b)
268 return FromRgba(r, g, b, 255);
271 internal static int Clamp(int self, int min, int max)
273 return Math.Min(max, Math.Max(self, min));
276 #region Color Definitions
278 /// The Tansparent is a predefined Color, it's rgba value is (0, 0, 0, 0).
280 public static readonly Color Transparent = FromRgba(0, 0, 0, 0);
282 /// The Aqua is a predefined Color instance, it's rgb value is (0, 255, 255).
284 public static readonly Color Aqua = FromRgb(0, 255, 255);
286 /// The Black is a predefined Color instance, it's rgb value is (0, 0, 0).
288 public static readonly Color Black = FromRgb(0, 0, 0);
290 /// The Blue is a predefined Color instance, it's rgb value is (0, 0, 255).
292 public static readonly Color Blue = FromRgb(0, 0, 255);
294 /// The Fuchsia is a predefined Color instance, it's rgb value is (255, 0, 255).
296 public static readonly Color Fuchsia = FromRgb(255, 0, 255);
298 /// The Gray is a predefined Color instance, it's rgb value is (128, 128, 128).
300 public static readonly Color Gray = FromRgb(128, 128, 128);
302 /// The Green is a predefined Color instance, it's rgb value is (0, 128, 0).
304 public static readonly Color Green = FromRgb(0, 128, 0);
306 /// The Lime is a predefined Color instance, it's rgb value is (0, 255, 0).
308 public static readonly Color Lime = FromRgb(0, 255, 0);
310 /// The Maroon is a predefined Color instance, it's rgb value is (128, 0, 0).
312 public static readonly Color Maroon = FromRgb(128, 0, 0);
314 /// The Navy is a predefined Color instance, it's rgb value is (0, 0, 128).
316 public static readonly Color Navy = FromRgb(0, 0, 128);
318 /// The Olive is a predefined Color instance, it's rgb value is (128, 128, 0).
320 public static readonly Color Olive = FromRgb(128, 128, 0);
322 /// The Orange is a predefined Color instance, it's rgb value is (255, 165, 0).
324 public static readonly Color Orange = FromRgb(255, 165, 0);
326 /// The Purple is a predefined Color instance, it's rgb value is (128, 0, 128).
328 public static readonly Color Purple = FromRgb(128, 0, 128);
330 /// The Pink is a predefined Color instance, it's rgb value is (255, 102, 255).
332 public static readonly Color Pink = FromRgb(255, 102, 255);
334 /// The Red is a predefined Color instance, it's rgb value is (255, 0, 0).
336 public static readonly Color Red = FromRgb(255, 0, 0);
338 /// The Silver is a predefined Color instance, it's rgb value is (192, 192, 192).
340 public static readonly Color Silver = FromRgb(192, 192, 192);
342 /// The Teal is a predefined Color instance, it's rgb value is (0, 128, 128).
344 public static readonly Color Teal = FromRgb(0, 128, 128);
346 /// The White is a predefined Color instance, it's rgb value is (255, 255, 255).
348 public static readonly Color White = FromRgb(255, 255, 255);
350 /// The Yellow is a predefined Color instance, it's rgb value is (255, 255, 0).
352 public static readonly Color Yellow = FromRgb(255, 255, 0);