开发者

Set a transparent color

I have a Color, and I have 开发者_开发技巧a method that should return a more "transparent" version of that color. I tried the following method:

public static Color SetTransparency(int A, Color color)
{
   return Color.FromArgb(A, color.R, color.G, color.B);
}

but for some reason, no matter what the A is, the returned Color's transparency level just won't change.

Any idea?


Well, it looks okay to me, except that you're using Color.R (etc) instead of color.R - are you sure you're actually using the returned Color rather than assuming it will change the existing color? How are you determining that the "transparency level" won't change?

Here's an example showing that the alpha value is genuinely correct in the returned color:

using System;
using System.Drawing;

class Test
{
    static Color SetTransparency(int A, Color color)
    {
        return Color.FromArgb(A, color.R, color.G, color.B);
    }
    
    static void Main()
    {
        Color halfTransparent = SetTransparency(127, Colors.Black);
        Console.WriteLine(halfTransparent.A); // Prints 127
    }
}

No surprises there. It would be really helpful if you'd provide a short but complete program which demonstrates the exact problem you're having. Are you sure that whatever you're doing with the color even supports transparency?

Note that this method effectively already exists as Color.FromArgb(int, Color).


Just use the correct overload of FromArgb

var color = Color.FromArgb(50, Color.Red);


There might be a problem with your naming. I made a standard Windows Forms project, with 2 buttons and added some code, when clicking the buttons their respective colors do actually fade away.

And I agree with Jon Skeet, you are implementing a duplicate method, also all parameter names should begin with a lower case letter, so 'a' instead of 'A'

code:

private void Form1_Load(object sender, EventArgs e)
{
    button1.BackColor = Color.Red;
    button2.BackColor = Color.Green;
}

private void button1_Click(object sender, EventArgs e)
{
    Color c = button1.BackColor;
    button1.BackColor = Color.FromArgb(Math.Max(c.A - 10, (byte)0), c.R, c.G, c.B);
}

private void button2_Click(object sender, EventArgs e)
{
    Color c = button2.BackColor;
    button2.BackColor = Color.FromArgb(Math.Max(c.A - 10, (byte)0), c.R, c.G, c.B);
}

public static Color SetTransparency(int a, Color color)
{
    return Color.FromArgb(a, color.R, color.G, color.B);
}


You can write an extension method for the Color class that returns a new Color with modified alpha value like this:

public static class ColorExtensions
{
    public static Color WithOpacity(this Color color, double opacity)
    {
        return Color.FromArgb((int)(opacity * 255), color);
    }
}

Usage

var orange = Color.Orange;
var orange1 = orange.WithOpacity(0.5);

Console.WriteLine($@"R={orange.R} G={orange.G} B={orange.B} A={orange.A}");
Console.WriteLine($@"R={orange1.R} G={orange1.G} B={orange1.B} A={orange1.A}");

Live Demo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜