Skip to content

对于MyColor中构造函数的简化 #96

@DotnetInstall

Description

@DotnetInstall

Discussed in #53

Originally posted by whitecat346 April 19, 2025
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)方法。

通过这样的方法可以大幅度简化构造函数的复杂度,下面给出我所做的优化:

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>

Metadata

Metadata

Labels

优化对现有内容的优化与改进

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions