开发者

percent to hex color value

I'm probably doing this the hard way, so what's the cleanest way to convert from a percentage to a string containing one part of an RGB hex value?

This is what I've got:

//dealing with small percentages, need to magnify the color differences.
double increaseVisibilityFactor = 5;     

double percent = someDouble / someOtherDouble;
double redLightenAmount = 1 - (percent * increaseVisibilityFactor);
if (percent > 0 && redLightenAmount < 0) { redLightenAmount = 0; } //overflow

int increaseWhiteBy = (int)(redLightenAmount * 0xFF);

string hexColor = increaseWhiteBy.ToString("X");
if (hexColor.Length < 2) { hexColor = "0" + hexColor; }
hexColor = "FF" + hexColor + hexColor;

Some of the above lines could obviously be combined (i.e. conversion to int and the开发者_运维知识库n string) but I'm wondering if there are overall flaws or a better way of doing this?


Yes, that can be done easier:

  string hexcolor = string.Format("FF{0:X2}{0:X2}", increaseWhiteBy);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜