简体   繁体   English

将画笔转换为颜色

[英]Converting Brush to Color

I want to convert a Brush object to Color so that I can change any xaml button background color to its light color on button click, but System.Windows.Forms.ControlPaint.Light() takes only color as argument. 我想将Brush对象转换为Color以便我可以在按钮单击时将任何xaml按钮背景颜色更改为其light color ,但System.Windows.Forms.ControlPaint.Light()仅将颜色作为参数。

Is there some alternative to achieve this? 有没有替代方案来实现这一目标?

You could try getting the A,RGB values of the brush, and then pass them to System.Drawing.Color.FromARGB() Pseudo-Code: 您可以尝试获取画笔的A,RGB值,然后将它们传递给System.Drawing.Color.FromARGB() Pseudo-Code:

Brush br = Brushes.Green;
byte a = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).A;
byte g = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).G;
byte r = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).R;
byte b = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).B;
System.Windows.Forms.ControlPaint.Light(
    System.Drawing.Color.FromArgb((int)a,(int)r,(int)g,(int)b));

I'm not a WPF expert, but the main thing I think you need to keep in mind is the easiest way to do what you are trying is to use System.Drawing.Color.FromArgb() or even System.Drawing.Color.FromName() . 我不是WPF专家,但我认为你需要牢记的主要事情是你要做的最简单的方法是使用System.Drawing.Color.FromArgb()甚至System.Drawing.Color.FromName()

You don't need to reference the huge Windows.Forms dll just to lighten a Color . 你并不需要参考的巨大Windows.Forms DLL 只是为了减轻一个Color In its simplest terms, you're just multiplying each value by the same factor: 用最简单的术语来说,你只是将每个值乘以相同的因子:

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;
}

This could of course be improved in several ways (and should), but you get the idea. 这当然可以通过几种方式(并且应该)得到改善,但是你可以得到这个想法。 In fact, this will throw an Exception if a value goes over 255, but I'm sure that you can take care of that. 实际上,如果值超过255,这将抛出Exception ,但我确信你可以处理它。 Now you just need to check what type of Brush you need to brighten: 现在你只需要检查一下你需要提亮的Brush类型:

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
}

In my case I've done it like this (extension method): 在我的情况下,我这样做(扩展方法):

public static class BrushExtension
{

    public static Color GetColor(this Brush brush)
    {
        return new Pen(brush).Color;
    }

}

And call it like Color brushColor = myBrush.GetColor(); 并将其称为Color brushColor = myBrush.GetColor();

Try this: 尝试这个:

        Brush brush = Brushes.Yellow;
        Color color = ((SolidColorBrush) brush).Color;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM