我有一个值:RGBA(1.000,0.000,0.000,0.090)
我要把这个画在格子里。
到目前为止,我有以下代码:
var matches = Regex.Matches(e.CellValue.ToString(), @"([0-9]+\.[0-9]+)");
if (matches.Count == 4)
{
Color.FromArgb(matches[4].Value, matches[0].Value, matches[1].Value, matches[2].Value);
}
问题是,Color.FromArgb
只与Int32
打交道。据我所见,Color.
下的所有函数都是Int32
事务。我怎么处理这里的精确性?
谢谢。
发布于 2014-01-28 14:40:12
您所做的是:使用所提供的正则表达式解析部件,使用这些部件根据0-255的范围计算适当的整数值,并组合这些部件以形成颜色。
var regex = new Regex(@"([0-9]+\.[0-9]+)");
string colorData = "RGBA(1.000, 0.000, 0.000, 0.090)";
var matches = regex.Matches(colorData);
int r = GetColorValue(matches[0].Value);
int g = GetColorValue(matches[1].Value);
int b = GetColorValue(matches[2].Value);
int a = GetColorValue(matches[3].Value);
var color = Color.FromArgb(a,r,g,b);
private static int GetColorValue(string match)
{
return (int)Math.Round(double.Parse(match, CultureInfo.InvariantCulture) * 255);
}
发布于 2014-01-28 14:29:20
Color.FromArgb使用不同的值标度,其中值介于0和255之间。我还没有测试过,但是应该有一些类似的东西:
public Color FromArgbFloat(float alpha, float r, float g, float b)
{
return Color.FromArgb((int)Math.Round(alpha*255), (int)Math.Round(r*255), (int)Math.Round(g*255), Math.Round(b*255);
}
发布于 2014-01-28 14:29:46
Color.FromArgb
与从0到255的int
一起工作,您可以将代码更改为:
Color.FromArgb(Transform(matches[3].Value), Transform(matches[0].Value), Transform(matches[1].Value), Transform(matches[2].Value));
// ...
private int Transform(double value)
{
return (int)Math.Round(value*255);
}
https://stackoverflow.com/questions/21408148
复制相似问题