55namespace PCL2 . Neo . Models ;
66
77
8- public class MyColor
8+ public class MyColor : IEquatable < MyColor >
99{
1010 private Vector4 _color ;
1111
@@ -161,9 +161,51 @@ public MyColor(SolidColorBrush brush)
161161 this . _color = new Vector4 ( color . A , color . R , color . G , color . B ) ;
162162 }
163163
164+ // IEquatable
165+
166+ public bool Equals ( MyColor ? other )
167+ {
168+ if ( other is null )
169+ {
170+ return false ;
171+ }
172+
173+ if ( ReferenceEquals ( this , other ) )
174+ {
175+ return true ;
176+ }
177+
178+ return _color . Equals ( other . _color ) ;
179+ }
180+
181+ public override bool Equals ( object ? obj )
182+ {
183+ if ( obj is null )
184+ {
185+ return false ;
186+ }
187+
188+ if ( ReferenceEquals ( this , obj ) )
189+ {
190+ return true ;
191+ }
192+
193+ if ( obj . GetType ( ) != GetType ( ) )
194+ {
195+ return false ;
196+ }
197+
198+ return Equals ( ( MyColor ) obj ) ;
199+ }
200+
201+ public override int GetHashCode ( )
202+ {
203+ return _color . GetHashCode ( ) ;
204+ }
205+
164206 // HSL
165207
166- public double Hue ( double v1 , double v2 , double vH )
208+ public static double Hue ( double v1 , double v2 , double vH )
167209 {
168210 if ( vH < 0 ) vH += 1 ;
169211 if ( vH > 1 ) vH -= 1 ;
@@ -173,13 +215,14 @@ public double Hue(double v1, double v2, double vH)
173215 return v1 ;
174216 }
175217
176- public MyColor FromHsl ( double sH , double sS , double sL )
218+ public static MyColor FromHsl ( double sH , double sS , double sL )
177219 {
220+ var color = new MyColor ( ) ;
178221 if ( sS == 0 )
179222 {
180- R = ( float ) ( sL * 2.55 ) ;
181- G = R ;
182- B = R ;
223+ color . R = ( float ) ( sL * 2.55 ) ;
224+ color . G = color . R ;
225+ color . B = color . R ;
183226 }
184227 else
185228 {
@@ -188,42 +231,47 @@ public MyColor FromHsl(double sH, double sS, double sL)
188231 double l = sL / 100 ;
189232 s = l < 0.5 ? s * l + l : s * ( 1.0 - l ) + l ;
190233 l = 2 * l - s ;
191- R = ( float ) ( 255 * Hue ( l , s , h + 1 / 3.0 ) ) ;
192- G = ( float ) ( 255 * Hue ( l , s , h ) ) ;
193- B = ( float ) ( 255 * Hue ( l , s , h - 1 / 3.0 ) ) ;
234+ color . R = ( float ) ( 255 * Hue ( l , s , h + 1 / 3.0 ) ) ;
235+ color . G = ( float ) ( 255 * Hue ( l , s , h ) ) ;
236+ color . B = ( float ) ( 255 * Hue ( l , s , h - 1 / 3.0 ) ) ;
194237 }
195- A = 255 ;
196- return this ;
238+
239+ color . A = 255 ;
240+ return color ;
197241 }
198242
199- public MyColor FromHsl2 ( double sH , double sS , double sL )
243+ public static MyColor FromHsl2 ( double sH , double sS , double sL )
200244 {
245+ var color = new MyColor ( ) ;
201246 if ( sS == 0 )
202247 {
203- R = ( float ) ( sL * 2.55 ) ;
204- G = R ;
205- B = R ;
248+ color . R = ( float ) ( sL * 2.55 ) ;
249+ color . G = color . R ;
250+ color . B = color . R ;
206251 }
207252 else
208253 {
209254 sH = ( sH + 3600000 ) % 360 ;
210- double [ ] cent = [
255+ double [ ] cent =
256+ [
211257 + 0.1 , - 0.06 , - 0.3 , // 0, 30, 60
212258 - 0.19 , - 0.15 , - 0.24 , // 90, 120, 150
213259 - 0.32 , - 0.09 , + 0.18 , // 180, 210, 240
214260 + 0.05 , - 0.12 , - 0.02 , // 270, 300, 330
215- + 0.1 , - 0.06 ] ; // 最后两位与前两位一致,加是变亮,减是变暗
261+ + 0.1 , - 0.06
262+ ] ; // 最后两位与前两位一致,加是变亮,减是变暗
216263 double center = sH / 30.0 ;
217- int intCenter = ( int ) Math . Floor ( center ) ; // 亮度片区编号
264+ int intCenter = ( int ) Math . Floor ( center ) ; // 亮度片区编号
218265 center = 50 - (
219266 ( 1 - center + intCenter ) * cent [ intCenter ] + ( center - intCenter ) * cent [ intCenter + 1 ]
220267 ) * sS ;
221268
222269 sL = sL < center ? sL / center : 1 + ( sL - center ) / ( 100 - center ) ;
223270 sL *= 50 ;
224- FromHsl ( sH , sS , sL ) ;
271+ color = FromHsl ( sH , sS , sL ) ;
225272 }
226- A = 255 ;
227- return this ;
273+
274+ color . A = 255 ;
275+ return color ;
228276 }
229277}
0 commit comments