本文介绍了百分之十六进制颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能做这个硬盘的方式,有啥从百分比转换为包含RGB的十六进制值的一部分字符串最干净的方法?

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?

这是我有:

//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;

以上一些线路可以明显地结合(即转换为int,然后串),但我不知道是否有整体的缺陷或这样做的更好的办法?

Some of the above lines could obviously be combined (i.e. conversion to int and then 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);

这篇关于百分之十六进制颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 06:40