简体   繁体   中英

C# Iterate Through Possible Combination of 5 Letters

I am creating an image format called DIF (Dictionary Image Format), in which the image specifies the color palette (dictionary) within the image, or can choose to load an external one.

I am trying to create a color dictionary for all the possible values in the RGB spectrum. I have found how to iterate through the RGB spectrum, but am at a loss on how to iterate through all the possible dictionary keys.

Each dictionary key will be made of of a string of upper or lower case letters (like aAaAB) that is five letters long. This provides for 380204032 possible combinations, enough to encode the 16.7 million colors in RGB.

How would this work, and how would I integrate it with my existing RGB code, which is below:

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
        }
   }
}

I would suggest that you don't need a dictionary at all.

Instead, you can convert each colour to and from a base 52 (a-zA-Z) notation on demand.

Here's an example. It uses some base conversion code from here: Quickest way to convert a base 10 number to any base in .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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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