我想将Brush对象转换为Color,以便我可以在按钮单击时将任何xaml按钮背景颜色更改为其浅色,但System.
Windows.Forms.ControlPaint.Light()仅将颜色作为参数.
有没有替代方案来实现这一目标?
解决方法
你不需要引用巨大的Windows.Forms dll来减轻颜色.用最简单的术语来说,你只是将每个值乘以相同的因子:
private Color AdjustBrightness(double brightnessFactor) { Color originalColour = Color.Red; Color adjustedColour = Color.FromArgb(originalColour.A,(int)(originalColour.R * brightnessFactor),(int)(originalColour.G * brightnessFactor),(int)(originalColour.B * brightnessFactor)); return adjustedColour; }
这当然可以通过几种方式(并且应该)得到改善,但是你可以得到这个想法.事实上,如果值超过255,这将抛出异常,但我确信你可以照顾它.现在你只需要检查一下你需要提亮的刷子类型:
if (brush is SolidColorBrush) return new SolidColorBrush(AdjustBrightness(((SolidColorBrush)brush).Color)); else if (brush is LinearGradientBrush || brush is RadialGradientBrush) { // Go through each `GradientStop` in the `Brush` and brighten its colour }