简体   繁体   English

设置透明颜色

[英]Set a transparent color

I have a Color , and I have a method that should return a more "transparent" version of that color.我有一个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.但出于某种原因,无论A是什么,返回的Color的透明度级别都不会改变。

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?好吧,对我来说看起来没问题,除了您使用的是Color.R (等)而不是color.R - 您确定您实际上是在使用返回的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:这是一个示例,显示 alpha 值在返回的颜色中真正正确:

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) .请注意,此方法实际上已作为Color.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.我制作了一个标准的 Windows 窗体项目,带有 2 个按钮并添加了一些代码,当单击按钮时,它们各自的颜色实际上会逐渐消失。

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'我同意 Jon Skeet,你正在实现一个重复的方法,而且所有参数名称都应该以小写字母开头,所以 'a' 而不是 '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:您可以为Color类编写一个扩展方法,该方法返回一个带有修改后的 alpha 值的新Color ,如下所示:

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 现场演示

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

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