簡體   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