1+ // --------------------------------------------------------------------------------------------------------------------
2+ // <copyright file="Color.cs" company="MADE Apps">
3+ // Copyright (c) MADE Apps.
4+ // </copyright>
5+ // <summary>
6+ // Defines a model for a UI element color.
7+ // </summary>
8+ // --------------------------------------------------------------------------------------------------------------------
9+
10+ namespace MADE . App . Design . Color
11+ {
12+ /// <summary>
13+ /// Defines a model for a UI element color.
14+ /// </summary>
15+ public class Color : IColor
16+ {
17+ /// <summary>
18+ /// Initializes a new instance of the <see cref="Color"/> class with set ARGB byte values.
19+ /// </summary>
20+ /// <param name="a">
21+ /// The alpha value.
22+ /// </param>
23+ /// <param name="r">
24+ /// The red value.
25+ /// </param>
26+ /// <param name="g">
27+ /// The green value.
28+ /// </param>
29+ /// <param name="b">
30+ /// The blue value.
31+ /// </param>
32+ internal Color ( byte a , byte r , byte g , byte b )
33+ {
34+ this . A = a ;
35+ this . R = r ;
36+ this . G = g ;
37+ this . B = b ;
38+ }
39+
40+ #if WINDOWS_UWP || __ANDROID__
41+ /// <summary>
42+ /// Initializes a new instance of the <see cref="Color"/> class with a <see cref="System.Drawing.Color"/>.
43+ /// </summary>
44+ /// <param name="color">
45+ /// The system color value.
46+ /// </param>
47+ public Color ( System . Drawing . Color color )
48+ {
49+ this . A = color . A ;
50+ this . R = color . R ;
51+ this . G = color . G ;
52+ this . B = color . B ;
53+ }
54+
55+ /// <summary>
56+ /// Supports the conversion of a <see cref="System.Drawing.Color"/> to <see cref="Color"/> implicitly.
57+ /// </summary>
58+ /// <param name="color">
59+ /// The system color value.
60+ /// </param>
61+ public static implicit operator Color ( System . Drawing . Color color )
62+ {
63+ return new Color ( color ) ;
64+ }
65+
66+ /// <summary>
67+ /// Supports the conversion of a <see cref="Color"/> to <see cref="System.Drawing.Color"/> implicitly.
68+ /// </summary>
69+ /// <param name="color">
70+ /// The internal color value.
71+ /// </param>
72+ public static implicit operator System . Drawing . Color ( Color color )
73+ {
74+ return System . Drawing . Color . FromArgb ( color . A , color . R , color . G , color . B ) ;
75+ }
76+ #endif
77+
78+ #if __ANDROID__
79+ /// <summary>
80+ /// Initializes a new instance of the <see cref="Color"/> class with a <see cref="Android.Graphics.Color"/>.
81+ /// </summary>
82+ /// <param name="color">
83+ /// The Android color value.
84+ /// </param>
85+ public Color ( Android . Graphics . Color color )
86+ {
87+ this . A = color . A ;
88+ this . R = color . R ;
89+ this . G = color . G ;
90+ this . B = color . B ;
91+ }
92+
93+ /// <summary>
94+ /// Supports the conversion of a <see cref="Android.Graphics.Color"/> to <see cref="Color"/> implicitly.
95+ /// </summary>
96+ /// <param name="color">
97+ /// The Android color value.
98+ /// </param>
99+ public static implicit operator Color ( Android . Graphics . Color color )
100+ {
101+ return new Color ( color ) ;
102+ }
103+
104+ /// <summary>
105+ /// Supports the conversion of a <see cref="Color"/> to <see cref="Android.Graphics.Color"/> implicitly.
106+ /// </summary>
107+ /// <param name="color">
108+ /// The internal color value.
109+ /// </param>
110+ public static implicit operator Android . Graphics . Color ( Color color )
111+ {
112+ return new Android . Graphics . Color ( color . R , color . G , color . B , color . A ) ;
113+ }
114+ #endif
115+
116+ #if WINDOWS_UWP
117+ /// <summary>
118+ /// Initializes a new instance of the <see cref="Color"/> class with a <see cref="Windows.UI.Color"/>.
119+ /// </summary>
120+ /// <param name="color">
121+ /// The Windows color value.
122+ /// </param>
123+ public Color ( Windows . UI . Color color )
124+ {
125+ this . A = color . A ;
126+ this . R = color . R ;
127+ this . G = color . G ;
128+ this . B = color . B ;
129+ }
130+
131+ /// <summary>
132+ /// Supports the conversion of a <see cref="Windows.UI.Color"/> to <see cref="Color"/> implicitly.
133+ /// </summary>
134+ /// <param name="color">
135+ /// The Windows color value.
136+ /// </param>
137+ public static implicit operator Color ( Windows . UI . Color color )
138+ {
139+ return new Color ( color ) ;
140+ }
141+
142+ /// <summary>
143+ /// Supports the conversion of a <see cref="Windows.UI.Xaml.Media.SolidColorBrush"/> to <see cref="Color"/> implicitly.
144+ /// </summary>
145+ /// <param name="colorBrush">
146+ /// The Windows solid color brush value.
147+ /// </param>
148+ public static implicit operator Color ( Windows . UI . Xaml . Media . SolidColorBrush colorBrush )
149+ {
150+ return new Color ( colorBrush . Color ) ;
151+ }
152+
153+ /// <summary>
154+ /// Supports the conversion of a <see cref="Color"/> to <see cref="Windows.UI.Color"/> implicitly.
155+ /// </summary>
156+ /// <param name="color">
157+ /// The internal color value.
158+ /// </param>
159+ public static implicit operator Windows . UI . Color ( Color color )
160+ {
161+ return Windows . UI . Color . FromArgb ( color . A , color . R , color . G , color . B ) ;
162+ }
163+
164+ /// <summary>
165+ /// Supports the conversion of a <see cref="Color"/> to <see cref="Windows.UI.Xaml.Media.SolidColorBrush"/> implicitly.
166+ /// </summary>
167+ /// <param name="color">
168+ /// The internal color value.
169+ /// </param>
170+ public static implicit operator Windows . UI . Xaml . Media . SolidColorBrush ( Color color )
171+ {
172+ return new Windows . UI . Xaml . Media . SolidColorBrush ( color ) ;
173+ }
174+ #endif
175+
176+ #if __IOS__
177+ /// <summary>
178+ /// Initializes a new instance of the <see cref="Color"/> class with a <see cref="UIKit.UIColor"/>.
179+ /// </summary>
180+ /// <param name="color">
181+ /// The iOS color value.
182+ /// </param>
183+ public Color ( UIKit . UIColor color )
184+ {
185+ if ( color != null && color . CGColor != null )
186+ {
187+ if ( color . CGColor . NumberOfComponents == 2 )
188+ {
189+ var rgb = ( byte ) ( color . CGColor . Components [ 0 ] * 255 ) ;
190+ this . R = rgb ;
191+ this . G = rgb ;
192+ this . B = rgb ;
193+ this . A = ( byte ) ( color . CGColor . Components [ 1 ] * 255 ) ;
194+ }
195+ else
196+ {
197+ this . R = ( byte ) ( color . CGColor . Components [ 0 ] * 255 ) ;
198+ this . G = ( byte ) ( color . CGColor . Components [ 1 ] * 255 ) ;
199+ this . B = ( byte ) ( color . CGColor . Components [ 2 ] * 255 ) ;
200+ this . A = ( byte ) ( color . CGColor . Components [ 3 ] * 255 ) ;
201+ }
202+ }
203+ }
204+
205+ /// <summary>
206+ /// Supports the conversion of a <see cref="UIKit.UIColor"/> to <see cref="Color"/> implicitly.
207+ /// </summary>
208+ /// <param name="color">
209+ /// The iOS color value.
210+ /// </param>
211+ public static implicit operator Color ( UIKit . UIColor color )
212+ {
213+ return new Color ( color ) ;
214+ }
215+
216+ /// <summary>
217+ /// Supports the conversion of a <see cref="Color"/> to <see cref="UIKit.UIColor"/> implicitly.
218+ /// </summary>
219+ /// <param name="color">
220+ /// The internal color value.
221+ /// </param>
222+ public static implicit operator UIKit . UIColor ( Color color )
223+ {
224+ return UIKit . UIColor . FromRGBA ( color . R , color . G , color . B , color . A ) ;
225+ }
226+ #endif
227+
228+ /// <summary>
229+ /// Gets or sets the alpha component of the color.
230+ /// </summary>
231+ public byte A { get ; set ; }
232+
233+ /// <summary>
234+ /// Gets or sets the red component of the color.
235+ /// </summary>
236+ public byte R { get ; set ; }
237+
238+ /// <summary>
239+ /// Gets or sets the green component of the color.
240+ /// </summary>
241+ public byte G { get ; set ; }
242+
243+ /// <summary>
244+ /// Gets or sets the blue component of the color.
245+ /// </summary>
246+ public byte B { get ; set ; }
247+
248+ /// <summary>
249+ /// Constructs a color based on the alpha, red, green and blue parameters.
250+ /// </summary>
251+ /// <param name="a">
252+ /// The alpha component.
253+ /// </param>
254+ /// <param name="r">
255+ /// The red component.
256+ /// </param>
257+ /// <param name="g">
258+ /// The green component.
259+ /// </param>
260+ /// <param name="b">
261+ /// The blue component.
262+ /// </param>
263+ /// <returns>
264+ /// Returns the constructed color.
265+ /// </returns>
266+ public static Color FromArgb ( byte a , byte r , byte g , byte b )
267+ {
268+ return new Color ( a , r , g , b ) ;
269+ }
270+
271+ /// <summary>
272+ /// Checks the equality of two color items.
273+ /// </summary>
274+ /// <param name="color1">
275+ /// The color 1.
276+ /// </param>
277+ /// <param name="color2">
278+ /// The color 2.
279+ /// </param>
280+ /// <returns>
281+ /// Return true if the colors are equal.
282+ /// </returns>
283+ public static bool operator == ( Color color1 , Color color2 )
284+ {
285+ if ( ReferenceEquals ( color1 , color2 ) )
286+ {
287+ return true ;
288+ }
289+
290+ if ( ReferenceEquals ( color1 , null ) )
291+ {
292+ return false ;
293+ }
294+
295+ if ( ReferenceEquals ( color2 , null ) )
296+ {
297+ return false ;
298+ }
299+
300+ return color1 . Equals ( color2 ) ;
301+ }
302+
303+ /// <summary>
304+ /// Checks the inequality of two color items.
305+ /// </summary>
306+ /// <param name="color1">
307+ /// The color 1.
308+ /// </param>
309+ /// <param name="color2">
310+ /// The color 2.
311+ /// </param>
312+ /// <returns>
313+ /// Returns true if the colors are not equal.
314+ /// </returns>
315+ public static bool operator != ( Color color1 , Color color2 )
316+ {
317+ return ! ( color1 == color2 ) ;
318+ }
319+
320+ /// <summary>
321+ /// Determines whether the specified object is equal to the current object.
322+ /// </summary>
323+ /// <param name="obj">
324+ /// The object to compare with the current object.
325+ /// </param>
326+ /// <returns>
327+ /// Returns true if the specified object is equal to the current object.
328+ /// </returns>
329+ public override bool Equals ( object obj )
330+ {
331+ if ( ReferenceEquals ( null , obj ) )
332+ {
333+ return false ;
334+ }
335+
336+ if ( ReferenceEquals ( this , obj ) )
337+ {
338+ return true ;
339+ }
340+
341+ return obj . GetType ( ) == this . GetType ( ) && this . Equals ( ( Color ) obj ) ;
342+ }
343+
344+ /// <summary>
345+ /// Serves as the default hash function.
346+ /// </summary>
347+ /// <returns>
348+ /// A hash code for the current object.
349+ /// </returns>
350+ public override int GetHashCode ( )
351+ {
352+ unchecked
353+ {
354+ int hashCode = this . A . GetHashCode ( ) ;
355+ hashCode = ( hashCode * 397 ) ^ this . R . GetHashCode ( ) ;
356+ hashCode = ( hashCode * 397 ) ^ this . G . GetHashCode ( ) ;
357+ hashCode = ( hashCode * 397 ) ^ this . B . GetHashCode ( ) ;
358+ return hashCode ;
359+ }
360+ }
361+
362+ /// <summary>
363+ /// Checks the equality of the current color and the given color.
364+ /// </summary>
365+ /// <param name="other">
366+ /// The other color to compare.
367+ /// </param>
368+ /// <returns>
369+ /// Return true if the colors are equal.
370+ /// </returns>
371+ protected bool Equals ( Color other )
372+ {
373+ return this . A == other . A && this . R == other . R && this . G == other . G && this . B == other . B ;
374+ }
375+ }
376+ }
0 commit comments