Int值为十六进制颜色C#WPF [英] Int value to hex color C# WPF

查看:322
本文介绍了Int值为十六进制颜色C#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转换存储为整数值的颜色。当我尝试转换时,颜色以相反的顺序出现。颜色是Alice Blue。整数值为16445926.十六进制值为#E6F1FA。 (R = 230,G = 241,B = 250)



但对我来说它是#FAF1E6(R = 250,G = 241,B = 230) ),当我从整数转换为Hex Value。因为我需要填充一个矩形,它应该是刷子格式。



我不知道我哪里错了



我尝试过:



  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( 十六进制值: + myHex);
var converter = new System.Windows.Media.BrushConverter();
var brush =(System.Windows.Media.Brush)converter.ConvertFrom(colorhex);
rect1.Fill = brush;

解决方案

我假设您正在处理订购BGR的颜色值MS Office。



我建议你建立自己的颜色转换函数库:这是我自己的库的开头:

使用System;使用System.Drawing 
;
使用System.Globalization;

命名空间ColorExtensions
{
公共静态类RGBExtensions
{
公共静态颜色RGBColorToBGRColor(此颜色rgbColor)
{
返回Color.FromArgb(rgbColor.A,rgbColor.B,rgbColor.G,rgbColor.R);
}

公共静态颜色BGRColorToRGBColor(此颜色bgrColor)
{
返回bgrColor.RGBColorToBGRColor();
}

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

返回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();
}

/ *留给你写

公共静态字符串BGRColorToRGBString(此颜色bgrColor,bool hexout = false)
{
}

公共静态字符串RGBColorToBGRString(此颜色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);
}
}
}

在某种方法中测试:

 //使用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;

颜色bgr = rgb.RGBIntToBGRColor();

颜色backtorgbcolor = bgr.BGRColorToRGBColor();

int backtoint = bgr.BGRColorToRGBInt();


FAF1E6是16445926的正确十六进制 - 所以你的红色和蓝色值交换。

交换字节:

  int  x = 0xFAF1E6; 
int y =((x& 0xFF0000)>> 16 )| (x& 0x00FF00)| ((x& 0x0000FF)<< 16 );

然后将其转换为颜色值

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:

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;

解决方案

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();


FAF1E6 is the correct Hex for 16445926 - so your Red and Blue colour values are swapped.
Swap the bytes:

int x = 0xFAF1E6;
int y = ((x & 0xFF0000) >> 16) | (x & 0x00FF00) | ((x & 0x0000FF) << 16);

Then convert it to a colour value


这篇关于Int值为十六进制颜色C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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