0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

.NET の ColorTranslator.FromHTML() の処理を高速化してみました

Last updated at Posted at 2017-02-21

.NET で、Web,CSS の色をパースする際に、"#fff" や "#ff0000" などの色を指定されている文字列を解析し System.Drawing.Color を取得するためには、ColorTranslator.FromHTML() が利用可能ですが、ベンチマークしてみると多少遅いため、高速化できるか試してみました。

前提としては、名前付きの色などは、別対応としています。


public static System.Drawing.Color getColorFromHexHtmlFaster(string str)
        {

            char[] cs = str.ToCharArray();
            int cslen = cs.Length;
            if (cs[0] == '#')
            {
                switch (cslen)
                {
                    case 4:
                        return System.Drawing.Color.FromArgb(255, convertHexcharsToInt32(cs[1], cs[1]), convertHexcharsToInt32(cs[2], cs[2]), convertHexcharsToInt32(cs[3], cs[3]));
                    case 7:

                        return System.Drawing.Color.FromArgb(255, convertHexcharsToInt32(cs[1], cs[2]), convertHexcharsToInt32(cs[3], cs[4]), convertHexcharsToInt32(cs[5], cs[6]));

                    default:
                        return System.Drawing.Color.Transparent;
                }

            }else
            {
                return Color.Transparent;
            }
        }
        internal static int convertHexcharsToInt32(char c1, char c2)
        {

            return (getHexCharToInt(c1) * 16) + getHexCharToInt(c2);
        }

        internal static int getHexCharToInt(char c)
        {

            switch (c)
            {
                case '1':
                    return 1;
                case '2':
                    return 2;
                case '3':
                    return 3;
                case '4':
                    return 4;
                case '5':
                    return 5;
                case '6':
                    return 6;
                case '7':
                    return 7;
                case '8':
                    return 8;
                case '9':
                    return 9;
                case 'A':
                case 'a':
                    return 0xa;
                case 'B':
                case 'b':
                    return 0xb;
                case 'C':
                case 'c':
                    return 0xc;
                case 'D':
                case 'd':
                    return 0xd;
                case 'E':
                case 'e':
                    return 0xe;
                case 'F':
                case 'f':
                    return 0xf;
                case '0':
                default:
                    return 0;
            }

        }

このコードで、ColorTranslator.FromHtml() よりおよそ、1/3 程度まで、処理時間が短縮できました。


[Benchmark Result]
ColorTranslator.fromHTML() took 1503 ms Color [A=255, R=255, G=0, B=0]
Faster fromHTML() took 451 ms : Color [A=255, R=255, G=0, B=0]
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?