Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen / Tizen.Common / 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
19 namespace Tizen.Common
20 {
21     /// <summary>
22     /// Structure that represents a color as RGBA.
23     /// </summary>
24     public struct Color : IEquatable<Color>
25     {
26         /// <summary>
27         /// Empty color instance. All components are 0.
28         /// </summary>
29         public static readonly Color Empty = FromRgba(0, 0, 0, 0);
30
31         /// <summary>
32         /// Transparent color instance. All components are 0.
33         /// </summary>
34         public static readonly Color Transparent = FromRgba(0, 0, 0, 0);
35
36         /// <summary>
37         /// Aqua color instance. Its RGB value is (0, 255, 255).
38         /// </summary>
39         public static readonly Color Aqua = FromRgb(0, 255, 255);
40
41         /// <summary>
42         /// Black color instance. Its RGB value is (0, 0, 0).
43         /// </summary>
44         public static readonly Color Black = FromRgb(0, 0, 0);
45
46         /// <summary>
47         /// Blue color instance. Its RGB value is (0, 0, 255).
48         /// </summary>
49         public static readonly Color Blue = FromRgb(0, 0, 255);
50
51         /// <summary>
52         /// Fuchsia color instance. Its RGB value is (255, 0, 255).
53         /// </summary>
54         public static readonly Color Fuchsia = FromRgb(255, 0, 255);
55
56         /// <summary>
57         /// Gray color instance. Its RGB value is (128, 128, 128).
58         /// </summary>
59         public static readonly Color Gray = FromRgb(128, 128, 128);
60
61         /// <summary>
62         /// Green color instance. Its RGB value is (0, 128, 0).
63         /// </summary>
64         public static readonly Color Green = FromRgb(0, 128, 0);
65
66         /// <summary>
67         /// Lime color instance. Its RGB value is (0, 255, 0).
68         /// </summary>
69         public static readonly Color Lime = FromRgb(0, 255, 0);
70
71         /// <summary>
72         /// Maroon color instance. Its RGB value is (128, 0, 0).
73         /// </summary>
74         public static readonly Color Maroon = FromRgb(128, 0, 0);
75
76         /// <summary>
77         /// Navy color instance. Its RGB value is (0, 0, 128).
78         /// </summary>
79         public static readonly Color Navy = FromRgb(0, 0, 128);
80
81         /// <summary>
82         /// Olive color instance. Its RGB value is (128, 128, 0).
83         /// </summary>
84         public static readonly Color Olive = FromRgb(128, 128, 0);
85
86         /// <summary>
87         /// Purple color instance. Its RGB value is (128, 0, 128).
88         /// </summary>
89         public static readonly Color Purple = FromRgb(128, 0, 128);
90
91         /// <summary>
92         /// Pink color instance. Its RGB value is (255, 102, 255).
93         /// </summary>
94         public static readonly Color Pink = FromRgb(255, 102, 255);
95
96         /// <summary>
97         /// Red color instance. Its RGB value is (255, 0, 0).
98         /// </summary>
99         public static readonly Color Red = FromRgb(255, 0, 0);
100
101         /// <summary>
102         /// Silver color instance. Its RGB value is (192, 192, 192).
103         /// </summary>
104         public static readonly Color Silver = FromRgb(192, 192, 192);
105
106         /// <summary>
107         /// Teal color instance. Its RGB value is (0, 128, 128).
108         /// </summary>
109         public static readonly Color Teal = FromRgb(0, 128, 128);
110
111         /// <summary>
112         /// White color instance. Its RGB value is (255, 255, 255).
113         /// </summary>
114         public static readonly Color White = FromRgb(255, 255, 255);
115
116         /// <summary>
117         /// Yellow color instance. Its RGB value is (255, 255, 0).
118         /// </summary>
119         public static readonly Color Yellow = FromRgb(255, 255, 0);
120
121         private int _value;
122
123         /// <summary>
124         /// Initiates new Color with r,g,b,a components.
125         /// </summary>
126         /// <param name="r">Red (0 ~ 255)</param>
127         /// <param name="g">Green (0 ~ 255)</param>
128         /// <param name="b">Blue (0 ~ 255)</param>
129         /// <param name="a">Alpha (0 ~ 255)</param>
130         public Color(int r, int g, int b, int a)
131         {
132             if (r > 255 || r < 0)
133                 throw CreateColorArgumentException(r, "red");
134             if (g > 255 || g < 0)
135                 throw CreateColorArgumentException(g, "green");
136             if (b > 255 || b < 0)
137                 throw CreateColorArgumentException(b, "blue");
138             if (a > 255 || a < 0)
139                 throw CreateColorArgumentException(a, "alpha");
140
141             _value = (int)(((uint)r << 24) + ((uint)g << 16) + ((uint)b << 8) + (uint)a);
142         }
143
144         /// <summary>
145         /// Initiates new Color with r,g,b components. The alpha value will be 255 as default.
146         /// </summary>
147         /// <param name="r">Red (0 ~ 255)</param>
148         /// <param name="g">Green (0 ~ 255)</param>
149         /// <param name="b">Blue (0 ~ 255)</param>
150         public Color(int r, int g, int b) : this(r, g, b, 255)
151         {
152         }
153
154         #region Properties
155
156         /// <summary>
157         /// Gets the Red component of the color.
158         /// </summary>
159         public int R
160         {
161             get { return (byte)(_value >> 24); }
162         }
163
164         /// <summary>
165         /// Gets the Green component of the color.
166         /// </summary>
167         public int G
168         {
169             get { return (byte)(_value >> 16); }
170         }
171
172         /// <summary>
173         /// Gets the blue component of the color.
174         /// </summary>
175         public int B
176         {
177             get { return (byte)(_value >> 8); }
178         }
179
180         /// <summary>
181         /// Gets the alpha component of the color.
182         /// </summary>
183         public int A
184         {
185             get { return (byte)_value; }
186         }
187
188         #endregion  // Properties
189
190         #region Static Methods
191
192         /// <summary>
193         /// Returns a boolean indicating whether the two given Colors are equal.
194         /// </summary>
195         /// <param name="color1">The first Color to compare.</param>
196         /// <param name="color2">The second Color to compare.</param>
197         /// <returns>True if the Colors are equal; False otherwise.</returns>
198         public static bool operator ==(Color color1, Color color2)
199         {
200             return color1.Equals(color2);
201         }
202
203         /// <summary>
204         /// Returns a boolean indicating whether the two given Colors are not equal.
205         /// </summary>
206         /// <param name="color1">The first Color to compare.</param>
207         /// <param name="color2">The second Color to compare.</param>
208         /// <returns>True if the Colors are not equal; False if they are equal.</returns>
209         public static bool operator !=(Color color1, Color color2)
210         {
211             return !color1.Equals(color2);
212         }
213
214         /// <summary>
215         /// Returns a new RGB color instance.
216         /// </summary>
217         /// <param name="r">The red component of the color.</param>
218         /// <param name="g">The green component of the color.</param>
219         /// <param name="b">The blue component of the color.</param>
220         /// <returns></returns>
221         public static Color FromRgb(int r, int g, int b)
222         {
223             return new Color(r, g, b);
224         }
225
226         /// <summary>
227         /// Returns a new RGBA color instance.
228         /// </summary>
229         /// <param name="r">The red component of the color.</param>
230         /// <param name="g">The green component of the color.</param>
231         /// <param name="b">The blue component of the color.</param>
232         /// <param name="a">The alpha component of the color.</param>
233         /// <returns>the RGBA color instance.</returns>
234         public static Color FromRgba(int r, int g, int b, int a)
235         {
236             return new Color(r, g, b, a);
237         }
238
239         /// <summary>
240         /// Returns a new RGB Color instance with the requested Red, Green, and Blue channels. The Alpha channel is set if hex contains one.
241         /// </summary>
242         /// <param name="hex">A string that contains the hexadecimal RGB(A) color representation.</param>
243         /// <returns>the RGBA color instance.</returns>
244         public static Color FromHex(string hex)
245         {
246             if (hex == null)
247             {
248                 throw new ArgumentNullException("hex");
249             }
250
251             // #fff
252             // #ffffff
253             // #ffff
254             // #ffffffff
255             if (hex.Length > 0 && hex[0] == '#') hex = hex.Substring(1);
256             if (hex.Length == 3) hex += "F";
257             if (hex.Length == 4)
258                 hex = new String(new char[] { hex[0], hex[0], hex[1], hex[1], hex[2], hex[2], hex[3], hex[3] });
259             if (hex.Length == 6) hex += "FF";
260             if (hex.Length != 8)
261             {
262                 throw new ArgumentException(@"Hex string is not valid color. length of hex should be 3, 4, 6, 8");
263             }
264             Color c = new Color();
265             c._value = Convert.ToInt32(hex, 16);
266             return c;
267         }
268
269         private static ArgumentException CreateColorArgumentException(int value, string color)
270         {
271             return new ArgumentException(string.Format("'{0}' is not a valid" +
272                         " value for '{1}'. '{1}' should be greater or equal to 0 and" +
273                         " less than or equal to 255.", value, color));
274         }
275
276         #endregion  // Static Methods
277
278         #region Methods
279
280         /// <summary>
281         /// Gets the 32-bits RGBA value of the color.
282         /// </summary>
283         public int GetRgba()
284         {
285             return _value;
286         }
287
288         /// <summary>
289         /// Gets the 32-bits ARGB value of the color.
290         /// </summary>
291         public int GetArgb()
292         {
293             return (int)((uint)A << 24 | (uint)R << 16 | (uint)G << 8 | (uint)B);
294         }
295
296         /// <summary>
297         /// Returns a string representation in Hex. (ex: \#FFFFFFFF in RGBA order)
298         /// </summary>
299         /// <returns>The string representation in Hex.</returns>
300         public string ToHex()
301         {
302             return "#" + _value.ToString("X8");
303         }
304
305         /// <summary>
306         /// Returns a boolean indicating whether the given Color is equal to this Color instance.
307         /// </summary>
308         /// <param name="other">The Color to compare this instance to.</param>
309         /// <returns>True if the other Color is equal to this instance; False otherwise.</returns>
310         public bool Equals(Color other)
311         {
312             return _value == other._value;
313         }
314
315         /// <summary>
316         /// Returns a boolean indicating whether the given Object is equal to this Color instance.
317         /// </summary>
318         /// <param name="obj">The Object to compare against.</param>
319         /// <returns>True if the Object is equal to this Color; False otherwise.</returns>
320         public override bool Equals(object obj)
321         {
322             if (obj is Color)
323             {
324                 return Equals((Color)obj);
325             }
326             return false;
327         }
328
329         /// <summary>
330         /// Returns a string representation of the Color.
331         /// </summary>
332         /// <returns>The string representation.</returns>
333         public override string ToString()
334         {
335             return string.Format("Color [R={0}, G={1}, B={2}, A={3}]", R, G, B, A);
336         }
337
338         /// <summary>
339         /// Returns the hash code for this instance.
340         /// </summary>
341         /// <returns>The hash code.</returns>
342         public override int GetHashCode()
343         {
344             return _value.GetHashCode();
345         }
346
347         #endregion  // Methods
348     }
349 }