繁体   English   中英

C#通过可能的5个字母组合进行迭代

[英]C# Iterate Through Possible Combination of 5 Letters

我正在创建一种称为DIF(词典图像格式)的图像格式,其中该图像指定了图像中的调色板(词典),或者可以选择加载外部调色板。

我正在尝试为RGB光谱中的所有可能值创建颜​​色字典。 我已经找到了如何遍历RGB光谱的方法,但是对于如何遍历所有可能的字典键却感到迷茫。

每个字典键都由一串大小写为5个字母的大写或小写字母(如aAaAB)组成。 这提供了380204032个可能的组合,足以对RGB中的1670万种颜色进行编码。

它将如何工作,以及如何将其与现有的RGB代码集成在一起,如下所示:

Dictionary<string,Color> rgb = new Dictionary<string,Color>();

for(int r = 0; r <= 255; r++)
{
   for(int g = 0; g <= 255; g++)
   {
        for(int b = 0; b <= 255; b++)
        {
            //Add colors to dictionary here
        }
   }
}

我建议您根本不需要字典。

相反,您可以按需将每种颜色与基数52(a-zA-Z)表示法之间进行转换。

这是一个例子。 它使用以下一些基本转换代码: 将基本10转换为.NET中任何基本的最快方法?

using System;
using System.Drawing;

namespace Demo
{
    internal class Program
    {
        private void run()
        {
            // As an example, iterate through all known colours and demonstrate
            // that we can convert each colour to a 5 character string and back.

            var colors = Enum.GetValues(typeof(KnownColor));

            foreach (KnownColor knowColor in colors)
            {
                Color colour = Color.FromKnownColor(knowColor);
                string colourString = ColourToBase52(colour);
                Color decodedColour = ColourFromBase52(colourString);

                if (colour.ToArgb() != decodedColour.ToArgb())
                    Console.WriteLine("ERROR with colour " + colour); // Expect this to complain about TRANSPARENT.
                else
                    Console.WriteLine("{0,-30} is represented by {1}", colour, colourString);
            }
        }

        public static string ColourToBase52(Color colour)
        {
            int value = colour.ToArgb() & 0x00FFFFFF; // Mask off the alpha channel.
            return ToBase52(value);
        }

        public static Color ColourFromBase52(string colour)
        {
            int value = FromBase52(colour);
            return Color.FromArgb(unchecked((int)(0xFF000000 | value)));
        }

        public static string ToBase52(int value)
        {
            char[] baseChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            int targetBase = baseChars.Length;

            int i = 32;
            char[] buffer = new char[i];

            do
            {
                buffer[--i] = baseChars[value % targetBase];
                value = value / targetBase;
            }
            while (value > 0);

            char[] result = new char[32 - i];
            Array.Copy(buffer, i, result, 0, 32 - i);

            return new string(result).PadLeft(5, 'a');
        }

        public static int FromBase52(string value)
        {
            char[] baseChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            int targetbase = baseChars.Length;

            int multiplier = 1;
            int result = 0;

            for (int i = value.Length-1; i >= 0; --i)
            {
                int digit = Array.IndexOf(baseChars, value[i]);
                result += digit*multiplier;
                multiplier *= targetbase;
            }

            return result;
        }

        public static void Main()
        {
            new Program().run();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM