我只是想知道是否有任何方法可以在给定 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/