[NUI] TCSACR-226 code change (#1032)
[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 the 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 the default Color instance, the mode type is default with 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         /// The 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 the 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 the 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 the 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 the 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 the object and this instance are of 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         /// Compares whether the two Color instances are 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 the two instances are the 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             return EqualsInner(a, b);
188         }
189
190         /// <summary>
191         /// Compares whether the two Color instances are different or not.
192         /// </summary>
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.
197         /// </returns>
198         /// <since_tizen> preview </since_tizen>
199         public static bool operator !=(Color a, Color b)
200         {
201             return !(a == b);
202         }
203
204         static bool EqualsInner(Color color1, Color color2)
205         {
206             if (color1._mode == Mode.Default && color2._mode == Mode.Default)
207                 return true;
208             return color1._r == color2._r && color1._g == color2._g && color1._b == color2._b && color1._a == color2._a;
209         }
210
211         /// <summary>
212         /// Returns the fully qualified type name of this instance.
213         /// </summary>
214         /// <returns>The fully qualified type name.</returns>
215         /// <since_tizen> preview </since_tizen>
216         public override string ToString()
217         {
218             return string.Format(CultureInfo.InvariantCulture, "[Color: R={0}, G={1}, B={2}, A={3}]", R, G, B, A);
219         }
220
221         /// <summary>
222         /// Gets a Color instance with a hexadecimal string parameter.
223         /// </summary>
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)
228         {
229             string ret = hex.Replace("#", "");
230             switch (ret.Length)
231             {
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]);
234                     break;
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]);
237                     break;
238                 case 6: //#rrggbb => ffrrggbb
239                     ret = string.Format("ff{0}", ret);
240                     break;
241             }
242             return FromUint(Convert.ToUInt32(ret.Replace("#", ""), 16));
243         }
244
245         /// <summary>
246         /// Gets a Color instance with an unsigned integer parameter.
247         /// </summary>
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)
252         {
253             return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
254         }
255
256         /// <summary>
257         /// Gets a Color instance with R,G,B,A parameters.
258         /// </summary>
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)
266         {
267             return new Color(r, g, b, a);
268         }
269
270         /// <summary>
271         /// Gets a Color instance with R,G,B parameters.
272         /// </summary>
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)
279         {
280             return FromRgba(r, g, b, 255);
281         }
282
283         internal static int Clamp(int self, int min, int max)
284         {
285             return Math.Min(max, Math.Max(self, min));
286         }
287
288         #region Color Definitions
289         /// <summary>
290         /// The Tansparent is a predefined Color instance. It's RGBA value is (0, 0, 0, 0).
291         /// </summary>
292         /// <since_tizen> preview </since_tizen>
293         public static readonly Color Transparent = FromRgba(0, 0, 0, 0);
294         /// <summary>
295         /// The Aqua is a predefined Color instance. It's RGB value is (0, 255, 255).
296         /// </summary>
297         /// <since_tizen> preview </since_tizen>
298         public static readonly Color Aqua = FromRgb(0, 255, 255);
299         /// <summary>
300         /// The Black is a predefined Color instance. It's RGB value is (0, 0, 0).
301         /// </summary>
302         /// <since_tizen> preview </since_tizen>
303         public static readonly Color Black = FromRgb(0, 0, 0);
304         /// <summary>
305         /// The Blue is a predefined Color instance. It's RGB value is (0, 0, 255).
306         /// </summary>
307         /// <since_tizen> preview </since_tizen>
308         public static readonly Color Blue = FromRgb(0, 0, 255);
309         /// <summary>
310         /// The Fuchsia is a predefined Color instance. It's RGB value is (255, 0, 255).
311         /// </summary>
312         /// <since_tizen> preview </since_tizen>
313         public static readonly Color Fuchsia = FromRgb(255, 0, 255);
314         /// <summary>
315         /// The Gray is a predefined Color instance. It's RGB value is (128, 128, 128).
316         /// </summary>
317         /// <since_tizen> preview </since_tizen>
318         public static readonly Color Gray = FromRgb(128, 128, 128);
319         /// <summary>
320         /// The Green is a predefined Color instance. It's RGB value is (0, 128, 0).
321         /// </summary>
322         /// <since_tizen> preview </since_tizen>
323         public static readonly Color Green = FromRgb(0, 128, 0);
324         /// <summary>
325         /// The Lime is a predefined Color instance. It's RGB value is (0, 255, 0).
326         /// </summary>
327         /// <since_tizen> preview </since_tizen>
328         public static readonly Color Lime = FromRgb(0, 255, 0);
329         /// <summary>
330         /// The Maroon is a predefined Color instance. It's RGB value is (128, 0, 0).
331         /// </summary>
332         /// <since_tizen> preview </since_tizen>
333         public static readonly Color Maroon = FromRgb(128, 0, 0);
334         /// <summary>
335         /// The Navy is a predefined Color instance. It's RGB value is (0, 0, 128).
336         /// </summary>
337         /// <since_tizen> preview </since_tizen>
338         public static readonly Color Navy = FromRgb(0, 0, 128);
339         /// <summary>
340         /// The Olive is a predefined Color instance. It's RGB value is (128, 128, 0).
341         /// </summary>
342         /// <since_tizen> preview </since_tizen>
343         public static readonly Color Olive = FromRgb(128, 128, 0);
344         /// <summary>
345         /// The Orange is a predefined Color instance. It's RGB value is (255, 165, 0).
346         /// </summary>
347         /// <since_tizen> preview </since_tizen>
348         public static readonly Color Orange = FromRgb(255, 165, 0);
349         /// <summary>
350         /// The Purple is a predefined Color instance. It's RGB value is (128, 0, 128).
351         /// </summary>
352         /// <since_tizen> preview </since_tizen>
353         public static readonly Color Purple = FromRgb(128, 0, 128);
354         /// <summary>
355         /// The Pink is a predefined Color instance. It's RGB value is (255, 102, 255).
356         /// </summary>
357         /// <since_tizen> preview </since_tizen>
358         public static readonly Color Pink = FromRgb(255, 102, 255);
359         /// <summary>
360         /// The Red is a predefined Color instance. It's RGB value is (255, 0, 0).
361         /// </summary>
362         /// <since_tizen> preview </since_tizen>
363         public static readonly Color Red = FromRgb(255, 0, 0);
364         /// <summary>
365         /// The Silver is a predefined Color instance. It's RGB value is (192, 192, 192).
366         /// </summary>
367         /// <since_tizen> preview </since_tizen>
368         public static readonly Color Silver = FromRgb(192, 192, 192);
369         /// <summary>
370         /// The Teal is a predefined Color instance. It's RGB value is (0, 128, 128).
371         /// </summary>
372         /// <since_tizen> preview </since_tizen>
373         public static readonly Color Teal = FromRgb(0, 128, 128);
374         /// <summary>
375         /// The White is a predefined Color instance. It's RGB value is (255, 255, 255).
376         /// </summary>
377         /// <since_tizen> preview </since_tizen>
378         public static readonly Color White = FromRgb(255, 255, 255);
379         /// <summary>
380         /// The Yellow is a predefined Color instance. It's RGB value is (255, 255, 0).
381         /// </summary>
382         /// <since_tizen> preview </since_tizen>
383         public static readonly Color Yellow = FromRgb(255, 255, 0);
384         #endregion
385     }
386 }