@@ -2,28 +2,100 @@ namespace LVGLSharp.Drawing;
22
33public readonly struct Color : IEquatable < Color >
44{
5+ private readonly string ? _name ;
6+
57 public Color ( byte r , byte g , byte b , byte a = 255 )
8+ : this ( a , r , g , b , null )
9+ {
10+ }
11+
12+ private Color ( byte a , byte r , byte g , byte b , string ? name )
613 {
714 A = a ;
815 R = r ;
916 G = g ;
1017 B = b ;
18+ _name = name ;
1119 }
1220
1321 public byte A { get ; }
22+
1423 public byte R { get ; }
24+
1525 public byte G { get ; }
26+
1627 public byte B { get ; }
1728
18- public static Color Empty => new ( 0 , 0 , 0 , 0 ) ;
29+ public bool IsEmpty => A == 0 && R == 0 && G == 0 && B == 0 && _name is null ;
30+
31+ public string Name => _name ?? ( IsEmpty ? nameof ( Empty ) : ToArgb ( ) . ToString ( "X8" ) ) ;
32+
33+ public static Color Empty => default ;
1934
20- public bool Equals ( Color other ) => A == other . A && R == other . R && G == other . G && B == other . B ;
35+ public static Color Transparent => new ( 0 , 255 , 255 , 255 , nameof ( Transparent ) ) ;
36+
37+ public static Color Black => new ( 255 , 0 , 0 , 0 , nameof ( Black ) ) ;
38+
39+ public static Color White => new ( 255 , 255 , 255 , 255 , nameof ( White ) ) ;
40+
41+ public static Color Red => new ( 255 , 255 , 0 , 0 , nameof ( Red ) ) ;
42+
43+ public static Color Green => new ( 255 , 0 , 128 , 0 , nameof ( Green ) ) ;
44+
45+ public static Color Blue => new ( 255 , 0 , 0 , 255 , nameof ( Blue ) ) ;
46+
47+ public static Color FromArgb ( int red , int green , int blue )
48+ {
49+ return FromArgb ( 255 , red , green , blue ) ;
50+ }
51+
52+ public static Color FromArgb ( int alpha , int red , int green , int blue )
53+ {
54+ return new Color ( ToByte ( alpha ) , ToByte ( red ) , ToByte ( green ) , ToByte ( blue ) , null ) ;
55+ }
56+
57+ public static Color FromArgb ( int alpha , Color baseColor )
58+ {
59+ return new Color ( ToByte ( alpha ) , baseColor . R , baseColor . G , baseColor . B , baseColor . _name ) ;
60+ }
61+
62+ public static Color FromName ( string name )
63+ {
64+ ArgumentException . ThrowIfNullOrWhiteSpace ( name ) ;
65+
66+ return name switch
67+ {
68+ nameof ( Transparent ) => Transparent ,
69+ nameof ( Black ) => Black ,
70+ nameof ( White ) => White ,
71+ nameof ( Red ) => Red ,
72+ nameof ( Green ) => Green ,
73+ nameof ( Blue ) => Blue ,
74+ _ => new Color ( 0 , 0 , 0 , 0 , name ) ,
75+ } ;
76+ }
77+
78+ public int ToArgb ( )
79+ {
80+ return ( A << 24 ) | ( R << 16 ) | ( G << 8 ) | B ;
81+ }
82+
83+ public bool Equals ( Color other ) => A == other . A && R == other . R && G == other . G && B == other . B && string . Equals ( _name , other . _name , StringComparison . Ordinal ) ;
2184
2285 public override bool Equals ( object ? obj ) => obj is Color other && Equals ( other ) ;
2386
24- public override int GetHashCode ( ) => HashCode . Combine ( A , R , G , B ) ;
87+ public override int GetHashCode ( ) => HashCode . Combine ( A , R , G , B , _name ) ;
88+
89+ public override string ToString ( ) => Name ;
2590
2691 public static bool operator == ( Color left , Color right ) => left . Equals ( right ) ;
2792
2893 public static bool operator != ( Color left , Color right ) => ! left . Equals ( right ) ;
94+
95+ private static byte ToByte ( int value )
96+ {
97+ return value < byte . MinValue || value > byte . MaxValue
98+ ? throw new ArgumentOutOfRangeException ( nameof ( value ) )
99+ : ( byte ) value ;
100+ }
29101}
0 commit comments