【问题标题】:How to convert Hex to RGB?如何将十六进制转换为 RGB?
【发布时间】:2011-08-09 19:17:06
【问题描述】:

我试图用它来判断颜色是浅色还是深色

Evaluate whether a HEX value is dark or light

现在。它接受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 吗?还是我就差这么远了?

【问题讨论】:

    标签: c# .net colors hex rgb


    【解决方案1】:

    Color 结构中的RGB 的范围是 0-255。

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

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

    【讨论】:

      【解决方案2】:

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

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

      应该等价于这个(除了你不使用的 alpha 值)

      int rgb = color.ToArgb();
      

      最后,正如您在 Chris Haas 的回答中看到的那样,您可以通过直接转换为 int 来跳过此步骤。

      【讨论】:

        【解决方案3】:

        calcLuminance 只返回一个百分比。

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

          • 哎呀,我赞成这个,因为我误读了这个问题。他实际上并不是在问“如何从十六进制转换为 RGB”——他真正想知道的是如何获得 RGB 颜色的亮度(亮度)。
          • 他有两个不同的潜在问题。 “如何做 x”和“有没有比 x 更好的方法”。我回答了第一个,你回答了第二个!
          【解决方案5】:

          我试图用它来判断颜色是浅色还是深色

          只需使用Color.GetBrightness()


          [编辑]

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

          有一个 number of ways 来确定在给定背景上使用什么颜色,但没有一个是完美的。

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

          【讨论】:

          • @BlueRaja - Danny Pflughoeft- 这样 System.Drawing.Color someColor = System.Drawing.Color.FromArgb(color.R, color.G, color.B);浮动 ab = someColor.GetBrightness();有没有办法在不先将其转换为 rgb 的情况下做到这一点?
          • @chobo2 就float ab = System.Drawing.ColorTranslator.FromHtml("#FFFFFF").GetBrightness(); :)
          • @Chobo:嗯,你是怎么把颜色输入到电脑里的?您给出的示例 RGB:在"#FFFFFF" 中,第一个"FF" 是R (十六进制),第二个"FF" 是G,而第三个"FF" 是B。正如@lasseespeholt 在上面的评论中所说,您可以在一行中从RGB 字符串中获取亮度。
          • @lasseespeholt - 酷。所以我想确定我的文本应该使用白色还是黑色。那么任何小于 0.5 的东西我应该使用白色,如果大于 0.5 我应该使用黑色?
          • 亮度和亮度不是一回事。我使用亮度来确定颜色是深还是浅,得到了更好的结果
          【解决方案6】:

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

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

          【讨论】:

            【解决方案7】:

            你可以使用:

            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

            【讨论】:

            • 找不到原始帖子的链接!
            【解决方案8】:

            你的想法没问题,但是你的功能错了,正确的在这里:

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

            【讨论】:

              猜你喜欢
              • 2015-05-09
              • 2011-05-14
              • 2020-11-14
              • 2013-07-10
              • 2015-01-31
              • 2021-03-22
              相关资源
              最近更新 更多