How to convert Hex to RGB?

后端 未结 8 1551
暖寄归人
暖寄归人 2021-02-12 11:13

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



        
相关标签:
8条回答
  • 2021-02-12 11:45

    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.

    0 讨论(0)
  • 2021-02-12 11:55

    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;
    
    0 讨论(0)
  • 2021-02-12 12:03

    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;
    }
    
    0 讨论(0)
  • 2021-02-12 12:03

    Just convert the hex string to an integer:

    int color = Convert.ToInt32("FFFFFF", 16);
    
    0 讨论(0)
  • 2021-02-12 12:04

    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

    0 讨论(0)
  • 2021-02-12 12:05

    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
       }
    }
    
    0 讨论(0)
提交回复
热议问题