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 the check's state.
25 /// <since_tizen> preview </since_tizen>
42 /// Gets a default Color instance.
45 /// In the default Color instance, the mode type is default with RGBA all set as -1.
47 /// <since_tizen> preview </since_tizen>
48 public static Color Default
50 get { return new Color(-1, -1, -1, -1, Mode.Default); }
54 /// Gets whether the Color instance's mode is default or not.
55 /// The return type is bool.
57 /// <since_tizen> preview </since_tizen>
60 get { return _mode == Mode.Default; }
64 /// Gets the A value of RGBA.
65 /// A means the Alpha in color.
67 /// <since_tizen> preview </since_tizen>
74 /// Gets the R value of RGBA.
75 /// R means the Red in color.
77 /// <since_tizen> preview </since_tizen>
84 /// Gets the G value of RGBA.
85 /// G means the Green in color.
87 /// <since_tizen> preview </since_tizen>
94 /// Gets the B value of RGBA.
95 /// B means the Blue in color.
97 /// <since_tizen> preview </since_tizen>
104 /// Creates and initializes a new instance of the Color class
105 /// with RGB parameters.
107 /// <param name="r">Red of RGB.</param>
108 /// <param name="g">Green of RGB.</param>
109 /// <param name="b">Blue of RGB.</param>
110 /// <since_tizen> preview </since_tizen>
111 public Color(int r, int g, int b) : this(r, g, b, 255)
116 /// Creates and initializes a new instance of the Color class
117 /// with RGBA parameters.
119 /// <param name="r">Red of RGBA.</param>
120 /// <param name="g">Green of RGBA.</param>
121 /// <param name="b">Blue of RGBA.</param>
122 /// <param name="a">Alpha of RGBA.</param>
123 /// <since_tizen> preview </since_tizen>
124 public Color(int r, int g, int b, int a) : this(r, g, b, a, Mode.Rgb)
128 Color(int r, int g, int b, int a, Mode mode)
131 if (mode == Mode.Rgb)
133 _r = Clamp(r, 0, 255);
134 _g = Clamp(g, 0, 255);
135 _b = Clamp(b, 0, 255);
136 _a = Clamp(a, 0, 255);
140 _r = _g = _b = _a = -1;
145 /// Returns the hash code for this instance.
147 /// <returns>A 32-bit signed integer hash code.</returns>
148 /// <since_tizen> preview </since_tizen>
149 public override int GetHashCode()
151 int hashcode = _r.GetHashCode();
152 hashcode = (hashcode * 397) ^ _g.GetHashCode();
153 hashcode = (hashcode * 397) ^ _b.GetHashCode();
154 hashcode = (hashcode * 397) ^ _a.GetHashCode();
159 /// Indicates whether this instance and a specified object are equal.
161 /// <param name="obj">The object to compare with the current instance.</param>
163 /// true if the object and this instance are of the same type and represent the same value.
164 /// otherwise, false.
166 /// <since_tizen> preview </since_tizen>
167 public override bool Equals(object obj)
171 return EqualsInner(this, (Color)obj);
173 return base.Equals(obj);
177 /// Compares whether the two Color instances are same or not.
179 /// <param name="a">A Color instance.</param>
180 /// <param name="b">A Color instance.</param>
181 /// <returns>The result whether the two instances are the same or not.
182 /// Return type is bool. If they are same, return true.
184 /// <since_tizen> preview </since_tizen>
185 public static bool operator ==(Color a, Color b)
187 return EqualsInner(a, b);
191 /// Compares whether the two Color instances are different or not.
193 /// <param name="a">A Color instance.</param>
194 /// <param name="b">A Color instance.</param>
195 /// <returns>The result whether the two instances are different or not.
196 /// Return type is bool. If they are different, return true.
198 /// <since_tizen> preview </since_tizen>
199 public static bool operator !=(Color a, Color b)
204 static bool EqualsInner(Color color1, Color color2)
206 if (color1._mode == Mode.Default && color2._mode == Mode.Default)
208 return color1._r == color2._r && color1._g == color2._g && color1._b == color2._b && color1._a == color2._a;
212 /// Returns the fully qualified type name of this instance.
214 /// <returns>The fully qualified type name.</returns>
215 /// <since_tizen> preview </since_tizen>
216 public override string ToString()
218 return string.Format(CultureInfo.InvariantCulture, "[Color: R={0}, G={1}, B={2}, A={3}]", R, G, B, A);
222 /// Gets a Color instance with a hexadecimal string parameter.
224 /// <param name="hex">Hexadecimal string.</param>
225 /// <returns>New instance of the Color struct.</returns>
226 /// <since_tizen> preview </since_tizen>
227 public static Color FromHex(string hex)
229 string ret = hex.Replace("#", "");
232 case 3: //#rgb => ffrrggbb
233 ret = string.Format("ff{0}{1}{2}{3}{4}{5}", ret[0], ret[0], ret[1], ret[1], ret[2], ret[2]);
235 case 4: //#argb => aarrggbb
236 ret = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", ret[0], ret[0], ret[1], ret[1], ret[2], ret[2], ret[3], ret[3]);
238 case 6: //#rrggbb => ffrrggbb
239 ret = string.Format("ff{0}", ret);
242 return FromUint(Convert.ToUInt32(ret.Replace("#", ""), 16));
246 /// Gets a Color instance with an unsigned integer parameter.
248 /// <param name="argb">Unsigned integer indicates RGBA.</param>
249 /// <returns>New instance of the Color struct.</returns>
250 /// <since_tizen> preview </since_tizen>
251 public static Color FromUint(uint argb)
253 return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
257 /// Gets a Color instance with R,G,B,A parameters.
259 /// <param name="r">Red of RGBA.</param>
260 /// <param name="g">Green of RGBA.</param>
261 /// <param name="b">Blue of RGBA.</param>
262 /// <param name="a">Alpha of RGBA.</param>
263 /// <returns>New instance of the Color struct.</returns>
264 /// <since_tizen> preview </since_tizen>
265 public static Color FromRgba(int r, int g, int b, int a)
267 return new Color(r, g, b, a);
271 /// Gets a Color instance with R,G,B parameters.
273 /// <param name="r">Red of RGB.</param>
274 /// <param name="g">Green of RGB.</param>
275 /// <param name="b">Blue of RGB.</param>
276 /// <returns>New instance of the Color struct.</returns>
277 /// <since_tizen> preview </since_tizen>
278 public static Color FromRgb(int r, int g, int b)
280 return FromRgba(r, g, b, 255);
283 internal static int Clamp(int self, int min, int max)
285 return Math.Min(max, Math.Max(self, min));
288 #region Color Definitions
290 /// The Tansparent is a predefined Color instance. It's RGBA value is (0, 0, 0, 0).
292 /// <since_tizen> preview </since_tizen>
293 public static readonly Color Transparent = FromRgba(0, 0, 0, 0);
295 /// The Aqua is a predefined Color instance. It's RGB value is (0, 255, 255).
297 /// <since_tizen> preview </since_tizen>
298 public static readonly Color Aqua = FromRgb(0, 255, 255);
300 /// The Black is a predefined Color instance. It's RGB value is (0, 0, 0).
302 /// <since_tizen> preview </since_tizen>
303 public static readonly Color Black = FromRgb(0, 0, 0);
305 /// The Blue is a predefined Color instance. It's RGB value is (0, 0, 255).
307 /// <since_tizen> preview </since_tizen>
308 public static readonly Color Blue = FromRgb(0, 0, 255);
310 /// The Fuchsia is a predefined Color instance. It's RGB value is (255, 0, 255).
312 /// <since_tizen> preview </since_tizen>
313 public static readonly Color Fuchsia = FromRgb(255, 0, 255);
315 /// The Gray is a predefined Color instance. It's RGB value is (128, 128, 128).
317 /// <since_tizen> preview </since_tizen>
318 public static readonly Color Gray = FromRgb(128, 128, 128);
320 /// The Green is a predefined Color instance. It's RGB value is (0, 128, 0).
322 /// <since_tizen> preview </since_tizen>
323 public static readonly Color Green = FromRgb(0, 128, 0);
325 /// The Lime is a predefined Color instance. It's RGB value is (0, 255, 0).
327 /// <since_tizen> preview </since_tizen>
328 public static readonly Color Lime = FromRgb(0, 255, 0);
330 /// The Maroon is a predefined Color instance. It's RGB value is (128, 0, 0).
332 /// <since_tizen> preview </since_tizen>
333 public static readonly Color Maroon = FromRgb(128, 0, 0);
335 /// The Navy is a predefined Color instance. It's RGB value is (0, 0, 128).
337 /// <since_tizen> preview </since_tizen>
338 public static readonly Color Navy = FromRgb(0, 0, 128);
340 /// The Olive is a predefined Color instance. It's RGB value is (128, 128, 0).
342 /// <since_tizen> preview </since_tizen>
343 public static readonly Color Olive = FromRgb(128, 128, 0);
345 /// The Orange is a predefined Color instance. It's RGB value is (255, 165, 0).
347 /// <since_tizen> preview </since_tizen>
348 public static readonly Color Orange = FromRgb(255, 165, 0);
350 /// The Purple is a predefined Color instance. It's RGB value is (128, 0, 128).
352 /// <since_tizen> preview </since_tizen>
353 public static readonly Color Purple = FromRgb(128, 0, 128);
355 /// The Pink is a predefined Color instance. It's RGB value is (255, 102, 255).
357 /// <since_tizen> preview </since_tizen>
358 public static readonly Color Pink = FromRgb(255, 102, 255);
360 /// The Red is a predefined Color instance. It's RGB value is (255, 0, 0).
362 /// <since_tizen> preview </since_tizen>
363 public static readonly Color Red = FromRgb(255, 0, 0);
365 /// The Silver is a predefined Color instance. It's RGB value is (192, 192, 192).
367 /// <since_tizen> preview </since_tizen>
368 public static readonly Color Silver = FromRgb(192, 192, 192);
370 /// The Teal is a predefined Color instance. It's RGB value is (0, 128, 128).
372 /// <since_tizen> preview </since_tizen>
373 public static readonly Color Teal = FromRgb(0, 128, 128);
375 /// The White is a predefined Color instance. It's RGB value is (255, 255, 255).
377 /// <since_tizen> preview </since_tizen>
378 public static readonly Color White = FromRgb(255, 255, 255);
380 /// The Yellow is a predefined Color instance. It's RGB value is (255, 255, 0).
382 /// <since_tizen> preview </since_tizen>
383 public static readonly Color Yellow = FromRgb(255, 255, 0);