### Discussed in https://github.com/PCL-Community/PCL.Neo/discussions/53 <div type='discussions-op-text'> <sup>Originally posted by **whitecat346** April 19, 2025</sup> MyColor中构造函数有些过于的繁杂了,例如: - `public MyColor(float r, float g, float b)`方法和`public MyColor(float a, float r, float g, float b)`方法就有些重复,可以使用带默认参数的构造函数以简化; - 而`public MyColor(Brush brush)`方法中将brush转为了SolidColorBrush,这个可以委托到`public MyColor(SolidColorBrush brush)`方法,然后`public MyColor(SolidColorBrush brush)`方法获取了brush的Color属性,这个属性是个`Avalonia.Media.Color`,这样可以委托到`public MyColor(Color color)`方法。 通过这样的方法可以大幅度简化构造函数的复杂度,下面给出我所做的优化: ```csharp public MyColor() { this._color = new Vector4(255f, 0f, 0f, 0f); } public MyColor(Color color) : this(color.R, color.G, color.B, a: color.A) { } public MyColor(string hex) : this(Color.Parse(hex)) { } public MyColor(float a, MyColor color) : this(color.R, color.G, color.B, a) { } public MyColor(float r, float g, float b) : this(r, g, b, 255f) { } public MyColor(float r, float g, float b, float a = 255f) { this._color = new Vector4(a, r, g, b); } public MyColor(SolidColorBrush brush) : this(brush.Color) { } public MyColor(Brush brush) : this((SolidColorBrush)brush) { } ```</div>
Activity
whitecat346 commentedon Jun 2, 2025
/cc @Hill23333
需要Hill23333查看