Release 4.0.0-preview1-00051
[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     public struct Color
26     {
27         readonly int _a;
28         readonly int _r;
29         readonly int _g;
30         readonly int _b;
31
32         readonly Mode _mode;
33
34         enum Mode
35         {
36             Default,
37             Rgb
38         }
39
40         /// <summary>
41         /// Gets a default Color instance.
42         /// </summary>
43         /// <remarks>
44         /// In default Color instance,Mode type is Default,RGBA all set as -1.
45         /// </remarks>
46         public static Color Default
47         {
48             get { return new Color(-1, -1, -1, -1, Mode.Default); }
49         }
50
51         /// <summary>
52         /// Gets whether the Color instance's mode is default or not.
53         /// Return type is bool.
54         /// </summary>
55         public bool IsDefault
56         {
57             get { return _mode == Mode.Default; }
58         }
59
60         /// <summary>
61         /// Gets A value of RGBA.
62         /// A means the Alpha in color.
63         /// </summary>
64         public int A
65         {
66             get { return _a; }
67         }
68
69         /// <summary>
70         /// Gets R value of RGBA.
71         /// R means the Red in color.
72         /// </summary>
73         public int R
74         {
75             get { return _r; }
76         }
77
78         /// <summary>
79         /// Gets G value of RGBA.
80         /// G means the Green in color.
81         /// </summary>
82         public int G
83         {
84             get { return _g; }
85         }
86
87         /// <summary>
88         /// Gets B value of RGBA.
89         /// B means the Blue in color.
90         /// </summary>
91         public int B
92         {
93             get { return _b; }
94         }
95
96         /// <summary>
97         /// Creates and initializes a new instance of the Color class.
98         /// With RGB parameters.
99         /// </summary>
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)
104         {
105         }
106
107         /// <summary>
108         /// Creates and initializes a new instance of the Color class.
109         /// With RGBA parameters.
110         /// </summary>
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)
116         {
117         }
118
119         Color(int r, int g, int b, int a, Mode mode)
120         {
121             _mode = mode;
122             if (mode == Mode.Rgb)
123             {
124                 _r = Clamp(r, 0, 255);
125                 _g = Clamp(g, 0, 255);
126                 _b = Clamp(b, 0, 255);
127                 _a = Clamp(a, 0, 255);
128             }
129             else // Default
130             {
131                 _r = _g = _b = _a = -1;
132             }
133         }
134
135         public override int GetHashCode()
136         {
137             int hashcode = _r.GetHashCode();
138             hashcode = (hashcode * 397) ^ _g.GetHashCode();
139             hashcode = (hashcode * 397) ^ _b.GetHashCode();
140             hashcode = (hashcode * 397) ^ _a.GetHashCode();
141             return hashcode;
142         }
143
144         public override bool Equals(object obj)
145         {
146             if (obj is Color)
147             {
148                 return EqualsInner(this, (Color)obj);
149             }
150             return base.Equals(obj);
151         }
152
153         /// <summary>
154         /// Compare whether two Color instance is same or not.
155         /// </summary>
156         /// <param name="a">A Color instance.</param>
157         /// <param name="b">A Color instance.</param>
158         /// <returns>The result whether two instance is same or not.
159         /// Return type is bool.If they are same, return true.
160         /// </returns>
161         public static bool operator ==(Color a, Color b)
162         {
163             if (ReferenceEquals(a, b))
164                 return true;
165
166             if ((object)a == null || (object)b == null)
167                 return false;
168
169             return EqualsInner(a, b);
170         }
171
172         /// <summary>
173         /// Compare whether two Color instance is different or not.
174         /// </summary>
175         /// <param name="a">A Color instance.</param>
176         /// <param name="b">A Color instance.</param>
177         /// <returns>The result whether two instance is different or not.
178         /// Return type is bool.If they are different, return true.
179         /// </returns>
180         public static bool operator !=(Color a, Color b)
181         {
182             return !(a == b);
183         }
184
185         static bool EqualsInner(Color color1, Color color2)
186         {
187             if (color1._mode == Mode.Default && color2._mode == Mode.Default)
188                 return true;
189             return color1._r == color2._r && color1._g == color2._g && color1._b == color2._b && color1._a == color2._a;
190         }
191
192         public override string ToString()
193         {
194             return string.Format(CultureInfo.InvariantCulture, "[Color: R={0}, G={1}, B={2}, A={3}]", R, G, B, A);
195         }
196
197         /// <summary>
198         /// Gets a Color instance with a hexadecimal string parameter.
199         /// </summary>
200         /// <param name="hex">Hexadecimal string.</param>
201         /// <returns>New instance of Color struct.</returns>
202         public static Color FromHex(string hex)
203         {
204             hex = hex.Replace("#", "");
205             switch (hex.Length)
206             {
207                 case 3: //#rgb => ffrrggbb
208                     hex = string.Format("ff{0}{1}{2}{3}{4}{5}", hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]);
209                     break;
210                 case 4: //#argb => aarrggbb
211                     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]);
212                     break;
213                 case 6: //#rrggbb => ffrrggbb
214                     hex = string.Format("ff{0}", hex);
215                     break;
216             }
217             return FromUint(Convert.ToUInt32(hex.Replace("#", ""), 16));
218         }
219
220         /// <summary>
221         /// Gets a Color instance with a Unsigned integer parameter.
222         /// </summary>
223         /// <param name="argb">Unsigned integer indicates RGBA.</param>
224         /// <returns>New instance of Color struct.</returns>
225         public static Color FromUint(uint argb)
226         {
227             return FromRgba((byte)((argb & 0x00ff0000) >> 0x10), (byte)((argb & 0x0000ff00) >> 0x8), (byte)(argb & 0x000000ff), (byte)((argb & 0xff000000) >> 0x18));
228         }
229
230         /// <summary>
231         /// Gets a Color instance with R,G,B,A parameters.
232         /// </summary>
233         /// <param name="r">Red in RGBA.</param>
234         /// <param name="g">Green in RGBA.</param>
235         /// <param name="b">Blue in RGBA.</param>
236         /// <param name="a">Alpha in RGBA.</param>
237         /// <returns>New instance of Color struct.</returns>
238         public static Color FromRgba(int r, int g, int b, int a)
239         {
240             return new Color(r, g, b, a);
241         }
242
243         /// <summary>
244         /// Gets a Color instance with R,G,B,A parameters.
245         /// </summary>
246         /// <param name="r">Red in RGB.</param>
247         /// <param name="g">Green in RGB.</param>
248         /// <param name="b">Blue in RGB.</param>
249         /// <returns>New instance of Color struct.</returns>
250         public static Color FromRgb(int r, int g, int b)
251         {
252             return FromRgba(r, g, b, 255);
253         }
254
255         internal static int Clamp(int self, int min, int max)
256         {
257             return Math.Min(max, Math.Max(self, min));
258         }
259
260         #region Color Definitions
261         /// <summary>
262         /// The Tansparent is a predefined Color, it's rgba value is (0, 0, 0, 0).
263         /// </summary>
264         public static readonly Color Transparent = FromRgba(0, 0, 0, 0);
265         /// <summary>
266         /// The Aqua is a predefined Color instance, it's rgb value is (0, 255, 255).
267         /// </summary>
268         public static readonly Color Aqua = FromRgb(0, 255, 255);
269         /// <summary>
270         /// The Black is a predefined Color instance, it's rgb value is (0, 0, 0).
271         /// </summary>
272         public static readonly Color Black = FromRgb(0, 0, 0);
273         /// <summary>
274         /// The Blue is a predefined Color instance, it's rgb value is (0, 0, 255).
275         /// </summary>
276         public static readonly Color Blue = FromRgb(0, 0, 255);
277         /// <summary>
278         /// The Fuchsia is a predefined Color instance, it's rgb value is (255, 0, 255).
279         /// </summary>
280         public static readonly Color Fuchsia = FromRgb(255, 0, 255);
281         /// <summary>
282         /// The Gray is a predefined Color instance, it's rgb value is (128, 128, 128).
283         /// </summary>
284         public static readonly Color Gray = FromRgb(128, 128, 128);
285         /// <summary>
286         /// The Green is a predefined Color instance, it's rgb value is (0, 128, 0).
287         /// </summary>
288         public static readonly Color Green = FromRgb(0, 128, 0);
289         /// <summary>
290         /// The Lime is a predefined Color instance, it's rgb value is (0, 255, 0).
291         /// </summary>
292         public static readonly Color Lime = FromRgb(0, 255, 0);
293         /// <summary>
294         /// The Maroon is a predefined Color instance, it's rgb value is (128, 0, 0).
295         /// </summary>
296         public static readonly Color Maroon = FromRgb(128, 0, 0);
297         /// <summary>
298         /// The Navy is a predefined Color instance, it's rgb value is (0, 0, 128).
299         /// </summary>
300         public static readonly Color Navy = FromRgb(0, 0, 128);
301         /// <summary>
302         /// The Olive is a predefined Color instance, it's rgb value is (128, 128, 0).
303         /// </summary>
304         public static readonly Color Olive = FromRgb(128, 128, 0);
305         /// <summary>
306         /// The Orange is a predefined Color instance, it's rgb value is (255, 165, 0).
307         /// </summary>
308         public static readonly Color Orange = FromRgb(255, 165, 0);
309         /// <summary>
310         /// The Purple is a predefined Color instance, it's rgb value is (128, 0, 128).
311         /// </summary>
312         public static readonly Color Purple = FromRgb(128, 0, 128);
313         /// <summary>
314         /// The Pink is a predefined Color instance, it's rgb value is (255, 102, 255).
315         /// </summary>
316         public static readonly Color Pink = FromRgb(255, 102, 255);
317         /// <summary>
318         /// The Red is a predefined Color instance, it's rgb value is (255, 0, 0).
319         /// </summary>
320         public static readonly Color Red = FromRgb(255, 0, 0);
321         /// <summary>
322         /// The Silver is a predefined Color instance, it's rgb value is (192, 192, 192).
323         /// </summary>
324         public static readonly Color Silver = FromRgb(192, 192, 192);
325         /// <summary>
326         /// The Teal is a predefined Color instance, it's rgb value is (0, 128, 128).
327         /// </summary>
328         public static readonly Color Teal = FromRgb(0, 128, 128);
329         /// <summary>
330         /// The White is a predefined Color instance, it's rgb value is (255, 255, 255).
331         /// </summary>
332         public static readonly Color White = FromRgb(255, 255, 255);
333         /// <summary>
334         /// The Yellow is a predefined Color instance, it's rgb value is (255, 255, 0).
335         /// </summary>
336         public static readonly Color Yellow = FromRgb(255, 255, 0);
337         #endregion
338     }
339 }