1
- using System ;
2
1
using Avalonia . Media ;
3
- using Color = Avalonia . Media . Color ;
2
+ using System ;
3
+ using System . Numerics ;
4
4
5
5
namespace PCL2 . Neo . Models ;
6
6
7
7
8
8
public class MyColor
9
9
{
10
- public double A
10
+ private Vector4 _color ;
11
+
12
+ public float A
11
13
{
12
- get => _a ;
13
- set => _a = Clamp ( value , 0 , 255 ) ;
14
+ get => this . _color . X ;
15
+ set => this . _color . X = value ;
14
16
}
15
- private double _a = 255 ;
16
17
17
-
18
- public double R
18
+ public float R
19
19
{
20
- get => _r ;
21
- set => _r = Clamp ( value , 0 , 255 ) ;
20
+ get => this . _color . Y ;
21
+ set => this . _color . Y = value ;
22
22
}
23
- private double _r ;
24
-
25
23
26
- public double G
24
+ public float G
27
25
{
28
- get => _g ;
29
- set => _g = Clamp ( value , 0 , 255 ) ;
26
+ get => this . _color . Z ;
27
+ set => this . _color . Z = value ;
30
28
}
31
- private double _g ;
32
29
33
-
34
- public double B
30
+ public float B
35
31
{
36
- get => _b ;
37
- set => _b = Clamp ( value , 0 , 255 ) ;
32
+ get => this . _color . W ;
33
+ set => this . _color . W = value ;
38
34
}
39
- private double _b ;
40
35
41
36
// 类型转换
37
+
42
38
public static implicit operator MyColor ( string str )
43
39
{
44
40
return new MyColor ( str ) ;
@@ -51,12 +47,10 @@ public static implicit operator MyColor(Color col)
51
47
52
48
public static implicit operator Color ( MyColor conv )
53
49
{
54
- return Color . FromArgb ( ( byte ) Clamp ( conv . A , 0 , 255 ) , ( byte ) Clamp ( conv . R , 0 , 255 ) , ( byte ) Clamp ( conv . G , 0 , 255 ) , ( byte ) Clamp ( conv . B , 0 , 255 ) ) ;
55
- }
56
-
57
- public static implicit operator System . Drawing . Color ( MyColor conv )
58
- {
59
- return System . Drawing . Color . FromArgb ( ( byte ) Clamp ( conv . A , 0 , 255 ) , ( byte ) Clamp ( conv . R , 0 , 255 ) , ( byte ) Clamp ( conv . G , 0 , 255 ) , ( byte ) Clamp ( conv . B , 0 , 255 ) ) ;
50
+ return Color . FromArgb ( ( byte ) Math . Clamp ( conv . A , 0 , 255 ) ,
51
+ ( byte ) Math . Clamp ( conv . R , 0 , 255 ) ,
52
+ ( byte ) Math . Clamp ( conv . G , 0 , 255 ) ,
53
+ ( byte ) Math . Clamp ( conv . B , 0 , 255 ) ) ;
60
54
}
61
55
62
56
public static implicit operator MyColor ( SolidColorBrush bru )
@@ -66,7 +60,10 @@ public static implicit operator MyColor(SolidColorBrush bru)
66
60
67
61
public static implicit operator SolidColorBrush ( MyColor conv )
68
62
{
69
- return new SolidColorBrush ( Color . FromArgb ( ( byte ) Clamp ( conv . A , 0 , 255 ) , ( byte ) Clamp ( conv . R , 0 , 255 ) , ( byte ) Clamp ( conv . G , 0 , 255 ) , ( byte ) Clamp ( conv . B , 0 , 255 ) ) ) ;
63
+ return new SolidColorBrush ( Color . FromArgb ( ( byte ) Math . Clamp ( conv . A , 0 , 255 ) ,
64
+ ( byte ) Math . Clamp ( conv . R , 0 , 255 ) ,
65
+ ( byte ) Math . Clamp ( conv . G , 0 , 255 ) ,
66
+ ( byte ) Math . Clamp ( conv . B , 0 , 255 ) ) ) ;
70
67
}
71
68
72
69
public static implicit operator MyColor ( Brush bru )
@@ -76,112 +73,95 @@ public static implicit operator MyColor(Brush bru)
76
73
77
74
public static implicit operator Brush ( MyColor conv )
78
75
{
79
- return new SolidColorBrush ( Color . FromArgb ( ( byte ) Clamp ( conv . A , 0 , 255 ) , ( byte ) Clamp ( conv . R , 0 , 255 ) , ( byte ) Clamp ( conv . G , 0 , 255 ) , ( byte ) Clamp ( conv . B , 0 , 255 ) ) ) ;
76
+ return new SolidColorBrush ( Color . FromArgb ( ( byte ) Math . Clamp ( conv . A , 0 , 255 ) ,
77
+ ( byte ) Math . Clamp ( conv . R , 0 , 255 ) ,
78
+ ( byte ) Math . Clamp ( conv . G , 0 , 255 ) ,
79
+ ( byte ) Math . Clamp ( conv . B , 0 , 255 ) ) ) ;
80
80
}
81
81
82
82
// 颜色运算
83
83
84
84
public static MyColor operator + ( MyColor a , MyColor b )
85
85
{
86
- return new MyColor { A = a . A + b . A , B = a . B + b . B , G = a . G + b . G , R = a . R + b . R } ;
86
+ return new MyColor { _color = a . _color + b . _color } ;
87
87
}
88
88
89
89
90
90
public static MyColor operator - ( MyColor a , MyColor b )
91
91
{
92
- return new MyColor { A = a . A - b . A , B = a . B - b . B , G = a . G - b . G , R = a . R - b . R } ;
92
+ return new MyColor { _color = a . _color - b . _color } ;
93
+ }
94
+
95
+ public static MyColor operator * ( MyColor a , float b )
96
+ {
97
+ return new MyColor { _color = a . _color * b } ;
98
+ }
99
+
100
+ public static MyColor operator / ( MyColor a , float b )
101
+ {
102
+ return new MyColor { _color = a . _color / b } ;
93
103
}
94
104
95
105
public static MyColor operator * ( MyColor a , double b )
96
106
{
97
- return new MyColor { A = a . A * b , B = a . B * b , G = a . G * b , R = a . R * b } ;
107
+ return a * ( float ) b ;
98
108
}
99
109
100
110
public static MyColor operator / ( MyColor a , double b )
101
111
{
102
- return new MyColor { A = a . A / b , B = a . B / b , G = a . G / b , R = a . R / b } ;
112
+ return a / ( float ) b ;
103
113
}
104
114
105
115
public static bool operator == ( MyColor a , MyColor b )
106
116
{
107
- if ( ReferenceEquals ( a , null ) && ReferenceEquals ( b , null ) ) return true ;
108
- if ( ReferenceEquals ( a , null ) || ReferenceEquals ( b , null ) ) return false ;
109
- return a . A == b . A && a . R == b . R && a . G == b . G && a . B == b . B ;
117
+ return a . _color == b . _color ;
110
118
}
111
119
112
120
public static bool operator != ( MyColor a , MyColor b )
113
121
{
114
- return ! ( a == b ) ;
122
+ return a . _color != b . _color ;
115
123
}
116
124
117
125
// 构造函数
118
126
119
127
public MyColor ( )
120
128
{
129
+ this . _color = new Vector4 ( 255f , 0f , 0f , 0f ) ;
121
130
}
122
-
123
- public MyColor ( Color col )
131
+ public MyColor ( Color color )
124
132
{
125
- A = col . A ;
126
- R = col . R ;
127
- G = col . G ;
128
- B = col . B ;
133
+ this . _color = new Vector4 ( color . A , color . R , color . G , color . B ) ;
129
134
}
130
-
131
- public MyColor ( string hexString )
135
+ public MyColor ( string hex )
132
136
{
133
- Color stringColor = Color . Parse ( hexString ) ;
134
- A = stringColor . A ;
135
- R = stringColor . R ;
136
- G = stringColor . G ;
137
- B = stringColor . B ;
137
+ var color = Color . Parse ( hex ) ;
138
+ this . _color = new Vector4 ( color . A , color . R , color . G , color . B ) ;
138
139
}
139
-
140
-
141
- public MyColor ( double newA , MyColor col )
140
+ public MyColor ( float a , MyColor color )
142
141
{
143
- A = newA ;
144
- R = col . R ;
145
- G = col . G ;
146
- B = col . B ;
142
+ this . _color = color . _color with { X = a } ;
147
143
}
148
144
149
- public MyColor ( double newR , double newG , double newB )
145
+ public MyColor ( float r , float g , float b )
150
146
{
151
- A = 255 ;
152
- R = newR ;
153
- G = newG ;
154
- B = newB ;
147
+ this . _color = new Vector4 ( 255f , r , g , b ) ;
155
148
}
156
-
157
- public MyColor ( double newA , double newR , double newG , double newB )
149
+ public MyColor ( float a , float r , float g , float b )
158
150
{
159
- A = newA ;
160
- R = newR ;
161
- G = newG ;
162
- B = newB ;
151
+ this . _color = new Vector4 ( a , r , g , b ) ;
163
152
}
164
-
165
153
public MyColor ( Brush brush )
166
154
{
167
- SolidColorBrush solidBrush = ( SolidColorBrush ) brush ;
168
- Color color = solidBrush . Color ;
169
- A = color . A ;
170
- R = color . R ;
171
- G = color . G ;
172
- B = color . B ;
155
+ var color = ( ( SolidColorBrush ) brush ) . Color ;
156
+ this . _color = new Vector4 ( color . A , color . R , color . G , color . B ) ;
173
157
}
174
-
175
158
public MyColor ( SolidColorBrush brush )
176
159
{
177
- Color color = brush . Color ;
178
- A = color . A ;
179
- R = color . R ;
180
- G = color . G ;
181
- B = color . B ;
160
+ var color = brush . Color ;
161
+ this . _color = new Vector4 ( color . A , color . R , color . G , color . B ) ;
182
162
}
183
163
184
- // HSL转换
164
+ // HSL
185
165
186
166
public double Hue ( double v1 , double v2 , double vH )
187
167
{
@@ -197,7 +177,7 @@ public MyColor FromHsl(double sH, double sS, double sL)
197
177
{
198
178
if ( sS == 0 )
199
179
{
200
- R = sL * 2.55 ;
180
+ R = ( float ) ( sL * 2.55 ) ;
201
181
G = R ;
202
182
B = R ;
203
183
}
@@ -208,9 +188,9 @@ public MyColor FromHsl(double sH, double sS, double sL)
208
188
double l = sL / 100 ;
209
189
s = l < 0.5 ? s * l + l : s * ( 1.0 - l ) + l ;
210
190
l = 2 * l - s ;
211
- R = 255 * Hue ( l , s , h + 1 / 3.0 ) ;
212
- G = 255 * Hue ( l , s , h ) ;
213
- B = 255 * Hue ( l , s , h - 1 / 3.0 ) ;
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 ) ) ;
214
194
}
215
195
A = 255 ;
216
196
return this ;
@@ -220,19 +200,19 @@ public MyColor FromHsl2(double sH, double sS, double sL)
220
200
{
221
201
if ( sS == 0 )
222
202
{
223
- R = sL * 2.55 ;
203
+ R = ( float ) ( sL * 2.55 ) ;
224
204
G = R ;
225
205
B = R ;
226
206
}
227
207
else
228
208
{
229
209
sH = ( sH + 3600000 ) % 360 ;
230
- double [ ] cent = {
210
+ double [ ] cent = [
231
211
+ 0.1 , - 0.06 , - 0.3 , // 0, 30, 60
232
212
- 0.19 , - 0.15 , - 0.24 , // 90, 120, 150
233
213
- 0.32 , - 0.09 , + 0.18 , // 180, 210, 240
234
214
+ 0.05 , - 0.12 , - 0.02 , // 270, 300, 330
235
- + 0.1 , - 0.06 } ; // 最后两位与前两位一致,加是变亮,减是变暗
215
+ + 0.1 , - 0.06 ] ; // 最后两位与前两位一致,加是变亮,减是变暗
236
216
double center = sH / 30.0 ;
237
217
int intCenter = ( int ) Math . Floor ( center ) ; // 亮度片区编号
238
218
center = 50 - (
@@ -246,19 +226,4 @@ public MyColor FromHsl2(double sH, double sS, double sL)
246
226
A = 255 ;
247
227
return this ;
248
228
}
249
-
250
- public override string ToString ( )
251
- {
252
- return $ "({ A } ,{ R } ,{ G } ,{ B } )";
253
- }
254
-
255
- public override bool Equals ( object obj )
256
- {
257
- return this == ( MyColor ) obj ;
258
- }
259
-
260
- public static double Clamp ( double value , double min , double max )
261
- {
262
- return Math . Min ( Math . Max ( value , min ) , max ) ;
263
- }
264
229
}
0 commit comments