问题描述
我想用它来计算出,如果颜色是浅色或深色
Evaluate一个十六进制值是否太暗或太亮
现在。这需要在 INT
浮动calcLuminance(INT RGB)
{
INT R =(RGB&安培;为0xFF0000)>> 16;
INT G =(RGB&安培;为0xFF00)>> 8;
INT B =(RGB&安培; 0xFF的);
回报(R * 0.299f + G * 0.587f + B * 0.114f)/ 256;
}
我有一个十六进制颜色虽然。
我试图做到这一点
VAR颜色= System.Drawing.ColorTranslator.FromHtml(#FFFFFF);
INT RGB = color.R + color.G + color.B;
变种一个= calcLuminance(RGB);
我得到了0.11725我认为这将必须在0-256或类似的东西的范围。
我是什么做错了吗?我一定要隐蔽研究
到 INT
?还是我刚才的路要走?
只需使用 颜色。 GetBrightness()
有一若干 的 ways以确定在给定的背景,其中没有一个是完美使用什么颜色
这是最后一个环节,实际上推荐使用的0.73,而不是0.5黑/白而已,但选择一个分界点。我想你应该只是去了,并且改变它,如果你发现它不适合你的工作。
I am trying to use this to figure out if a color is light or dark
Evaluate whether a HEX value is dark or light
Now. It takes in a 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;
}
I have a hex color though.
I tried to do this
var color = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
int rgb = color.R + color.G + color.B;
var a = calcLuminance(rgb);
I got 0.11725 I thought it would have to be in the range of 0-256 or something like that.
What am I doing wrong? Do I have to covert R
to an int
? Or am I just way off?
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.
这篇关于如何十六进制转换为RGB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!