use more readable colours if player has a light-backgrounded terminal (#327)

This commit is contained in:
Jesse Plamondon-Willard 2017-07-13 18:30:46 -04:00
parent ae01396d9d
commit 48ced0336c
2 changed files with 53 additions and 9 deletions

View File

@ -4,6 +4,7 @@
For players: For players:
* The SMAPI console is now much simpler and easier-to-read. * The SMAPI console is now much simpler and easier-to-read.
* The SMAPI console now uses more readable colors in terminals with a light background.
For mod developers: For mod developers:
* Added API to edit XNB images & data loaded by the game (see [API reference](http://stardewvalleywiki.com/Modding:SMAPI_APIs#Content)). * Added API to edit XNB images & data loaded by the game (see [API reference](http://stardewvalleywiki.com/Modding:SMAPI_APIs#Content)).

View File

@ -25,15 +25,7 @@ namespace StardewModdingAPI.Framework
private static readonly int MaxLevelLength = (from level in Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>() select level.ToString().Length).Max(); private static readonly int MaxLevelLength = (from level in Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>() select level.ToString().Length).Max();
/// <summary>The console text color for each log level.</summary> /// <summary>The console text color for each log level.</summary>
private static readonly Dictionary<LogLevel, ConsoleColor> Colors = new Dictionary<LogLevel, ConsoleColor> private static readonly IDictionary<LogLevel, ConsoleColor> Colors = Monitor.GetConsoleColorScheme();
{
[LogLevel.Trace] = ConsoleColor.DarkGray,
[LogLevel.Debug] = ConsoleColor.DarkGray,
[LogLevel.Info] = ConsoleColor.White,
[LogLevel.Warn] = ConsoleColor.Yellow,
[LogLevel.Error] = ConsoleColor.Red,
[LogLevel.Alert] = ConsoleColor.Magenta
};
/// <summary>Propagates notification that SMAPI should exit.</summary> /// <summary>Propagates notification that SMAPI should exit.</summary>
private readonly CancellationTokenSource ExitTokenSource; private readonly CancellationTokenSource ExitTokenSource;
@ -172,5 +164,56 @@ namespace StardewModdingAPI.Framework
if (this.WriteToFile) if (this.WriteToFile)
this.LogFile.WriteLine(fullMessage); this.LogFile.WriteLine(fullMessage);
} }
/// <summary>Get the color scheme to use for the current console.</summary>
private static IDictionary<LogLevel, ConsoleColor> GetConsoleColorScheme()
{
#if !SMAPI_1_x
// scheme for dark console background
if (Monitor.IsDark(Console.BackgroundColor))
{
#endif
return new Dictionary<LogLevel, ConsoleColor>
{
[LogLevel.Trace] = ConsoleColor.DarkGray,
[LogLevel.Debug] = ConsoleColor.DarkGray,
[LogLevel.Info] = ConsoleColor.White,
[LogLevel.Warn] = ConsoleColor.Yellow,
[LogLevel.Error] = ConsoleColor.Red,
[LogLevel.Alert] = ConsoleColor.Magenta
};
#if !SMAPI_1_x
}
// scheme for light console background
return new Dictionary<LogLevel, ConsoleColor>
{
[LogLevel.Trace] = ConsoleColor.DarkGray,
[LogLevel.Debug] = ConsoleColor.DarkGray,
[LogLevel.Info] = ConsoleColor.Black,
[LogLevel.Warn] = ConsoleColor.DarkYellow,
[LogLevel.Error] = ConsoleColor.Red,
[LogLevel.Alert] = ConsoleColor.DarkMagenta
};
#endif
}
/// <summary>Get whether a console color should be considered dark, which is subjectively defined as 'white looks better than black on this text'.</summary>
/// <param name="color">The color to check.</param>
private static bool IsDark(ConsoleColor color)
{
switch (color)
{
case ConsoleColor.Black:
case ConsoleColor.Blue:
case ConsoleColor.DarkBlue:
case ConsoleColor.DarkRed:
case ConsoleColor.Red:
return true;
default:
return false;
}
}
} }
} }