Merge "[WiFi] GetConnectedAP() Returns null if there is no connected AP"
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Color.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Globalization;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// The Color is a struct to record Check's state.
24     /// </summary>
25     /// <since_tizen> preview </since_tizen>
26     public struct Color
27     {
28         readonly int _a;
29         readonly int _r;
30         readonly int _g;
31         readonly int _b;
32
33         readonly Mode _mode;
34
35         enum Mode
36         {
37             Default,
38             Rgb
39         }
40
41         /// <summary>
42         /// Gets a default Color instance.
43         /// </summary>
44         /// <remarks>
45         /// In default Color instance,Mode type is Default,RGBA all set as -1.
46         /// </remarks>
47         /// <since_tizen> preview </since_tizen>
48         public static Color Default
49         {
50             get { return new Color(-1, -1, -1, -1, Mode.Default); }
51         }
52
53         /// <summary>
54         /// Gets whether the Color instance's mode is default or not.
55         /// Return type is bool.
56         /// </summary>
57         /// <since_tizen> preview </since_tizen>
58         public bool IsDefault
59         {
60             get { return _mode == Mode.Default; }
61         }
62
63         /// <summary>
64         /// Gets A value of RGBA.
65         /// A means the Alpha in color.
66         /// </summary>
67         /// <since_tizen> preview </since_tizen>
68         public int A
69         {
70             get { return _a; }
71         }
72
73         /// <summary>
74         /// Gets R value of RGBA.
75         /// R means the Red in color.
76         /// </summary>
77         /// <since_tizen> preview </since_tizen>
78         public int R
79         {
80             get { return _r; }
81         }
82
83         /// <summary>
84         /// Gets G value of RGBA.
85         /// G means the Green in color.
86         /// </summary>
87         /// <since_tizen> preview </since_tizen>
88         public int G
89         {
90             get { return _g; }
91         }
92
93         /// <summary>
94         /// Gets B value of RGBA.
95         /// B means the Blue in color.
96         /// </summary>
97         /// <since_tizen> preview </since_tizen>
98         public int B
99         {
100             get { return _b; }
101         }
102
103         /// <summary>
104         /// Creates and initializes a new instance of the Color class.
105         /// With RGB parameters.
106         /// </summary>
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)
112         {
113         }
114
115         /// <summary>
116         /// Creates and initializes a new instance of the Color class.
117         /// With RGBA parameters.
118         /// </summary>
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)
125         {
126         }
127
128         Color(int r, int g, int b, int a, Mode mode)
129         {
130             _mode = mode;
131             if (mode == Mode.Rgb)
132             {
133                 _r = Clamp(r, 0, 255);
134                 _g = Clamp(g, 0, 255);
135                 _b = Clamp(b, 0, 255);
136                 _a = Clamp(a, 0, 255);
137             }
138             else // Default
139             {
140                 _r = _g = _b = _a = -1;
141             }
142         }
143
144         /// <summary>
145         /// Returns the hash code for this instance.
146         /// </summary>
147         /// <returns>A 32-bit signed integer hash code.</returns>
148         /// <since_tizen> preview </since_tizen>
149         public override int GetHashCode()
150         {
151             int hashcode = _r.GetHashCode();
152             hashcode = (hashcode * 397) ^ _g.GetHashCode();
153             hashcode = (hashcode * 397) ^ _b.GetHashCode();
154             hashcode = (hashcode * 397) ^ _a.GetHashCode();
155             return hashcode;
156         }
157
158         /// <summary>
159         /// Indicates whether this instance and a specified object are equal.
160         /// </summary>
161         /// <param name="obj">The object to compare with the current instance.</param>
162         /// <returns>
163         /// true if obj and this instance are the same type and represent the same value.
164         /// otherwise, false.
165         /// </returns>
166         /// <since_tizen> preview </since_tizen>
167         public override bool Equals(object obj)
168         {
169             if (obj is Color)
170             {
171                 return EqualsInner(this, (Color)obj);
172             }
173             return base.Equals(obj);
174         }
175
176         /// <summary>
177         /// Compare whether two Color instance is same or not.
178         /// </summary>
179         /// <param name="a">A Color instance.</param>
180         /// <param name="b">A Color instance.</param>
181         /// <returns>The result whether two instance is same or not.
182         /// Return type is bool.If they are same, return true.
183         /// </returns>
184         /// <since_tizen> preview </since_tizen>
185         public static bool operator ==(Color a, Color b)
186         {
187             if (ReferenceEquals(a, b))
188                 return true;
189
190             if ((object)a == null || (object)b == null)
191                 return false;
192
193             return EqualsInner(a, b);
194         }
195
196         /// <summary>
197         /// Compare whether two Color instance is different or not.
198         /// </summary>
199         /// <param name="a">A Color instance.</param>
200         /// <param name="b">A Color instance.</param>
201         /// <returns>The result whether two instance is different or not.
202         /// Return type is bool.If they are different, return true.
203         /// </returns>
204         /// <since_tizen> preview </since_tizen>
205         public static bool operator !=(Color a, Color b)
206         {
207             return !(a == b);
208         }
209
210         static bool EqualsInner(Color color1, Color color2)
211         {
212             if (color1._mode == Mode.Default && color2._mode == Mode.Default)
213                 return true;
214             return color1._r == color2._r && color1._g == color2._g && color1._b == color2._b && color1._a == color2._a;
215         }
216
217         /// <summary>
218         /// Returns the fully qualified type name of this instance.
219         /// </summary>
220         /// <returns>The fully qualified type name.</returns>
221         /// <since_tizen> preview </since_tizen>
222         public override string ToString()
223         {
224             return string.Format(CultureInfo.InvariantCulture, "[Color: R={0}, G={1}, B={2}, A={3}]", R, G, B, A);
225         }
226
227         /// <summary>
228         /// Gets a Color instance with a hexadecimal string parameter.
229         /// </summary>
230         /// <param name="hex">Hexadecimal string.</param>
231         /// <returns>New instance of Color struct.</returns>
232         /// <since_tizen> preview </since_tizen>
233         public static Color FromHex(string hex)
234         {
235             hex = hex.Replace("#", "");
236             switch (hex.Length)
237             {
238                 case 3: //#rgb => ffrrggbb
239                     hex = string.Format("ff{0}{1}{2}{3}{4}{5}", hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]);
240                     break;
241                 case 4: //#argb => aarrggbb
242                     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]);
243                     break;
244                 case 6: //#rrggbb => ffrrggbb
245                     hex = string.Format("ff{0}", hex);
246                     break;
247             }
248             return FromUint(Convert.ToUInt32(hex.Replace("#", ""), 16));
249         }
250
251         /// <summary>
252         /// Gets a Color instance with a Unsigned integer parameter.
253         /// </summary>
254         /// <param name="argb">Unsigned integer indicates RGBA.</param>
255         /// <returns>New instance of Color struct.</returns>
256         /// <since_tizen> preview </since_tizen>
257         public static Color FromUint(uint argb)
258         {
259             return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
260         }
261
262         /// <summary>
263         /// Gets a Color instance with R,G,B,A parameters.
264         /// </summary>
265         /// <param name="r">Red in RGBA.</param>
266         /// <param name="g">Green in RGBA.</param>
267         /// <param name="b">Blue in RGBA.</param>
268         /// <param name="a">Alpha in RGBA.</param>
269         /// <returns>New instance of Color struct.</returns>
270         /// <since_tizen> preview </since_tizen>
271         public static Color FromRgba(int r, int g, int b, int a)
272         {
273             return new Color(r, g, b, a);
274         }
275
276         /// <summary>
277         /// Gets a Color instance with R,G,B,A parameters.
278         /// </summary>
279         /// <param name="r">Red in RGB.</param>
280         /// <param name="g">Green in RGB.</param>
281         /// <param name="b">Blue in RGB.</param>
282         /// <returns>New instance of Color struct.</returns>
283         /// <since_tizen> preview </since_tizen>
284         public static Color FromRgb(int r, int g, int b)
285         {
286             return FromRgba(r, g, b, 255);
287         }
288
289         internal static int Clamp(int self, int min, int max)
290         {
291             return Math.Min(max, Math.Max(self, min));
292         }
293
294         #region Color Definitions
295         /// <summary>
296         /// The Tansparent is a predefined Color, it's rgba value is (0, 0, 0, 0).
297         /// </summary>
298         /// <since_tizen> preview </since_tizen>
299         public static readonly Color Transparent = FromRgba(0, 0, 0, 0);
300         /// <summary>
301         /// The Aqua is a predefined Color instance, it's rgb value is (0, 255, 255).
302         /// </summary>
303         /// <since_tizen> preview </since_tizen>
304         public static readonly Color Aqua = FromRgb(0, 255, 255);
305         /// <summary>
306         /// The Black is a predefined Color instance, it's rgb value is (0, 0, 0).
307         /// </summary>
308         /// <since_tizen> preview </since_tizen>
309         public static readonly Color Black = FromRgb(0, 0, 0);
310         /// <summary>
311         /// The Blue is a predefined Color instance, it's rgb value is (0, 0, 255).
312         /// </summary>
313         /// <since_tizen> preview </since_tizen>
314         public static readonly Color Blue = FromRgb(0, 0, 255);
315         /// <summary>
316         /// The Fuchsia is a predefined Color instance, it's rgb value is (255, 0, 255).
317         /// </summary>
318         /// <since_tizen> preview </since_tizen>
319         public static readonly Color Fuchsia = FromRgb(255, 0, 255);
320         /// <summary>
321         /// The Gray is a predefined Color instance, it's rgb value is (128, 128, 128).
322         /// </summary>
323         /// <since_tizen> preview </since_tizen>
324         public static readonly Color Gray = FromRgb(128, 128, 128);
325         /// <summary>
326         /// The Green is a predefined Color instance, it's rgb value is (0, 128, 0).
327         /// </summary>
328         /// <since_tizen> preview </since_tizen>
329         public static readonly Color Green = FromRgb(0, 128, 0);
330         /// <summary>
331         /// The Lime is a predefined Color instance, it's rgb value is (0, 255, 0).
332         /// </summary>
333         /// <since_tizen> preview </since_tizen>
334         public static readonly Color Lime = FromRgb(0, 255, 0);
335         /// <summary>
336         /// The Maroon is a predefined Color instance, it's rgb value is (128, 0, 0).
337         /// </summary>
338         /// <since_tizen> preview </since_tizen>
339         public static readonly Color Maroon = FromRgb(128, 0, 0);
340         /// <summary>
341         /// The Navy is a predefined Color instance, it's rgb value is (0, 0, 128).
342         /// </summary>
343         /// <since_tizen> preview </since_tizen>
344         public static readonly Color Navy = FromRgb(0, 0, 128);
345         /// <summary>
346         /// The Olive is a predefined Color instance, it's rgb value is (128, 128, 0).
347         /// </summary>
348         /// <since_tizen> preview </since_tizen>
349         public static readonly Color Olive = FromRgb(128, 128, 0);
350         /// <summary>
351         /// The Orange is a predefined Color instance, it's rgb value is (255, 165, 0).
352         /// </summary>
353         /// <since_tizen> preview </since_tizen>
354         public static readonly Color Orange = FromRgb(255, 165, 0);
355         /// <summary>
356         /// The Purple is a predefined Color instance, it's rgb value is (128, 0, 128).
357         /// </summary>
358         /// <since_tizen> preview </since_tizen>
359         public static readonly Color Purple = FromRgb(128, 0, 128);
360         /// <summary>
361         /// The Pink is a predefined Color instance, it's rgb value is (255, 102, 255).
362         /// </summary>
363         /// <since_tizen> preview </since_tizen>
364         public static readonly Color Pink = FromRgb(255, 102, 255);
365         /// <summary>
366         /// The Red is a predefined Color instance, it's rgb value is (255, 0, 0).
367         /// </summary>
368         /// <since_tizen> preview </since_tizen>
369         public static readonly Color Red = FromRgb(255, 0, 0);
370         /// <summary>
371         /// The Silver is a predefined Color instance, it's rgb value is (192, 192, 192).
372         /// </summary>
373         /// <since_tizen> preview </since_tizen>
374         public static readonly Color Silver = FromRgb(192, 192, 192);
375         /// <summary>
376         /// The Teal is a predefined Color instance, it's rgb value is (0, 128, 128).
377         /// </summary>
378         /// <since_tizen> preview </since_tizen>
379         public static readonly Color Teal = FromRgb(0, 128, 128);
380         /// <summary>
381         /// The White is a predefined Color instance, it's rgb value is (255, 255, 255).
382         /// </summary>
383         /// <since_tizen> preview </since_tizen>
384         public static readonly Color White = FromRgb(255, 255, 255);
385         /// <summary>
386         /// The Yellow is a predefined Color instance, it's rgb value is (255, 255, 0).
387         /// </summary>
388         /// <since_tizen> preview </since_tizen>
389         public static readonly Color Yellow = FromRgb(255, 255, 0);
390         #endregion
391     }
392 }