文字列をメソッドで色付けする必要があるので、Console.ForegroundColor
プロパティを使用して後でテキストを書き込みますが、どこかで間違えたため、すべての行が1色になっています。または、より良い解決策はありますか?文字列を&0-&f(16進数)で色付けしてコンソールに出力する必要があります。これが私の解決策です。
public static void ColorizeConsoleMessage(string message)
{
var matches = Regex.Matches(message, "&+[0-9a-f]");
var split = Regex.Split(message, "&+[0-9a-f]");
var def = Console.ForegroundColor;
var i = 0;
foreach (var match in matches)
{
switch (match.ToString().Replace("&", "").ToCharArray()[0])
{
case '0':
Console.ForegroundColor = ConsoleColor.White;
break;
case '1':
Console.ForegroundColor = ConsoleColor.Gray;
break;
case '2':
Console.ForegroundColor = ConsoleColor.DarkGray;
break;
case '3':
Console.ForegroundColor = ConsoleColor.Black;
break;
case '4':
Console.ForegroundColor = ConsoleColor.Red;
break;
case '5':
Console.ForegroundColor = ConsoleColor.Green;
break;
case '6':
Console.ForegroundColor = ConsoleColor.Blue;
break;
case '7':
Console.ForegroundColor = ConsoleColor.Yellow;
break;
default:
Console.ForegroundColor = ConsoleColor.White;
break;
}
Console.Write(split[i]);
i++;
}
Console.WriteLine();
Console.ForegroundColor = def;
}
そしてテスト:EventManager.ColorizeConsoleMessage("&4Hello, &6world!");