Home > DeveloperSection > Forums > WPF color effect for UI element
F Scott Fitzgerald
F Scott Fitzgerald

Total Post:29

Points:203
Posted on    July-15-2013 3:50 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 770  View(s)
Rate this:

Hello Mindstick!

How do I add a color effect to a UI element?

For example, it should look more yellow, so pixels have a more yellow color. All I need is to make my black form a bit white, while it is inactive.

Thanks in Advance.



shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    July-15-2013 7:57 AM

I recently needed a gradient effect that would go from a specified color to a lighter version of that color. I came across this post which works very nicely.

Here is the code as an extension method

public static Color Interpolate(this Color color1, Color color2, float percentage)
    {
        double a1 = color1.A / 255.0, r1 = color1.R / 255.0, g1 = color1.G / 255.0, b1 = color1.B / 255.0;
        double a2 = color2.A / 255.0, r2 = color2.R / 255.0, g2 = color2.G / 255.0, b2 = color2.B / 255.0;
        byte a3 = Convert.ToByte((a1 + (a2 - a1) * percentage) * 255);
        byte r3 = Convert.ToByte((r1 + (r2 - r1) * percentage) * 255);
        byte g3 = Convert.ToByte((g1 + (g2 - g1) * percentage) * 255);
        byte b3 = Convert.ToByte((b1 + (b2 - b1) * percentage) * 255);
        return Color.FromArgb(a3, r3, g3, b3);
    }   

In my case I mix in 50 % white

BackgroundColor.Interpolate(Colors.White, .5f);

Don't want to miss updates? Please click the below button!

Follow MindStick