c# - 如何在不使用 System.Drawing 的情况下生成颜色

标签 c# colors system.drawing system.drawing.color

我只是想知道是否有任何方法可以在给定 RGB 的情况下创建颜色,而无需导入 System.Drawing。我想使用这种颜色来填充我创建的一些矩形。

最佳答案

您的意思是您想要一个颜色结构来存储 r g 和 b 值并帮助您将其转换为 32 位整数?

public struct MyColor
{
    public int Value;

    public MyColor(int a, int r, int g, int b)
    {
        this.Value = ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF);
    }

    public MyColor(int r, int g, int b) :
        this(255, r, g, b)
    {
    }

    public int A
    {
        get
        {
            return (byte) (this.Value >> 24);
        }
        set
        {
            this.Value = (this.Value & ~(0xFF << 24)) | ((value & 0xFF) << 24);
        }
    }

    public int R
    {
        get
        {
            return (byte) (this.Value >> 16);
        }
        set
        {
            this.Value = (this.Value & ~(0xFF << 16)) | ((value & 0xFF) << 16);
        }
    }

    public int G
    {
        get
        {
            return (byte) (this.Value >> 8);
        }
        set
        {
            this.Value = (this.Value & ~(0xFF << 8)) | ((value & 0xFF) << 8);
        }
    }

    public int B
    {
        get
        {
            return (byte) (this.Value);
        }
        set
        {
            this.Value = (this.Value & ~(0xFF)) | ((value & 0xFF));
        }
    }
}

注:纯手写,可能有错误。

关于c# - 如何在不使用 System.Drawing 的情况下生成颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7857017/

相关文章:

c# - 在 C# 中调整大小和水印图像

.net - Graphics.DrawRectangle 在第二个屏幕上缺少边缘

c# - System.Drawing.Bitmap GetBounds GraphicsUnit.Inch

c# - 在 Windows 窗体项目上使用 DataAnnotations

c# - MS visual Studio Forms 应用程序 : Can't get form to have less width then 100 Pixels

c# - WCF,生成的DataMember List<>代理类属性为null?

python - 根据时间函数绘制图像 rgb 值

java - 悬停在 JButton 上时出现奇怪的颜色变化

flutter - 如何更改 CircularProgressIndicator 的颜色

c# - 打开/关闭 Windows Phone 设置