前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >win10 uwp 颜色转换 字符串转颜色颜色转字符串

win10 uwp 颜色转换 字符串转颜色颜色转字符串

作者头像
林德熙
发布2018-09-18 17:10:36
1.2K0
发布2018-09-18 17:10:36
举报
文章被收录于专栏:林德熙的博客林德熙的博客

本文告诉大家如何从字符串转颜色,从颜色转字符串

字符串转颜色

在 WPF 可以使用下面的代码把十六进制的颜色字符串转颜色

代码语言:javascript
复制
            Color color = (Color) ColorConverter.ConvertFromString("#FFDFD991");
代码语言:javascript
复制
string hex = "#FFFFFF";  
Color color = System.Drawing.ColorTranslator.FromHtml(hex); 

但是 UWP 没这个方法,所以需要自己写一个方法

代码语言:javascript
复制
        public SolidColorBrush GetSolidColorBrush(string hex)
        {
            hex = hex.Replace("#", string.Empty);
            byte a = (byte) (Convert.ToUInt32(hex.Substring(0, 2), 16));
            byte r = (byte) (Convert.ToUInt32(hex.Substring(2, 2), 16));
            byte g = (byte) (Convert.ToUInt32(hex.Substring(4, 2), 16));
            byte b = (byte) (Convert.ToUInt32(hex.Substring(6, 2), 16));
            return new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
        }

如果有小伙伴传入一个不带透明的,那么上面的代码就会出现异常,因为不带透明的颜色只有 6 个字符,所以就无法使用上面的代码,我修改了下面代码可以转换颜色

代码语言:javascript
复制
       public SolidColorBrush GetSolidColorBrush(string hex)
        {
            hex = hex.Replace("#", string.Empty);

            bool existAlpha = hex.Length == 8;

            if (!existAlpha && hex.Length != 6)
            {
                throw new ArgumentException("输入的hex不是有效颜色");
            }

            int n = 0;
            byte a;
            if (existAlpha)
            {
                n = 2;
                a = (byte) ConvertHexToByte(hex, 0);
            }
            else
            {
                a = 0xFF;
            }

            var r = (byte) ConvertHexToByte(hex, n);
            var g = (byte) ConvertHexToByte(hex, n + 2);
            var b = (byte) ConvertHexToByte(hex, n + 4);
            return new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
        }

        private static uint ConvertHexToByte(string hex, int n)
        {
            return Convert.ToUInt32(hex.Substring(n, 2), 16);
        }

大家可以从上面代码发现 ConvertHexToByte 这就是 16 进制转 int 的方法,请看C# 16 进制字符串转 int

但是存在这样写的颜色 #FD92 #DAC 的颜色,所以还需要继续修改一下算法

代码语言:javascript
复制
       public SolidColorBrush GetSolidColorBrush(string hex)
        {
            hex = hex.Replace("#", string.Empty);

            //#FFDFD991
            //#DFD991
            //#FD92
            //#DAC

            bool existAlpha = hex.Length == 8 || hex.Length == 4;
            bool isDoubleHex = hex.Length == 8 || hex.Length == 6;

            if (!existAlpha && hex.Length != 6 && hex.Length != 3)
            {
                throw new ArgumentException("输入的hex不是有效颜色");
            }

            int n = 0;
            byte a;
            int hexCount = isDoubleHex ? 2 : 1;
            if (existAlpha)
            {
                n = hexCount;
                a = (byte) ConvertHexToByte(hex, 0, hexCount);
                if (!isDoubleHex)
                {
                    a = (byte) (a * 16 + a);
                }
            }
            else
            {
                a = 0xFF;
            }

            var r = (byte) ConvertHexToByte(hex, n, hexCount);
            var g = (byte) ConvertHexToByte(hex, n + hexCount, hexCount);
            var b = (byte) ConvertHexToByte(hex, n + 2 * hexCount, hexCount);
            if (!isDoubleHex)
            {
                //#FD92 = #FFDD9922

                r = (byte) (r * 16 + r);
                g = (byte) (g * 16 + g);
                b = (byte) (b * 16 + b);
            }

            return new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
        }

        private static uint ConvertHexToByte(string hex, int n, int count = 2)
        {
            return Convert.ToUInt32(hex.Substring(n, count), 16);
        }

如果想看微软的转换,请看 https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/Parsers.cs

可以复制的源代码:

如果你没有在上面看到代码,请点击 https://gist.github.com/lindexi/36c5e223ff77cfb8adc4909dec1576b5

颜色转字符串

如果需要从颜色转字符串是很简单

代码语言:javascript
复制
Color.ToString()

上面的代码就可以输出字符串


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字符串转颜色
  • 颜色转字符串
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档