简体   繁体   中英

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 asColor.FromArgb(int, Color) .

只需使用 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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