如何十六进制转换为RGB? [英] How to convert Hex to RGB?

查看:237
本文介绍了如何十六进制转换为RGB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用它来计算出,如果颜色是浅色或深色

<一个href="http://stackoverflow.com/questions/1754211/evaluate-whether-a-hex-value-is-dark-or-light">Evaluate一个十六进制值是否太暗或太亮

现在。这需要在 INT

 浮动calcLuminance(INT RGB)
 {
      INT R =(RGB&安培;为0xFF0000)&GT;&GT; 16;
      INT G =(RGB&安培;为0xFF00)&GT;&GT; 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()


  

我要确定我是否应该使用白色或黑色为我的文字。所以,什么≤0.5,我应该用白色和> 0.5的黑?

有一<一href="http://www.bennadel.com/blog/902-Selecting-Contrasting-Text-Color-Based-On-Background-Color.htm"相对=nofollow>若干 <一href="http://stackoverflow.com/questions/946544/good-text-foreground-color-for-a-given-background-color/946734#946734">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?

解决方案

I am trying to use this to figure out if a color is light or dark

Just use Color.GetBrightness()


[Edit]

I want to determine if I should use white or black for my text. So anything ≤ .5 I should use white and > .5 black?

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆