views:

43

answers:

1

Hi Im trying to add some color to this string for a simple Console app im creating. I would like each letter to be in a different color.

const string WelcomeMessage =     @" _______ _   _______      _______" + NewLine +
                                  @"|__   __(_) |__   __|    |__   __|   " + NewLine +
                                  @"   | |   _  ___| | __ _  ___| | ___   __" + NewLine +
                                  @"   | |  | |/ __| |/ _` |/ __| |/ _ \ / _ \    " + NewLine +
                                  @"   | |  | | (__| | (_| | (__| | (_) |  __/    " + NewLine +
                                  @"   |_|  |_|\___|_|\__,_|\___|_|\___/ \___|    "

I know that I could just use

Console.ForegroundColor = ConsoleColor.Blue; 
Console.Write(" _______"); 

and then write each part of the letter, but that would make my code almost impossible to read and maintain. So I just wanna know if there exist some kind of StringBuilder designet for Console output where its possible to include foregroundColor information.

+1  A: 

I doubt there is any well known API for it. But you can create a list of rectangles that mark each letter. The following example demonstrates this for the first three letters:

static void Main(string[] args)
{
  string WelcomeMessage =
                              @" _______ _   _______      _______          " + Environment.NewLine +
                              @"|__   __(_) |__   __|    |__   __|         " + Environment.NewLine +
                              @"   | |   _  ___| | __ _  ___| | ___   __   " + Environment.NewLine +
                              @"   | |  | |/ __| |/ _` |/ __| |/ _ \ / _ \ " + Environment.NewLine +
                              @"   | |  | | (__| | (_| | (__| | (_) |  __/ " + Environment.NewLine +
                              @"   |_|  |_|\___|_|\__,_|\___|_|\___/ \___| ";

  List<Rectangle> list = new List<Rectangle>();
  list.Add(new Rectangle(new Point(0, 0), new Size(7, 6)));
  list.Add(new Rectangle(new Point(8, 0), new Size(2, 6)));
  list.Add(new Rectangle(new Point(10, 2), new Size(4, 4)));

  Dictionary<Rectangle, ConsoleColor> colors = new Dictionary<Rectangle, ConsoleColor>();
  colors.Add(list[0], ConsoleColor.DarkBlue);
  colors.Add(list[1], ConsoleColor.DarkRed);
  colors.Add(list[2], ConsoleColor.DarkGreen);
  Console.WriteLine(WelcomeMessage);

  // NOTE: you might want to save the lines in an array where you define it:
  string[] lines = WelcomeMessage.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
  for (int y = 0; y < lines.Length; y++)
  {
    string line = lines[y];
    for (int x = 0; x < line.Length; x++)
    {
      Rectangle rect = list.Where(c =>
        x >= c.X && x <= c.X + c.Width && 
        y >= c.Y && y <= c.Y + c.Height).FirstOrDefault();

      if (rect == Rectangle.Empty)
        break ; // TODO Not implemented yet           
      else
      {            
        Console.ForegroundColor = colors[rect];
        Console.Write(line[x]);
      }
    }
    Console.WriteLine();
  }

  Console.ReadKey();      
}
steinar
Thanks alot steinar looks like its what I need, though a bit complicated :-)
gulbaek