将 .Net 颜色对象转换为十六进制代码并返回 [英] Convert .Net Color Objects to HEX codes and Back

查看:36
本文介绍了将 .Net 颜色对象转换为十六进制代码并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据问题标题,我如何获取十六进制代码并将其转换为 .Net Color 对象,然后以其他方式执行?

As per the question title, How could I take a hex code and convert it to a .Net Color object, and do it the other way?

我用谷歌搜索并不断得到同样的方法,但不起作用.

I googled and keep getting the same way which doesn't work.

 ColorTranslator.ToHtml(renderedChart.ForeColor)

返回颜色的名称,如'White'而不是'#ffffff'!以另一种方式做似乎有奇怪的结果,只能在某些时候工作......

Which returns the name of the color as in 'White' instead of '#ffffff'! Doing it the other way seems to have odd results, only working some of the time...

推荐答案

类似:

Color color = Color.Red;
string colorString = string.Format("#{0:X2}{1:X2}{2:X2}",
    color.R, color.G, color.B);

用另一种方式来做有点复杂,因为#F00 是一个有效的 html 颜色(意味着全红色)但它仍然可以使用正则表达式,这里是一个小示例类:

Doing it the other way is a little more complex as #F00 is a valid html color (meaning full red) but it is still doable using regex, here is a small sample class :

using System;
using System.Diagnostics;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public static class HtmlColors
{
    public static string ToHtmlHexadecimal(this Color color)
    {
        return string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
    }

    static Regex htmlColorRegex = new Regex(
        @"^#((?'R'[0-9a-f]{2})(?'G'[0-9a-f]{2})(?'B'[0-9a-f]{2}))"
        + @"|((?'R'[0-9a-f])(?'G'[0-9a-f])(?'B'[0-9a-f]))$",
        RegexOptions.Compiled | RegexOptions.IgnoreCase);

    public static Color FromHtmlHexadecimal(string colorString)
    {
        if (colorString == null)
        {
            throw new ArgumentNullException("colorString");
        }

        var match = htmlColorRegex.Match(colorString);
        if (!match.Success)
        {
            var msg = "The string "{0}" doesn't represent"
            msg += "a valid HTML hexadecimal color";
            msg = string.Format(msg, colorString);

            throw new ArgumentException(msg,
                "colorString");
        }

        return Color.FromArgb(
            ColorComponentToValue(match.Groups["R"].Value),
            ColorComponentToValue(match.Groups["G"].Value),
            ColorComponentToValue(match.Groups["B"].Value));
    }

    static int ColorComponentToValue(string component)
    {
        Debug.Assert(component != null);
        Debug.Assert(component.Length > 0);
        Debug.Assert(component.Length <= 2);

        if (component.Length == 1)
        {
            component += component;
        }

        return int.Parse(component,
            System.Globalization.NumberStyles.HexNumber);
    }
}

用法:

// Display #FF0000
Console.WriteLine(Color.Red.ToHtmlHexadecimal());

// Display #00FF00
Console.WriteLine(HtmlColors.FromHtmlHexadecimal("#0F0").ToHtmlHexadecimal());

// Display #FAF0FE
Console.WriteLine(HtmlColors.FromHtmlHexadecimal("#FAF0FE").ToHtmlHexadecimal());

这篇关于将 .Net 颜色对象转换为十六进制代码并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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