Inline Color Code Processing

From XNAWiki
Jump to: navigation, search

Uses the IRC color numbers as a means of setting colors in the string. It uses ~ instead of the Ctrl+K. Scalable, meaning you can add as many colors as you want to from 0 to 99

The string "[~9System~] Game Initialized" will make System have a Lime color font while the rest will be white.

// Code
        public static Color[] colors = new Color[] { 
            Color.White, 
            Color.Black,
            Color.Navy,
            Color.Green,
            Color.Red,
            Color.Brown,
            Color.Purple,
            Color.Orange,
            Color.Yellow,
            Color.Lime,
            Color.Teal,
            Color.Aqua,
            Color.Blue,
            Color.Fuchsia,
            Color.Gray,
            Color.Silver
        };
 
        public static void DrawChat(SpriteBatch spriteBatch, SpriteFont font, Vector2 xy, string str)
        {
                    string[] splits = str.Split(new char[] { '~' }, StringSplitOptions.RemoveEmptyEntries);
 
                    int currentOffset = 0;
 
                    for (int j = 0; j < splits.Length; j++)
                    {
                        Color color = Color.White;
 
                        if (splits[j].Length > 2)
                        {
                            int number = 0;
 
                            // for double digits like 00 or 99
                            if (Char.IsNumber(splits[j][0]) && Char.IsNumber(splits[j][1])) {
                                number = int.Parse(splits[j][0] + "") * 10 + int.Parse(splits[j][1] + "");
                                splits[j] = splits[j].Substring(2);
                            } 
 
                            // for single digits 0-9
                            else if (Char.IsNumber(splits[j][0]) && !Char.IsNumber(splits[j][1])) {
                                number = int.Parse(splits[j][0] + "");
                                splits[j] = splits[j].Substring(1);
                            }
 
                            if (number < colors.Length && number >= 0) {
                                color = colors[number];
                            }
 
                        }
                        spriteBatch.DrawString(font, splits[j], xy + new Vector2(currentOffset, 0) + new Vector2(1), Color.Black);
                        spriteBatch.DrawString(font, splits[j], xy + new Vector2(currentOffset, 0), color);
                        currentOffset += (int)font.MeasureString(splits[j]).X;
                    }
        }


Here you can find another version of Inline Color Code Processing, I've optimized for memory usage, so if you have normal string without color don't load memory with split. squid <http://squareengine.codeplex.com>

public static void DrawChat(SpriteBatch spriteBatch, SpriteFont font, Vector2 xy, string str)
        {
 
            char chr_color = '$';
            string[] splits = str.Split(new char[] { chr_color }, StringSplitOptions.RemoveEmptyEntries);
 
            int currentOffset = 0;
 
            //Don't spend memory
            if (splits.Length > 1 || str.Contains(chr_color.ToString()))
            {
                for (int j = 0; j < splits.Length; j++)
                {
                    Color color = Color.White;
 
                    if (splits[j].Length > 2)
                    {
                        int number = 0;
 
                        // for double digits like 00 or 99
                        if (Char.IsNumber(splits[j][0]) && Char.IsNumber(splits[j][1]))
                        {
                            number = int.Parse(splits[j][0] + "") * 10 + int.Parse(splits[j][1] + "");
                            splits[j] = splits[j].Substring(2);
                        }
 
                        // for single digits 0-9
                        else if (Char.IsNumber(splits[j][0]) && !Char.IsNumber(splits[j][1]))
                        {
                            number = int.Parse(splits[j][0] + "");
                            splits[j] = splits[j].Substring(1);
                        }
 
                        if (number < colors.Length && number >= 0)
                        {
                            color = colors[number];
                        }
 
                    }
                    spriteBatch.DrawString(font, splits[j], xy + new Vector2(currentOffset, 0) + new Vector2(1), Color.Black);
                    spriteBatch.DrawString(font, splits[j], xy + new Vector2(currentOffset, 0), color);
                    currentOffset += (int)font.MeasureString(splits[j]).X;
 
                }
            }
            else
            {
                spriteBatch.DrawString(font, str, xy, colors[0]);
 
            }
        }