Skip to content

Commit 6cd614d

Browse files
authored
Merge pull request #51 from 9223372036854775807-Studio/main
perf: MyColor重构
2 parents f8cd803 + 75b41a1 commit 6cd614d

File tree

2 files changed

+73
-108
lines changed

2 files changed

+73
-108
lines changed

PCL2.Neo/Models/MyColor.cs

Lines changed: 69 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-
using System;
21
using Avalonia.Media;
3-
using Color = Avalonia.Media.Color;
2+
using System;
3+
using System.Numerics;
44

55
namespace PCL2.Neo.Models;
66

77

88
public class MyColor
99
{
10-
public double A
10+
private Vector4 _color;
11+
12+
public float A
1113
{
12-
get => _a;
13-
set => _a = Clamp(value, 0, 255);
14+
get => this._color.X;
15+
set => this._color.X = value;
1416
}
15-
private double _a = 255;
1617

17-
18-
public double R
18+
public float R
1919
{
20-
get => _r;
21-
set => _r = Clamp(value, 0, 255);
20+
get => this._color.Y;
21+
set => this._color.Y = value;
2222
}
23-
private double _r;
24-
2523

26-
public double G
24+
public float G
2725
{
28-
get => _g;
29-
set => _g = Clamp(value, 0, 255);
26+
get => this._color.Z;
27+
set => this._color.Z = value;
3028
}
31-
private double _g;
3229

33-
34-
public double B
30+
public float B
3531
{
36-
get => _b;
37-
set => _b = Clamp(value, 0, 255);
32+
get => this._color.W;
33+
set => this._color.W = value;
3834
}
39-
private double _b;
4035

4136
// 类型转换
37+
4238
public static implicit operator MyColor(string str)
4339
{
4440
return new MyColor(str);
@@ -51,12 +47,10 @@ public static implicit operator MyColor(Color col)
5147

5248
public static implicit operator Color(MyColor conv)
5349
{
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));
6054
}
6155

6256
public static implicit operator MyColor(SolidColorBrush bru)
@@ -66,7 +60,10 @@ public static implicit operator MyColor(SolidColorBrush bru)
6660

6761
public static implicit operator SolidColorBrush(MyColor conv)
6862
{
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)));
7067
}
7168

7269
public static implicit operator MyColor(Brush bru)
@@ -76,112 +73,95 @@ public static implicit operator MyColor(Brush bru)
7673

7774
public static implicit operator Brush(MyColor conv)
7875
{
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)));
8080
}
8181

8282
// 颜色运算
8383

8484
public static MyColor operator +(MyColor a, MyColor b)
8585
{
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 };
8787
}
8888

8989

9090
public static MyColor operator -(MyColor a, MyColor b)
9191
{
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 };
93103
}
94104

95105
public static MyColor operator *(MyColor a, double b)
96106
{
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;
98108
}
99109

100110
public static MyColor operator /(MyColor a, double b)
101111
{
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;
103113
}
104114

105115
public static bool operator ==(MyColor a, MyColor b)
106116
{
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;
110118
}
111119

112120
public static bool operator !=(MyColor a, MyColor b)
113121
{
114-
return !(a == b);
122+
return a._color != b._color;
115123
}
116124

117125
// 构造函数
118126

119127
public MyColor()
120128
{
129+
this._color = new Vector4(255f, 0f, 0f, 0f);
121130
}
122-
123-
public MyColor(Color col)
131+
public MyColor(Color color)
124132
{
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);
129134
}
130-
131-
public MyColor(string hexString)
135+
public MyColor(string hex)
132136
{
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);
138139
}
139-
140-
141-
public MyColor(double newA, MyColor col)
140+
public MyColor(float a, MyColor color)
142141
{
143-
A = newA;
144-
R = col.R;
145-
G = col.G;
146-
B = col.B;
142+
this._color = color._color with { X = a };
147143
}
148144

149-
public MyColor(double newR, double newG, double newB)
145+
public MyColor(float r, float g, float b)
150146
{
151-
A = 255;
152-
R = newR;
153-
G = newG;
154-
B = newB;
147+
this._color = new Vector4(255f, r, g, b);
155148
}
156-
157-
public MyColor(double newA, double newR, double newG, double newB)
149+
public MyColor(float a, float r, float g, float b)
158150
{
159-
A = newA;
160-
R = newR;
161-
G = newG;
162-
B = newB;
151+
this._color = new Vector4(a, r, g, b);
163152
}
164-
165153
public MyColor(Brush brush)
166154
{
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);
173157
}
174-
175158
public MyColor(SolidColorBrush brush)
176159
{
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);
182162
}
183163

184-
// HSL转换
164+
// HSL
185165

186166
public double Hue(double v1, double v2, double vH)
187167
{
@@ -197,7 +177,7 @@ public MyColor FromHsl(double sH, double sS, double sL)
197177
{
198178
if (sS == 0)
199179
{
200-
R = sL * 2.55;
180+
R = (float)(sL * 2.55);
201181
G = R;
202182
B = R;
203183
}
@@ -208,9 +188,9 @@ public MyColor FromHsl(double sH, double sS, double sL)
208188
double l = sL / 100;
209189
s = l < 0.5 ? s * l + l : s * (1.0 - l) + l;
210190
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));
214194
}
215195
A = 255;
216196
return this;
@@ -220,19 +200,19 @@ public MyColor FromHsl2(double sH, double sS, double sL)
220200
{
221201
if (sS == 0)
222202
{
223-
R = sL * 2.55;
203+
R = (float)(sL * 2.55);
224204
G = R;
225205
B = R;
226206
}
227207
else
228208
{
229209
sH = (sH + 3600000) % 360;
230-
double[] cent = {
210+
double[] cent = [
231211
+0.1, -0.06, -0.3, // 0, 30, 60
232212
-0.19, -0.15, -0.24, // 90, 120, 150
233213
-0.32, -0.09, +0.18, // 180, 210, 240
234214
+0.05, -0.12, -0.02, // 270, 300, 330
235-
+0.1, -0.06}; // 最后两位与前两位一致,加是变亮,减是变暗
215+
+0.1, -0.06]; // 最后两位与前两位一致,加是变亮,减是变暗
236216
double center = sH / 30.0;
237217
int intCenter = (int)Math.Floor(center); // 亮度片区编号
238218
center = 50 - (
@@ -246,19 +226,4 @@ public MyColor FromHsl2(double sH, double sS, double sL)
246226
A = 255;
247227
return this;
248228
}
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-
}
264229
}

PCL2.Neo/Utils/MathUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public static MyColor MathRound(MyColor col, int w = 0)
6969
{
7070
return new MyColor
7171
{
72-
A = Math.Round(col.A, w),
73-
R = Math.Round(col.R, w),
74-
G = Math.Round(col.G, w),
75-
B = Math.Round(col.B, w)
72+
A = (float)Math.Round(col.A, w),
73+
R = (float)Math.Round(col.R, w),
74+
G = (float)Math.Round(col.G, w),
75+
B = (float)Math.Round(col.B, w)
7676
};
7777
}
7878

0 commit comments

Comments
 (0)