如何将十六进制转换为RGB?

发布于 2024-11-02 20:43:48 字数 727 浏览 2 评论 0 原文

我试图用它来确定颜色是浅色还是深色

立即评估十六进制值是深色还是浅色

。它接受一个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 ?或者我还差得远?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

何止钟意 2024-11-09 20:43:48

只需将十六进制字符串转换为整数:

int color = Convert.ToInt32("FFFFFF", 16);

Just convert the hex string to an integer:

int color = Convert.ToInt32("FFFFFF", 16);
浮世清欢 2024-11-09 20:43:48

您可以使用:

public string GenerateRgba(string backgroundColor, decimal backgroundOpacity)
{
 Color color = ColorTranslator.FromHtml(hexBackgroundColor);
 int r = Convert.ToInt16(color.R);
 int g = Convert.ToInt16(color.G);
 int b = Convert.ToInt16(color.B);
 return string.Format("rgba({0}, {1}, {2}, {3});", r, g, b, backgroundOpacity);
}

链接到 git 上 jeremy clifton 的原始帖子

You can use:

public string GenerateRgba(string backgroundColor, decimal backgroundOpacity)
{
 Color color = ColorTranslator.FromHtml(hexBackgroundColor);
 int r = Convert.ToInt16(color.R);
 int g = Convert.ToInt16(color.G);
 int b = Convert.ToInt16(color.B);
 return string.Format("rgba({0}, {1}, {2}, {3});", r, g, b, backgroundOpacity);
}

Link To original Post by jeremy clifton on git

提笔书几行 2024-11-09 20:43:48

我试图用它来确定颜色是浅色还是深色

只需使用 Color.GetBrightness()


[编辑]

我想确定我的文本应该使用白色还是黑色。所以任何 ≤ .5 我应该使用白色和 > .5黑色?

有一个数字 方法确定使用什么颜色给定的背景,没有一个是完美的。

最后一个链接实际上建议仅使用黑色/白色,但选择截止点 0.73 而不是 0.5。我认为你应该继续这样做,如果你发现它不适合你,就改变它。

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.

可爱咩 2024-11-09 20:43:48

有点话题,但这里是我创建的颜色结构的扩展方法,用于使用不同的算法计算亮度。希望对您有帮助。

public static class ColorExtensions
{
    /// <summary>
    /// Gets the luminance of the color. A value between 0 (black) and 1 (white)
    /// </summary>
    /// <param name="color">The color.</param>
    /// <param name="algorithm">The type of luminance alg to use.</param>
    /// <returns>A value between 0 (black) and 1 (white)</returns>
    public static double GetLuminance(this Color color, LuminanceAlgorithm algorithm = LuminanceAlgorithm.Photometric)
    {
        switch (algorithm)
        {
            case LuminanceAlgorithm.CCIR601:
                return (0.2126 * color.R + 0.7152 * color.G + 0.0722 * color.B) / 255;

            case LuminanceAlgorithm.Perceived:
                return (Math.Sqrt(0.241 * Math.Pow(color.R, 2) + 0.691 * Math.Pow(color.G, 2) + 0.068 * Math.Pow(color.B, 2)) / 255);

            case LuminanceAlgorithm.Photometric:
                return (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;
        }

    }

   /// <summary>
   /// The luminances
   /// </summary>
   public enum LuminanceAlgorithm
   {
       /// <summary>
       /// Photometric/digital ITU-R
       /// </summary>
       Photometric,

       /// <summary>
       /// Digital CCIR601 (gives more weight to the R and B components, as preciev by the human eye)
       /// </summary>
       CCIR601,

       /// <summary>
       /// A perceived luminance
       /// </summary>
       Perceived
   }
}

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.

public static class ColorExtensions
{
    /// <summary>
    /// Gets the luminance of the color. A value between 0 (black) and 1 (white)
    /// </summary>
    /// <param name="color">The color.</param>
    /// <param name="algorithm">The type of luminance alg to use.</param>
    /// <returns>A value between 0 (black) and 1 (white)</returns>
    public static double GetLuminance(this Color color, LuminanceAlgorithm algorithm = LuminanceAlgorithm.Photometric)
    {
        switch (algorithm)
        {
            case LuminanceAlgorithm.CCIR601:
                return (0.2126 * color.R + 0.7152 * color.G + 0.0722 * color.B) / 255;

            case LuminanceAlgorithm.Perceived:
                return (Math.Sqrt(0.241 * Math.Pow(color.R, 2) + 0.691 * Math.Pow(color.G, 2) + 0.068 * Math.Pow(color.B, 2)) / 255);

            case LuminanceAlgorithm.Photometric:
                return (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;
        }

    }

   /// <summary>
   /// The luminances
   /// </summary>
   public enum LuminanceAlgorithm
   {
       /// <summary>
       /// Photometric/digital ITU-R
       /// </summary>
       Photometric,

       /// <summary>
       /// Digital CCIR601 (gives more weight to the R and B components, as preciev by the human eye)
       /// </summary>
       CCIR601,

       /// <summary>
       /// A perceived luminance
       /// </summary>
       Perceived
   }
}
神仙妹妹 2024-11-09 20:43:48

在我看来,问题在于您对 rgb 的计算。您将这些值加在一起,得到一个 0 到 3*255 之间的数字,这显然不是您的方法所期望的值。您必须像这样计算它,

int rgb = (int)color.R << 16 + (int)color.G << 8 + color.B;

它应该与此等效(除了您不使用的 alpha 值)

int rgb = color.ToArgb();

最后,正如您在 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 this

int rgb = (int)color.R << 16 + (int)color.G << 8 + color.B;

which should be equivalent to this (except for the alpha-value you don't use)

int rgb = color.ToArgb();

Lastly, as you can see in Chris Haas answer, you can skip this step by converting directly to an int.

初心 2024-11-09 20:43:48

你的想法是好的,但是你的功能是错误的,正确的在这里:

int rgb = Convert.ToInt32("#FFFFFF", 16);
var a = calcLuminance(rgb);

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

Your idea is OK, but your function is wrong, correct one is here:

int rgb = Convert.ToInt32("#FFFFFF", 16);
var a = calcLuminance(rgb);

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;
}
帅气尐潴 2024-11-09 20:43:48

您还可以将 HEX 转换为 RGB 而不使用 System.Drawing

 int RGBint = Convert.ToInt32("FFD700", 16);
 byte Red = (byte)((RGBint >> 16) & 255);
 byte Green = (byte)((RGBint >> 8) & 255);
 byte Blue = (byte)(RGBint & 255);
 //Color.FromRgb(Red, Green, Blue);

You can also convert HEX to RGB without using System.Drawing with this :

 int RGBint = Convert.ToInt32("FFD700", 16);
 byte Red = (byte)((RGBint >> 16) & 255);
 byte Green = (byte)((RGBint >> 8) & 255);
 byte Blue = (byte)(RGBint & 255);
 //Color.FromRgb(Red, Green, Blue);
凉薄对峙 2024-11-09 20:43:48

R、GB 的范围/library/system.drawing.color.aspx" rel="nofollow">Color 结构体为 0-255。

要获得您在函数中期望的 rgb 值,您需要相应地左移:

int rgb = (int)color.R << 16 + (int)color.G << 8 + color.B;

The ranges of the R, G and B from the Color struct are 0-255.

To get the rgb value you expect in your function, you will need to left shift accordingly:

int rgb = (int)color.R << 16 + (int)color.G << 8 + color.B;
满地尘埃落定 2024-11-09 20:43:48

calcLuminance 仅返回百分比。

calcLuminance only returns a percentage.

心清如水 2024-11-09 20:43:48

当您第一次以某种方式从十六进制转换为 rgb 时...

您知道 rgb 的总值 3 * 256 = 768。其中一半 (384) 将是白色或黑色文本颜色之间的转折点。而且您不必使其变得更加复杂,因为颜色越接近中心,文本是白色还是黑色就越不重要。

private static string GetTextColor(string backgroundColor)
{
    string color = "000000";

    Color bg = ColorTranslator.FromHtml("#" + backgroundColor);

    if (bg.R + bg.G + bg.B < 384)
    {
        color = "FFFFFF";
    }

    return color;
}

请注意,该方法使用不带主题标签的十六进制颜色。

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.

private static string GetTextColor(string backgroundColor)
{
    string color = "000000";

    Color bg = ColorTranslator.FromHtml("#" + backgroundColor);

    if (bg.R + bg.G + bg.B < 384)
    {
        color = "FFFFFF";
    }

    return color;
}

Note that the method uses hex color without the hashtag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文