如何将十六进制转换为RGB?
我试图用它来确定颜色是浅色还是深色
。它接受一个int,
float calcLuminance(int rgb)
{
int r = (rgb & 0xff0000) >> 16;
int g = (rgb & 0xff00) >> 8;
int b = (rgb & 0xff);
return (r*0.299f + g*0.587f + b*0.114f) / 256;
}
但我有一个十六进制颜色。
我尝试这样做
var color = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
int rgb = color.R + color.G + color.B;
var a = calcLuminance(rgb);
,得到了 0.11725,我认为它必须在 0-256 或类似的范围内。
我做错了什么?我是否必须将 R
转换为 int
?或者我还差得远?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
只需将十六进制字符串转换为整数:
Just convert the hex string to an integer:
您可以使用:
链接到 git 上 jeremy clifton 的原始帖子
You can use:
Link To original Post by jeremy clifton on git
只需使用
Color.GetBrightness()
[编辑]
有一个数字 方法确定使用什么颜色给定的背景,没有一个是完美的。
最后一个链接实际上建议仅使用黑色/白色,但选择截止点 0.73 而不是 0.5。我认为你应该继续这样做,如果你发现它不适合你,就改变它。
Just use
Color.GetBrightness()
[Edit]
There are a number of ways to determine what color to use on a given background, none of which are perfect.
That last link actually recommends using black/white only, but choosing a cutoff point of 0.73 instead of 0.5. I think you should just go with that, and change it if you find it doesn't work for you.
有点话题,但这里是我创建的颜色结构的扩展方法,用于使用不同的算法计算亮度。希望对您有帮助。
A little of topic, but here is an extension method to the Color struct I've created to calculate Luminance with different algorithms. Hope it helps you.
在我看来,问题在于您对 rgb 的计算。您将这些值加在一起,得到一个 0 到 3*255 之间的数字,这显然不是您的方法所期望的值。您必须像这样计算它,
它应该与此等效(除了您不使用的 alpha 值)
最后,正如您在 Chris Haas 答案中看到的那样,您可以通过直接转换为 int 来跳过此步骤。
The problem, as I see it, is your calculation of
rgb
. You add the values together which gives you a number between 0 and 3*255 which clearly isn't the value your method expect. You will have to calculate it like thiswhich should be equivalent to this (except for the alpha-value you don't use)
Lastly, as you can see in Chris Haas answer, you can skip this step by converting directly to an int.
你的想法是好的,但是你的功能是错误的,正确的在这里:
Your idea is OK, but your function is wrong, correct one is here:
您还可以将 HEX 转换为 RGB 而不使用 System.Drawing :
You can also convert HEX to RGB without using System.Drawing with this :
R、
G
和B
的范围/library/system.drawing.color.aspx" rel="nofollow">Color
结构体为 0-255。要获得您在函数中期望的 rgb 值,您需要相应地左移:
The ranges of the
R
,G
andB
from theColor
struct are 0-255.To get the rgb value you expect in your function, you will need to left shift accordingly:
calcLuminance
仅返回百分比。calcLuminance
only returns a percentage.当您第一次以某种方式从十六进制转换为 rgb 时...
您知道 rgb 的总值 3 * 256 = 768。其中一半 (384) 将是白色或黑色文本颜色之间的转折点。而且您不必使其变得更加复杂,因为颜色越接近中心,文本是白色还是黑色就越不重要。
请注意,该方法使用不带主题标签的十六进制颜色。
When you first translated from hex to rgb one way or other...
You know the total value of rgb 3 * 256 = 768. Half of that (384) would be the turning point between white or black text color. And you do not have to complicate it more because the closer the color is at the center the less it would matter if the text is white or black.
Note that the method uses hex color without the hashtag.