15

質問のタイトルにあるように、16進コードを取得して.Net Colorオブジェクトに変換し、その逆を行うにはどうすればよいですか?

私はグーグルして、うまくいかない同じ方法を取得し続けます。

 ColorTranslator.ToHtml(renderedChart.ForeColor)

これは、「#ffffff」ではなく「White」のように色の名前を返します。他の方法でそれを行うと、奇妙な結果が得られるようで、一部の時間しか機能しません...

4

5 に答える 5

27

何かのようなもの :

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

#F00は有効なhtmlカラー(完全な赤を意味する)であるため、逆の方法で行うのは少し複雑ですが、正規表現を使用して実行できます。ここに小さなサンプルクラスがあります。

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());
于 2009-06-11T16:10:49.520 に答える
12

「白」有効なHTMLカラーです。ご覧くださいColorTranslator.ToHtml

このメソッドは、Color構造をHTMLカラーの文字列表現に変換します。これは、「赤」、「青」、「緑」などの一般的に使用される色の名前であり、「FF33AA」などの数値の色値の文字列表現ではありません。

色をHTMLカラー文字列にマップできない場合、このメソッドは色の有効な16進数を返します。この例を参照してください。

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
        Console.WriteLine(ColorTranslator.ToHtml(Color.White));
        Console.WriteLine(ColorTranslator.ToHtml(Color.FromArgb(32,67,89)));
    }
}
于 2009-06-11T16:08:07.937 に答える
1

{ColorTranslator.FromHTML}を使用して色を元に戻す場合、「白」はHTML色の有効な文字列です。しかし、この実装では、一般的でない場合、色を表す文字列の前に#記号が必要であることがわかりました(たとえば、Whiteと#FFFFFFで機能しますが、#WhiteまたはFFFFFFでは機能しません)。そこで、変換をtry / catchブロック内に入れて、文字列に「#」を追加して変換を行い、例外がスローされた場合は、それをキャッチして#なしで変換を行いました。

于 2012-03-22T20:14:56.793 に答える
1

16進数から整数へ、整数(ARGB)から色へ、またはその逆に簡単に変換するために、独自の.net拡張子を作成しました。

Imports System.Drawing
Imports System.Runtime.CompilerServices

Public Module ColorsExt

    Private Sub example_for_usage()
        Dim hex As String
        Dim c As Color = Color.Red
        hex = c.ToHexString() 'Converts color to hexadecimal string'
        c = hex.ToIntegerFromHex().ToColor() 'Gets Integer value from the hex string and simply convert it to Color()'
    End Sub

    <Extension> _
    Public Function ToColor(ByVal argb As Integer) As Color
        Return Color.FromArgb(argb)
    End Function

    <Extension> _
    Public Function ToIntegerFromHex(ByVal argb_hex_string As String) As Integer
        Return Int("&H" + argb_hex_string)
    End Function

    <Extension> _
    Public Function ToHexString(ByVal c As Color) As String
        Return String.Format("{0:X2}{1:X2}{2:X2}{3:X2}", c.A, c.R, c.G, c.B)
    End Function

    <Extension> _
    Public Sub FromHexString(ByVal c As Color, ByVal hex As String)
        c = hex.ToIntegerFromHex().ToColor()
    End Sub

    <Extension> _
    Public Function ToHexString(ByVal c As Integer) As String
        Return String.Format("{0:X8}", c)
    End Function

    <Extension> _
    Public Function ToArgb(ByVal c As Color) As Integer
        Return (c.A * &HFF0000) + (c.R * &HFF00) + (c.G * &HFF) + (c.B)
    End Function

End Module
于 2015-10-24T20:31:12.437 に答える
-4

Color.ToARGB()を調べてください

http://msdn.microsoft.com/en-us/library/system.drawing.color.toargb.aspx

于 2009-06-11T16:24:52.480 に答える