About
Home
History
Clients
Specification
Comprehensive
For MUD Clients
Implementation
256 Colors
IBM Chars
Utilities
MUSHii
External
8BitMUSH
Below are various snippets of code for usage with the implementation of XTerm's 256 colors. Specification information can be found on the Specifications page.

Red/Green/Blue Value Array For The 256 Colors
An array that contains the Red/Green/Blue values of each of the 256 colors.
static int[,] numbers = new int[256, 3] { { 0, 0, 0 }, { 128, 0, 0 }, { 0, 128, 0 },
{ 128, 128, 0 }, { 0, 0, 128 }, { 128, 0, 128 },
{ 0, 128, 128 }, { 192, 192, 192 }, { 128, 128, 128 },
{ 255, 0, 0 }, { 0, 255, 0 }, { 255, 255, 0 },
{ 0, 0, 255 }, { 255, 0, 255 }, { 0, 255, 255 },
{ 255, 255, 255 }, { 0, 0, 0 }, { 0, 0, 95 }, { 0, 0, 135 }, { 0, 0, 175 }, { 0, 0, 215 }, { 0, 0, 255 }, { 0, 95, 0 }, 
{ 0, 95, 95 }, { 0, 95, 135 }, { 0, 95, 175 }, { 0, 95, 215 }, { 0, 95, 255 }, { 0, 135, 0 }, { 0, 135, 95 }, 
{ 0, 135, 135 }, { 0, 135, 175 }, { 0, 135, 215 }, { 0, 135, 255 }, { 0, 175, 0 }, { 0, 175, 95 }, { 0, 175, 135 }, 
{ 0, 175, 175 }, { 0, 175, 215 }, { 0, 175, 255 }, { 0, 215, 0 }, { 0, 215, 95 }, { 0, 215, 135 }, { 0, 215, 175 }, 
{ 0, 215, 215 }, { 0, 215, 255 }, { 0, 255, 0 }, { 0, 255, 95 }, { 0, 255, 135 }, { 0, 255, 175 }, { 0, 255, 215 }, 
{ 0, 255, 255 }, { 95, 0, 0 }, { 95, 0, 95 }, { 95, 0, 135 }, { 95, 0, 175 }, { 95, 0, 215 }, { 95, 0, 255 }, 
{ 95, 95, 0 }, { 95, 95, 95 }, { 95, 95, 135 }, { 95, 95, 175 }, { 95, 95, 215 }, { 95, 95, 255 }, { 95, 135, 0 }, 
{ 95, 135, 95 }, { 95, 135, 135 }, { 95, 135, 175 }, { 95, 135, 215 }, { 95, 135, 255 }, { 95, 175, 0 }, 
{ 95, 175, 95 }, { 95, 175, 135 }, { 95, 175, 175 }, { 95, 175, 215 }, { 95, 175, 255 }, { 95, 215, 0 }, 
{ 95, 215, 95 }, { 95, 215, 135 }, { 95, 215, 175 }, { 95, 215, 215 }, { 95, 215, 255 }, { 95, 255, 0 }, 
{ 95, 255, 95 }, { 95, 255, 135 }, { 95, 255, 175 }, { 95, 255, 215 }, { 95, 255, 255 }, { 135, 0, 0 }, 
{ 135, 0, 95 }, { 135, 0, 135 }, { 135, 0, 175 }, { 135, 0, 215 }, { 135, 0, 255 }, { 135, 95, 0 }, 
{ 135, 95, 95 }, { 135, 95, 135 }, { 135, 95, 175 }, { 135, 95, 215 }, { 135, 95, 255 }, { 135, 135, 0 }, 
{ 135, 135, 95 }, { 135, 135, 135 }, { 135, 135, 175 }, { 135, 135, 215 }, { 135, 135, 255 }, { 135, 175, 0 }, 
{ 135, 175, 95 }, { 135, 175, 135 }, { 135, 175, 175 }, { 135, 175, 215 }, { 135, 175, 255 }, { 135, 215, 0 }, 
{ 135, 215, 95 }, { 135, 215, 135 }, { 135, 215, 175 }, { 135, 215, 215 }, { 135, 215, 255 }, { 135, 255, 0 }, 
{ 135, 255, 95 }, { 135, 255, 135 }, { 135, 255, 175 }, { 135, 255, 215 }, { 135, 255, 255 }, { 175, 0, 0 }, 
{ 175, 0, 95 }, { 175, 0, 135 }, { 175, 0, 175 }, { 175, 0, 215 }, { 175, 0, 255 }, { 175, 95, 0 }, { 175, 95, 95 }, 
{ 175, 95, 135 }, { 175, 95, 175 }, { 175, 95, 215 }, { 175, 95, 255 }, { 175, 135, 0 }, { 175, 135, 95 }, 
{ 175, 135, 135 }, { 175, 135, 175 }, { 175, 135, 215 }, { 175, 135, 255 }, { 175, 175, 0 }, { 175, 175, 95 }, 
{ 175, 175, 135 }, { 175, 175, 175 }, { 175, 175, 215 }, { 175, 175, 255 }, { 175, 215, 0 }, { 175, 215, 95 }, 
{ 175, 215, 135 }, { 175, 215, 175 }, { 175, 215, 215 }, { 175, 215, 255 }, { 175, 255, 0 }, { 175, 255, 95 }, 
{ 175, 255, 135 }, { 175, 255, 175 }, { 175, 255, 215 }, { 175, 255, 255 }, { 215, 0, 0 }, { 215, 0, 95 }, 
{ 215, 0, 135 }, { 215, 0, 175 }, { 215, 0, 215 }, { 215, 0, 255 }, { 215, 95, 0 }, { 215, 95, 95 }, { 215, 95, 135 }, 
{ 215, 95, 175 }, { 215, 95, 215 }, { 215, 95, 255 }, { 215, 135, 0 }, { 215, 135, 95 }, { 215, 135, 135 }, 
{ 215, 135, 175 }, { 215, 135, 215 }, { 215, 135, 255 }, { 215, 175, 0 }, { 215, 175, 95 }, { 215, 175, 135 }, 
{ 215, 175, 175 }, { 215, 175, 215 }, { 215, 175, 255 }, { 215, 215, 0 }, { 215, 215, 95 }, { 215, 215, 135 }, 
{ 215, 215, 175 }, { 215, 215, 215 }, { 215, 215, 255 }, { 215, 255, 0 }, { 215, 255, 95 }, { 215, 255, 135 }, 
{ 215, 255, 175 }, { 215, 255, 215 }, { 215, 255, 255 }, { 255, 0, 0 }, { 255, 0, 95 }, { 255, 0, 135 }, 
{ 255, 0, 175 }, { 255, 0, 215 }, { 255, 0, 255 }, { 255, 95, 0 }, { 255, 95, 95 }, { 255, 95, 135 }, 
{ 255, 95, 175 }, { 255, 95, 215 }, { 255, 95, 255 }, { 255, 135, 0 }, { 255, 135, 95 }, { 255, 135, 135 }, 
{ 255, 135, 175 }, { 255, 135, 215 }, { 255, 135, 255 }, { 255, 175, 0 }, { 255, 175, 95 }, { 255, 175, 135 }, 
{ 255, 175, 175 }, { 255, 175, 215 }, { 255, 175, 255 }, { 255, 215, 0 }, { 255, 215, 95 }, { 255, 215, 135 },
{ 255, 215, 175 }, { 255, 215, 215 }, { 255, 215, 255 }, { 255, 255, 0 }, { 255, 255, 95 }, { 255, 255, 135 }, 
{ 255, 255, 175 }, { 255, 255, 215 }, { 255, 255, 255 }, { 0, 0, 0 }, { 18, 18, 18 }, { 28, 28, 28 }, { 38, 38, 38 }, 
{ 48, 48, 48 }, { 58, 58, 58 }, { 68, 68, 68 }, { 78, 78, 78 }, { 88, 88, 88 }, { 98, 98, 98 }, { 108, 108, 108 }, 
{ 118, 118, 118 }, { 128, 128, 128 }, { 138, 138, 138 }, { 148, 148, 148 }, { 158, 158, 158 }, { 168, 168, 168 }, 
{ 178, 178, 178 }, { 188, 188, 188 }, { 198, 198, 198 }, { 208, 208, 208 }, { 218, 218, 218 }, { 228, 228, 228 }, 
{ 238, 238, 238 } };
Color Object Array For The 256 Colors
An array that contains C# Color objects for each of the 256 colors.
static public Color[] Colors = new Color[] { Color.FromArgb(0, 0, 0), Color.FromArgb(128, 0, 0), Color.FromArgb(0, 128, 0), 
Color.FromArgb(128, 128, 0), Color.FromArgb(0, 0, 128), Color.FromArgb(128, 0, 128), Color.FromArgb(0, 128, 128), 
Color.FromArgb(192, 192, 192), Color.FromArgb(128, 128, 128), Color.FromArgb(255, 0, 0), Color.FromArgb(0, 255, 0), 
Color.FromArgb(255, 255, 0), Color.FromArgb(0, 0, 255), Color.FromArgb(255, 0, 255), Color.FromArgb(0, 255, 255), 
Color.FromArgb(255, 255, 255), Color.FromArgb(0, 0, 0), Color.FromArgb(0, 0, 95), Color.FromArgb(0, 0, 135), 
Color.FromArgb(0, 0, 175), Color.FromArgb(0, 0, 215), Color.FromArgb(0, 0, 255), Color.FromArgb(0, 95, 0), 
Color.FromArgb(0, 95, 95), Color.FromArgb(0, 95, 135), Color.FromArgb(0, 95, 175), Color.FromArgb(0, 95, 215), 
Color.FromArgb(0, 95, 255), Color.FromArgb(0, 135, 0), Color.FromArgb(0, 135, 95), Color.FromArgb(0, 135, 135), 
Color.FromArgb(0, 135, 175), Color.FromArgb(0, 135, 215), Color.FromArgb(0, 135, 255), Color.FromArgb(0, 175, 0), 
Color.FromArgb(0, 175, 95), Color.FromArgb(0, 175, 135), Color.FromArgb(0, 175, 175), Color.FromArgb(0, 175, 215), 
Color.FromArgb(0, 175, 255), Color.FromArgb(0, 215, 0), Color.FromArgb(0, 215, 95), Color.FromArgb(0, 215, 135), 
Color.FromArgb(0, 215, 175), Color.FromArgb(0, 215, 215), Color.FromArgb(0, 215, 255), Color.FromArgb(0, 255, 0), 
Color.FromArgb(0, 255, 95), Color.FromArgb(0, 255, 135), Color.FromArgb(0, 255, 175), Color.FromArgb(0, 255, 215), 
Color.FromArgb(0, 255, 255), Color.FromArgb(95, 0, 0), Color.FromArgb(95, 0, 95), Color.FromArgb(95, 0, 135), 
Color.FromArgb(95, 0, 175), Color.FromArgb(95, 0, 215), Color.FromArgb(95, 0, 255), Color.FromArgb(95, 95, 0), 
Color.FromArgb(95, 95, 95), Color.FromArgb(95, 95, 135), Color.FromArgb(95, 95, 175), Color.FromArgb(95, 95, 215), 
Color.FromArgb(95, 95, 255), Color.FromArgb(95, 135, 0), Color.FromArgb(95, 135, 95), Color.FromArgb(95, 135, 135), 
Color.FromArgb(95, 135, 175), Color.FromArgb(95, 135, 215), Color.FromArgb(95, 135, 255), Color.FromArgb(95, 175, 0), 
Color.FromArgb(95, 175, 95), Color.FromArgb(95, 175, 135), Color.FromArgb(95, 175, 175), Color.FromArgb(95, 175, 215), 
Color.FromArgb(95, 175, 255), Color.FromArgb(95, 215, 0), Color.FromArgb(95, 215, 95), Color.FromArgb(95, 215, 135), 
Color.FromArgb(95, 215, 175), Color.FromArgb(95, 215, 215), Color.FromArgb(95, 215, 255), Color.FromArgb(95, 255, 0), 
Color.FromArgb(95, 255, 95), Color.FromArgb(95, 255, 135), Color.FromArgb(95, 255, 175), Color.FromArgb(95, 255, 215), 
Color.FromArgb(95, 255, 255), Color.FromArgb(135, 0, 0), Color.FromArgb(135, 0, 95), Color.FromArgb(135, 0, 135), 
Color.FromArgb(135, 0, 175), Color.FromArgb(135, 0, 215), Color.FromArgb(135, 0, 255), Color.FromArgb(135, 95, 0), 
Color.FromArgb(135, 95, 95), Color.FromArgb(135, 95, 135), Color.FromArgb(135, 95, 175), Color.FromArgb(135, 95, 215), 
Color.FromArgb(135, 95, 255), Color.FromArgb(135, 135, 0), Color.FromArgb(135, 135, 95), Color.FromArgb(135, 135, 135), 
Color.FromArgb(135, 135, 175), Color.FromArgb(135, 135, 215), Color.FromArgb(135, 135, 255), Color.FromArgb(135, 175, 0), 
Color.FromArgb(135, 175, 95), Color.FromArgb(135, 175, 135), Color.FromArgb(135, 175, 175), Color.FromArgb(135, 175, 215), 
Color.FromArgb(135, 175, 255), Color.FromArgb(135, 215, 0), Color.FromArgb(135, 215, 95), Color.FromArgb(135, 215, 135), 
Color.FromArgb(135, 215, 175), Color.FromArgb(135, 215, 215), Color.FromArgb(135, 215, 255), Color.FromArgb(135, 255, 0), 
Color.FromArgb(135, 255, 95), Color.FromArgb(135, 255, 135), Color.FromArgb(135, 255, 175), Color.FromArgb(135, 255, 215), 
Color.FromArgb(135, 255, 255), Color.FromArgb(175, 0, 0), Color.FromArgb(175, 0, 95), Color.FromArgb(175, 0, 135), 
Color.FromArgb(175, 0, 175), Color.FromArgb(175, 0, 215), Color.FromArgb(175, 0, 255), Color.FromArgb(175, 95, 0), 
Color.FromArgb(175, 95, 95), Color.FromArgb(175, 95, 135), Color.FromArgb(175, 95, 175), Color.FromArgb(175, 95, 215), 
Color.FromArgb(175, 95, 255), Color.FromArgb(175, 135, 0), Color.FromArgb(175, 135, 95), Color.FromArgb(175, 135, 135), 
Color.FromArgb(175, 135, 175), Color.FromArgb(175, 135, 215), Color.FromArgb(175, 135, 255), Color.FromArgb(175, 175, 0), 
Color.FromArgb(175, 175, 95), Color.FromArgb(175, 175, 135), Color.FromArgb(175, 175, 175), Color.FromArgb(175, 175, 215), 
Color.FromArgb(175, 175, 255), Color.FromArgb(175, 215, 0), Color.FromArgb(175, 215, 95), Color.FromArgb(175, 215, 135), 
Color.FromArgb(175, 215, 175), Color.FromArgb(175, 215, 215), Color.FromArgb(175, 215, 255), Color.FromArgb(175, 255, 0), 
Color.FromArgb(175, 255, 95), Color.FromArgb(175, 255, 135), Color.FromArgb(175, 255, 175), Color.FromArgb(175, 255, 215), 
Color.FromArgb(175, 255, 255), Color.FromArgb(215, 0, 0), Color.FromArgb(215, 0, 95), Color.FromArgb(215, 0, 135), 
Color.FromArgb(215, 0, 175), Color.FromArgb(215, 0, 215), Color.FromArgb(215, 0, 255), Color.FromArgb(215, 95, 0), 
Color.FromArgb(215, 95, 95), Color.FromArgb(215, 95, 135), Color.FromArgb(215, 95, 175), Color.FromArgb(215, 95, 215),
Color.FromArgb(215, 95, 255), Color.FromArgb(215, 135, 0), Color.FromArgb(215, 135, 95), Color.FromArgb(215, 135, 135), 
Color.FromArgb(215, 135, 175), Color.FromArgb(215, 135, 215), Color.FromArgb(215, 135, 255), Color.FromArgb(215, 175, 0), 
Color.FromArgb(215, 175, 95), Color.FromArgb(215, 175, 135), Color.FromArgb(215, 175, 175), Color.FromArgb(215, 175, 215), 
Color.FromArgb(215, 175, 255), Color.FromArgb(215, 215, 0), Color.FromArgb(215, 215, 95), Color.FromArgb(215, 215, 135), 
Color.FromArgb(215, 215, 175), Color.FromArgb(215, 215, 215), Color.FromArgb(215, 215, 255), Color.FromArgb(215, 255, 0), 
Color.FromArgb(215, 255, 95), Color.FromArgb(215, 255, 135), Color.FromArgb(215, 255, 175), Color.FromArgb(215, 255, 215), 
Color.FromArgb(215, 255, 255), Color.FromArgb(255, 0, 0), Color.FromArgb(255, 0, 95), Color.FromArgb(255, 0, 135), 
Color.FromArgb(255, 0, 175), Color.FromArgb(255, 0, 215), Color.FromArgb(255, 0, 255), Color.FromArgb(255, 95, 0), 
Color.FromArgb(255, 95, 95), Color.FromArgb(255, 95, 135), Color.FromArgb(255, 95, 175), Color.FromArgb(255, 95, 215), 
Color.FromArgb(255, 95, 255), Color.FromArgb(255, 135, 0), Color.FromArgb(255, 135, 95), Color.FromArgb(255, 135, 135), 
Color.FromArgb(255, 135, 175), Color.FromArgb(255, 135, 215), Color.FromArgb(255, 135, 255), Color.FromArgb(255, 175, 0), 
Color.FromArgb(255, 175, 95), Color.FromArgb(255, 175, 135), Color.FromArgb(255, 175, 175), Color.FromArgb(255, 175, 215), 
Color.FromArgb(255, 175, 255), Color.FromArgb(255, 215, 0), Color.FromArgb(255, 215, 95), Color.FromArgb(255, 215, 135), 
Color.FromArgb(255, 215, 175), Color.FromArgb(255, 215, 215), Color.FromArgb(255, 215, 255), Color.FromArgb(255, 255, 0), 
Color.FromArgb(255, 255, 95), Color.FromArgb(255, 255, 135), Color.FromArgb(255, 255, 175), Color.FromArgb(255, 255, 215), 
Color.FromArgb(255, 255, 255), Color.FromArgb(0, 0, 0), Color.FromArgb(18, 18, 18), Color.FromArgb(28, 28, 28), 
Color.FromArgb(38, 38, 38), Color.FromArgb(48, 48, 48), Color.FromArgb(58, 58, 58), Color.FromArgb(68, 68, 68), 
Color.FromArgb(78, 78, 78), Color.FromArgb(88, 88, 88), Color.FromArgb(98, 98, 98), Color.FromArgb(108, 108, 108), 
Color.FromArgb(118, 118, 118), Color.FromArgb(128, 128, 128), Color.FromArgb(138, 138, 138), Color.FromArgb(148, 148, 148), 
Color.FromArgb(158, 158, 158), Color.FromArgb(168, 168, 168), Color.FromArgb(178, 178, 178), Color.FromArgb(188, 188, 188), 
Color.FromArgb(198, 198, 198), Color.FromArgb(208, 208, 208), Color.FromArgb(218, 218, 218), Color.FromArgb(228, 228, 228), 
Color.FromArgb(238, 238, 238) };
Converting 256 Colors To 16
This function accepts one of the 256 colors (0 to 255) and finds its closest match to the 16 standard ANSI colors. If IsBack is true, it finds its closest match to the 8 dull ANSI colors, as standard ANSI did not allow for bright background colors.
    private int Color256To16(int ColorNum, bool IsBack)
   {
            int c;
            int[,] a16rgbtable;
            Color InColor = Utilities.Colors[ColorNum];
            int red = InColor.R;
            int green = InColor.G;
            int blue = InColor.B;

            a16rgbtable = new int[16, 3] { { 0, 0, 0 }, { 128, 0, 0 }, { 0, 128, 0 },
                                           { 128, 128, 0 }, { 0, 0, 128 }, { 128, 0, 128 },
                                           { 0, 128, 128 }, { 192, 192, 192 }, { 128, 128, 128 },
                                           { 255, 0, 0 }, { 0, 255, 0 }, { 255, 255, 0 },
                                           { 0, 0, 255 }, { 255, 0, 255 }, { 0, 255, 255 },
                                           { 255, 255, 255 } };

            int best_match = 0;
            double d, smallest_distance;

            smallest_distance = 10000000000.0;

            for (c = 0; c < (IsBack ? 8 : 16); c++)
            {
                d = Math.Pow(a16rgbtable[c, 0] - red, 2.0) +
                    Math.Pow(a16rgbtable[c, 1] - green, 2.0) +
                    Math.Pow(a16rgbtable[c, 2] - blue, 2.0);
                if (d < smallest_distance)
                {
                    smallest_distance = d;
                    best_match = c;
                }
            }

            return best_match;
    }