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+ public Color ( Windows . UI . Color color )
118+ {
119+ this . A = color . A ;
120+ this . R = color . R ;
121+ this . G = color . G ;
122+ this . B = color . B ;
123+ }
124+
125+ public static implicit operator Color ( Windows . UI . Color color )
126+ {
127+ return new Color ( color ) ;
128+ }
129+
130+ public static implicit operator Color ( Windows . UI . Xaml . Media . SolidColorBrush colorBrush )
131+ {
132+ return new Color ( colorBrush . Color ) ;
133+ }
134+
135+ public static implicit operator Windows . UI . Color ( Color color )
136+ {
137+ return Windows . UI . Color . FromArgb ( color . A , color . R , color . G , color . B ) ;
138+ }
139+
140+ public static implicit operator Windows . UI . Xaml . Media . SolidColorBrush ( Color color )
141+ {
142+ return new Windows . UI . Xaml . Media . SolidColorBrush ( color ) ;
143+ }
144+ #endif
145+
146+ #if __IOS__
147+ public Color ( UIKit . UIColor color )
148+ {
149+ if ( color != null && color . CGColor != null )
150+ {
151+ if ( color . CGColor . NumberOfComponents == 2 )
152+ {
153+ var rgb = ( byte ) ( color . CGColor . Components [ 0 ] * 255 ) ;
154+ this . R = rgb ;
155+ this . G = rgb ;
156+ this . B = rgb ;
157+ this . A = ( byte ) ( color . CGColor . Components [ 1 ] * 255 ) ;
158+ }
159+ else
160+ {
161+ this . R = ( byte ) ( color . CGColor . Components [ 0 ] * 255 ) ;
162+ this . G = ( byte ) ( color . CGColor . Components [ 1 ] * 255 ) ;
163+ this . B = ( byte ) ( color . CGColor . Components [ 2 ] * 255 ) ;
164+ this . A = ( byte ) ( color . CGColor . Components [ 3 ] * 255 ) ;
165+ }
166+ }
167+ }
168+
169+ public static implicit operator Color ( UIKit . UIColor color )
170+ {
171+ return new Color ( color ) ;
172+ }
173+
174+ public static implicit operator UIKit . UIColor ( Color color )
175+ {
176+ return UIKit . UIColor . FromRGBA ( color . R , color . G , color . B , color . A ) ;
177+ }
178+ #endif
179+
180+ /// <summary>
181+ /// Gets or sets the alpha component of the color.
182+ /// </summary>
183+ public byte A { get ; set ; }
184+
185+ /// <summary>
186+ /// Gets or sets the red component of the color.
187+ /// </summary>
188+ public byte R { get ; set ; }
189+
190+ /// <summary>
191+ /// Gets or sets the green component of the color.
192+ /// </summary>
193+ public byte G { get ; set ; }
194+
195+ /// <summary>
196+ /// Gets or sets the blue component of the color.
197+ /// </summary>
198+ public byte B { get ; set ; }
199+
200+ /// <summary>
201+ /// Constructs a color based on the alpha, red, green and blue parameters.
202+ /// </summary>
203+ /// <param name="a">
204+ /// The alpha component.
205+ /// </param>
206+ /// <param name="r">
207+ /// The red component.
208+ /// </param>
209+ /// <param name="g">
210+ /// The green component.
211+ /// </param>
212+ /// <param name="b">
213+ /// The blue component.
214+ /// </param>
215+ /// <returns>
216+ /// Returns the constructed color.
217+ /// </returns>
218+ public static Color FromArgb ( byte a , byte r , byte g , byte b )
219+ {
220+ return new Color ( a , r , g , b ) ;
221+ }
222+
223+ /// <summary>
224+ /// Checks the equality of two color items.
225+ /// </summary>
226+ /// <param name="color1">
227+ /// The color 1.
228+ /// </param>
229+ /// <param name="color2">
230+ /// The color 2.
231+ /// </param>
232+ /// <returns>
233+ /// Return true if the colors are equal.
234+ /// </returns>
235+ public static bool operator == ( Color color1 , Color color2 )
236+ {
237+ if ( ReferenceEquals ( color1 , color2 ) )
238+ {
239+ return true ;
240+ }
241+
242+ if ( ReferenceEquals ( color1 , null ) )
243+ {
244+ return false ;
245+ }
246+
247+ if ( ReferenceEquals ( color2 , null ) )
248+ {
249+ return false ;
250+ }
251+
252+ return color1 . Equals ( color2 ) ;
253+ }
254+
255+ /// <summary>
256+ /// Checks the inequality of two color items.
257+ /// </summary>
258+ /// <param name="color1">
259+ /// The color 1.
260+ /// </param>
261+ /// <param name="color2">
262+ /// The color 2.
263+ /// </param>
264+ /// <returns>
265+ /// Returns true if the colors are not equal.
266+ /// </returns>
267+ public static bool operator != ( Color color1 , Color color2 )
268+ {
269+ return ! ( color1 == color2 ) ;
270+ }
271+
272+ /// <summary>
273+ /// Determines whether the specified object is equal to the current object.
274+ /// </summary>
275+ /// <param name="obj">
276+ /// The object to compare with the current object.
277+ /// </param>
278+ /// <returns>
279+ /// Returns true if the specified object is equal to the current object.
280+ /// </returns>
281+ public override bool Equals ( object obj )
282+ {
283+ if ( ReferenceEquals ( null , obj ) )
284+ {
285+ return false ;
286+ }
287+
288+ if ( ReferenceEquals ( this , obj ) )
289+ {
290+ return true ;
291+ }
292+
293+ return obj . GetType ( ) == this . GetType ( ) && this . Equals ( ( Color ) obj ) ;
294+ }
295+
296+ /// <summary>
297+ /// Serves as the default hash function.
298+ /// </summary>
299+ /// <returns>
300+ /// A hash code for the current object.
301+ /// </returns>
302+ public override int GetHashCode ( )
303+ {
304+ unchecked
305+ {
306+ int hashCode = this . A . GetHashCode ( ) ;
307+ hashCode = ( hashCode * 397 ) ^ this . R . GetHashCode ( ) ;
308+ hashCode = ( hashCode * 397 ) ^ this . G . GetHashCode ( ) ;
309+ hashCode = ( hashCode * 397 ) ^ this . B . GetHashCode ( ) ;
310+ return hashCode ;
311+ }
312+ }
313+
314+ /// <summary>
315+ /// Checks the equality of the current color and the given color.
316+ /// </summary>
317+ /// <param name="other">
318+ /// The other color to compare.
319+ /// </param>
320+ /// <returns>
321+ /// Return true if the colors are equal.
322+ /// </returns>
323+ protected bool Equals ( Color other )
324+ {
325+ return this . A == other . A && this . R == other . R && this . G == other . G && this . B == other . B ;
326+ }
327+ }
328+ }
0 commit comments