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.
19 namespace Tizen.Common
22 /// Structure that represents a color as RGBA.
24 public struct Color : IEquatable<Color>
27 /// Empty color instance. All components are 0.
29 public static readonly Color Empty = FromRgba(0, 0, 0, 0);
32 /// Transparent color instance. All components are 0.
34 public static readonly Color Transparent = FromRgba(0, 0, 0, 0);
37 /// Aqua color instance. Its RGB value is (0, 255, 255).
39 public static readonly Color Aqua = FromRgb(0, 255, 255);
42 /// Black color instance. Its RGB value is (0, 0, 0).
44 public static readonly Color Black = FromRgb(0, 0, 0);
47 /// Blue color instance. Its RGB value is (0, 0, 255).
49 public static readonly Color Blue = FromRgb(0, 0, 255);
52 /// Fuchsia color instance. Its RGB value is (255, 0, 255).
54 public static readonly Color Fuchsia = FromRgb(255, 0, 255);
57 /// Gray color instance. Its RGB value is (128, 128, 128).
59 public static readonly Color Gray = FromRgb(128, 128, 128);
62 /// Green color instance. Its RGB value is (0, 128, 0).
64 public static readonly Color Green = FromRgb(0, 128, 0);
67 /// Lime color instance. Its RGB value is (0, 255, 0).
69 public static readonly Color Lime = FromRgb(0, 255, 0);
72 /// Maroon color instance. Its RGB value is (128, 0, 0).
74 public static readonly Color Maroon = FromRgb(128, 0, 0);
77 /// Navy color instance. Its RGB value is (0, 0, 128).
79 public static readonly Color Navy = FromRgb(0, 0, 128);
82 /// Olive color instance. Its RGB value is (128, 128, 0).
84 public static readonly Color Olive = FromRgb(128, 128, 0);
87 /// Purple color instance. Its RGB value is (128, 0, 128).
89 public static readonly Color Purple = FromRgb(128, 0, 128);
92 /// Pink color instance. Its RGB value is (255, 102, 255).
94 public static readonly Color Pink = FromRgb(255, 102, 255);
97 /// Red color instance. Its RGB value is (255, 0, 0).
99 public static readonly Color Red = FromRgb(255, 0, 0);
102 /// Silver color instance. Its RGB value is (192, 192, 192).
104 public static readonly Color Silver = FromRgb(192, 192, 192);
107 /// Teal color instance. Its RGB value is (0, 128, 128).
109 public static readonly Color Teal = FromRgb(0, 128, 128);
112 /// White color instance. Its RGB value is (255, 255, 255).
114 public static readonly Color White = FromRgb(255, 255, 255);
117 /// Yellow color instance. Its RGB value is (255, 255, 0).
119 public static readonly Color Yellow = FromRgb(255, 255, 0);
124 /// Initiates new Color with r,g,b,a components.
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)
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");
141 _value = (int)(((uint)r << 24) + ((uint)g << 16) + ((uint)b << 8) + (uint)a);
145 /// Initiates new Color with r,g,b components. The alpha value will be 255 as default.
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)
157 /// Gets the Red component of the color.
161 get { return (byte)(_value >> 24); }
165 /// Gets the Green component of the color.
169 get { return (byte)(_value >> 16); }
173 /// Gets the blue component of the color.
177 get { return (byte)(_value >> 8); }
181 /// Gets the alpha component of the color.
185 get { return (byte)_value; }
188 #endregion // Properties
190 #region Static Methods
193 /// Returns a boolean indicating whether the two given Colors are equal.
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)
200 return color1.Equals(color2);
204 /// Returns a boolean indicating whether the two given Colors are not equal.
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)
211 return !color1.Equals(color2);
215 /// Returns a new RGB color instance.
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)
223 return new Color(r, g, b);
227 /// Returns a new RGBA color instance.
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)
236 return new Color(r, g, b, a);
240 /// Returns a new RGB Color instance with the requested Red, Green, and Blue channels. The Alpha channel is set if hex contains one.
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)
248 throw new ArgumentNullException("hex");
255 if (hex.Length > 0 && hex[0] == '#') hex = hex.Substring(1);
256 if (hex.Length == 3) hex += "F";
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";
262 throw new ArgumentException(@"Hex string is not valid color. length of hex should be 3, 4, 6, 8");
264 Color c = new Color();
265 c._value = Convert.ToInt32(hex, 16);
269 private static ArgumentException CreateColorArgumentException(int value, string color)
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));
276 #endregion // Static Methods
281 /// Gets the 32-bits RGBA value of the color.
289 /// Gets the 32-bits ARGB value of the color.
293 return (int)((uint)A << 24 | (uint)R << 16 | (uint)G << 8 | (uint)B);
297 /// Returns a string representation in Hex. (ex: \#FFFFFFFF in RGBA order)
299 /// <returns>The string representation in Hex.</returns>
300 public string ToHex()
302 return "#" + _value.ToString("X8");
306 /// Returns a boolean indicating whether the given Color is equal to this Color instance.
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)
312 return _value == other._value;
316 /// Returns a boolean indicating whether the given Object is equal to this Color instance.
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)
324 return Equals((Color)obj);
330 /// Returns a string representation of the Color.
332 /// <returns>The string representation.</returns>
333 public override string ToString()
335 return string.Format("Color [R={0}, G={1}, B={2}, A={3}]", R, G, B, A);
339 /// Returns the hash code for this instance.
341 /// <returns>The hash code.</returns>
342 public override int GetHashCode()
344 return _value.GetHashCode();
347 #endregion // Methods