Ad
Strings

Maybe it is my bias for having some dictionary, for the sake of extensibility

Code
Diff
  • using System.Collections.Generic;
    public class ColorAdder
    {
      private static Dictionary<string,int> codes = new(){
        { "Red", 31},
        { "Green", 32},
        { "Yellow", 33},
        { "Blue", 34},
        { "Magenta", 35},
        { "Cyan", 36},
        { "White", 37}
      };
        public static string AddColor(string color, string text)
          => codes.TryGetValue(color, out int v) ? $"\033[{v}m{text}\033[0m" :"";
        
    }
    
    • using System.Collections.Generic;
    • public class ColorAdder
    • {
    • private static Dictionary<string,int> codes = new(){
    • { "Red", 31},
    • { "Green", 32},
    • { "Yellow", 33},
    • { "Blue", 34},
    • { "Magenta", 35},
    • { "Cyan", 36},
    • { "White", 37}
    • };
    • public static string AddColor(string color, string text)
    • {
    • string Reset = "\033[0m";
    • return color switch{
    • "Red" => $"\033[31m{text}{Reset}",
    • "Green" => $"\033[32m{text}{Reset}",
    • "Yellow" => $"\033[33m{text}{Reset}",
    • "Blue" => $"\033[34m{text}{Reset}",
    • "Magenta" => $"\033[35m{text}{Reset}",
    • "Cyan" => $"\033[36m{text}{Reset}",
    • "White" => $"\033[37m{text}{Reset}",
    • _ => ""
    • };
    • }
    • => codes.TryGetValue(color, out int v) ? $"\033[{v}m{text}\033[0m" :"";
    • }