Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to convert the color which is stored as an integer value. When I am trying to convert, the color is coming in reverse order. The Color is Alice Blue. Integer Value is 16445926. Hexadecimal value is #E6F1FA. (R=230, G=241, B=250)

But for me it is coming as #FAF1E6 (R=250, G=241, B=230), when I convert from the integer to Hex Value.Since I need to be fill in a rectangle, it should be in the brush format.

I don't know where I am going wrong

What I have tried:

C#
int pcolor = 16445926;
           
                var panelcolor = System.Drawing.Color.FromArgb(pcolor);
                
                System.Windows.MessageBox.Show("Panelcolor", panelcolor.ToString());
                string myHex = pcolor.ToString("X");
                string colorhex = "#" + myHex;
                System.Windows.MessageBox.Show("Hexadecimal Value : ", "#"+myHex); 
                var converter = new System.Windows.Media.BrushConverter();
                var brush = (System.Windows.Media.Brush)converter.ConvertFrom(colorhex);
                rect1.Fill = brush;
Posted
Updated 1-Jul-22 2:14am

I am going to assume you are dealing with color values ordered BGR as used in MS Office.

I suggest you build your own library of color conversion functions: here's a start from my own library:
using System;
using System.Drawing;
using System.Globalization;

namespace ColorExtensions
{
    public static class RGBExtensions
    {
        public static Color RGBColorToBGRColor(this Color rgbColor)
        {
            return Color.FromArgb(rgbColor.A, rgbColor.B, rgbColor.G, rgbColor.R);
        }

        public static Color BGRColorToRGBColor(this Color bgrColor)
        {
            return bgrColor.RGBColorToBGRColor();
        }

        public static Color RGBIntToBGRColor(this int rgbColor)
        {
            var byteAry = BitConverter.GetBytes(rgbColor);

            return Color.FromArgb(byteAry[3], byteAry[0], byteAry[1], byteAry[2]);
        }

        public static Color BGRIntToRGBColor(this int bgrColor)
        {
            return bgrColor.RGBIntToBGRColor();
        }

        public static int RGBColorToBGRInt(this Color rgbColor)
        {
            return rgbColor.ToArgb().RGBIntToBGRColor().ToArgb();
        }

        public static int BGRColorToRGBInt(this Color bgrColor)
        {
            return bgrColor.ToArgb().BGRIntToRGBColor().ToArgb();
        }

/* left for you to write

        public static string BGRColorToRGBString(this Color bgrColor, bool hexout = false)
        {
        }

        public static string RGBColorToBGRString(this Color rgbColor, bool hexout = false)
        {
        }
*/

        public static string ColorToHexString(this Color color)
        {
            return color.ToArgb().ToString("X");
        }

        private static CultureInfo provider = CultureInfo.InvariantCulture;

        public static (bool, Color) HexStringToColor(this string colorstring)
        {
            int i = 0;

            if (int.TryParse(colorstring, NumberStyles.HexNumber, provider, out i))
            {
                return (true, Color.FromArgb(i));
            }

            return (false, Color.Empty);
        }
    }
}
Test in some method:
// required using ColorExtensions;

            string hex = "FAF1E6";

            int toint = int.Parse(hex, NumberStyles.HexNumber);

            string backtostring = toint.ToString("X");

            var clr = hex.HexStringToColor();

            string backtohex;

            if (clr.Item1) backtohex = (clr.Item2).ColorToHexString();

            int rgb = 16445926;

            Color bgr = rgb.RGBIntToBGRColor();

            Color backtorgbcolor = bgr.BGRColorToRGBColor();

            int backtoint = bgr.BGRColorToRGBInt();
 
Share this answer
 
Comments
Maciej Los 27-Mar-19 5:57am    
5ed!
FAF1E6 is the correct Hex for 16445926 - so your Red and Blue colour values are swapped.
Swap the bytes:
C#
int x = 0xFAF1E6;
int y = ((x & 0xFF0000) >> 16) | (x & 0x00FF00) | ((x & 0x0000FF) << 16);
Then convert it to a colour value
 
Share this answer
 
For me, others solutions gave me #FF00 for 65280.
but colorTranslator of System.Drawing have done the work in 2 lines :

var byteAry = BitConverter.GetBytes(decValue);
string htmlHexColorValueTwo = ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(byteAry[3], byteAry[0], byteAry[1], byteAry[2]));

255 ==> #FF0000 (red)
65280 ==> #00FF00 (green) and NOT #FF00 (red)
 
Share this answer
 
C#
private Color LongToColor(long l)
        {
            Color couleur;
            long blueValue = l / 65536;
            long greenValue = (l - blueValue * 65536) / 256;
            long redValue = l - blueValue * 65536 - greenValue * 256;
            ColorConverter conv = new ColorConverter();
            string colorhex = "#FF" + redValue.ToString("X").PadLeft(2, '0') + greenValue.ToString("X").PadLeft(2, '0') + blueValue.ToString("X").PadLeft(2,            '0');
            couleur = (Color)conv.ConvertFrom(colorhex);
            return couleur;
        }

        private long ColorToLong(Color couleur)
        {
            long l;
            long blueValue = couleur.B * 65536;
            long greenValue = couleur.G * (65536 / 256);
            long redValue = couleur.G * (65536 / 256) / 256;
            l = blueValue + greenValue + redValue;
            return l;
        }
 
Share this answer
 
Comments
Richard Deeming 5-Mar-21 11:02am    
An unexplained code-dump is not a solution to this already-solved question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900