Add default background and foreground color as possible colors

This commit is contained in:
2024-09-10 08:42:44 +02:00
parent 47b0f5b456
commit 98fa812abf
7 changed files with 51 additions and 14 deletions

View File

@@ -1,3 +1,8 @@
[*]
tab_width = 4
indent_size = 4
indent_style = space
[*.cs] [*.cs]
dotnet_diagnostic.RCS1196.severity = none dotnet_diagnostic.RCS1196.severity = none
csharp_style_namespace_declarations = file_scoped:error csharp_style_namespace_declarations = file_scoped:error

View File

@@ -60,7 +60,7 @@ public static class Startup
DefaultForegroundColor: null, DefaultForegroundColor: null,
DefaultForegroundAccentColor: colorProvider.RedForeground, DefaultForegroundAccentColor: colorProvider.RedForeground,
DefaultBackgroundColor: null, DefaultBackgroundColor: null,
ElementColor: colorProvider.GrayForeground, ElementColor: colorProvider.DefaultForeground,
ContainerColor: colorProvider.BlueForeground, ContainerColor: colorProvider.BlueForeground,
MarkedItemForegroundColor: colorProvider.YellowForeground, MarkedItemForegroundColor: colorProvider.YellowForeground,
MarkedItemBackgroundColor: null, MarkedItemBackgroundColor: null,

View File

@@ -25,6 +25,7 @@ public class AnsiColorProvider : ColorProviderBase
public override IColor FromRgb(Rgb rgb, ColorType type) => new ColorRgb(rgb.R, rgb.G, rgb.B, type); public override IColor FromRgb(Rgb rgb, ColorType type) => new ColorRgb(rgb.R, rgb.G, rgb.B, type);
public override IColor DefaultForeground { get; } = Color256.DefaultForeground;
public override IColor BlackForeground { get; } = new Color256(0, ColorType.Foreground); public override IColor BlackForeground { get; } = new Color256(0, ColorType.Foreground);
public override IColor BlueForeground { get; } = new Color256(12, ColorType.Foreground); public override IColor BlueForeground { get; } = new Color256(12, ColorType.Foreground);
public override IColor CyanForeground { get; } = new Color256(14, ColorType.Foreground); public override IColor CyanForeground { get; } = new Color256(14, ColorType.Foreground);
@@ -42,6 +43,7 @@ public class AnsiColorProvider : ColorProviderBase
public override IColor WhiteForeground { get; } = new Color256(15, ColorType.Foreground); public override IColor WhiteForeground { get; } = new Color256(15, ColorType.Foreground);
public override IColor YellowForeground { get; } = new Color256(11, ColorType.Foreground); public override IColor YellowForeground { get; } = new Color256(11, ColorType.Foreground);
public override IColor DefaultBackground { get; } = Color256.DefaultBackground;
public override IColor BlackBackground { get; } = new Color256(0, ColorType.Background); public override IColor BlackBackground { get; } = new Color256(0, ColorType.Background);
public override IColor BlueBackground { get; } = new Color256(12, ColorType.Background); public override IColor BlueBackground { get; } = new Color256(12, ColorType.Background);
public override IColor CyanBackground { get; } = new Color256(14, ColorType.Background); public override IColor CyanBackground { get; } = new Color256(14, ColorType.Background);

View File

@@ -2,17 +2,42 @@
namespace TerminalUI.Color; namespace TerminalUI.Color;
public readonly record struct Color256(byte Color, ColorType Type) : IColor public readonly record struct Color256(byte? Color, ColorType Type) : IColor
{ {
public string ToConsoleColor() public string ToConsoleColor()
=> Type switch {
if (Color is null)
{
return string.Empty;
}
return Type switch
{ {
ColorType.Foreground => $"\x1b[38;5;{Color}m", ColorType.Foreground => $"\x1b[38;5;{Color}m",
ColorType.Background => $"\x1b[48;5;{Color}m", ColorType.Background => $"\x1b[48;5;{Color}m",
_ => throw new InvalidEnumArgumentException(nameof(Type), (int)Type, typeof(ColorType)) _ => throw new InvalidEnumArgumentException(nameof(Type), (int)Type, typeof(ColorType))
}; };
}
public IColor AsForeground() => this with {Type = ColorType.Foreground};
public static Color256 DefaultForeground { get; } = new(null, ColorType.Foreground);
public IColor AsBackground() => this with {Type = ColorType.Background}; public static Color256 DefaultBackground { get; } = new(null, ColorType.Background);
private static Color256 DefaultForegroundAsBackground { get; } = new(7, ColorType.Background);
private static Color256 DefaultBackgroundAsForeground { get; } = new(0, ColorType.Foreground);
private static Dictionary<Color256, Color256> _defaultColorMapping = new()
{
{ DefaultForeground, DefaultForegroundAsBackground },
{ DefaultBackground, DefaultBackgroundAsForeground },
{ DefaultForegroundAsBackground, DefaultForeground },
{ DefaultBackgroundAsForeground, DefaultBackground },
};
public IColor AsForeground()
=> _defaultColorMapping.TryGetValue(this, out var color)
? color
: this with { Type = ColorType.Foreground };
public IColor AsBackground()
=> _defaultColorMapping.TryGetValue(this, out var color)
? color
: this with { Type = ColorType.Background };
} }

View File

@@ -82,6 +82,7 @@ public abstract class ColorProviderBase : IColorProvider
_ => null _ => null
}; };
public abstract IColor DefaultForeground { get; }
public abstract IColor BlackForeground { get; } public abstract IColor BlackForeground { get; }
public abstract IColor BlueForeground { get; } public abstract IColor BlueForeground { get; }
public abstract IColor CyanForeground { get; } public abstract IColor CyanForeground { get; }
@@ -99,6 +100,7 @@ public abstract class ColorProviderBase : IColorProvider
public abstract IColor WhiteForeground { get; } public abstract IColor WhiteForeground { get; }
public abstract IColor YellowForeground { get; } public abstract IColor YellowForeground { get; }
public abstract IColor DefaultBackground { get; }
public abstract IColor BlackBackground { get; } public abstract IColor BlackBackground { get; }
public abstract IColor BlueBackground { get; } public abstract IColor BlueBackground { get; }
public abstract IColor CyanBackground { get; } public abstract IColor CyanBackground { get; }

View File

@@ -73,6 +73,7 @@ public class ConsoleColorProvider : ColorProviderBase
protected IColor ApproximateColor(Rgb rgb, IEnumerable<(Rgb, IColor)> colors) protected IColor ApproximateColor(Rgb rgb, IEnumerable<(Rgb, IColor)> colors)
=> colors.MinBy(c => c.Item1 - rgb).Item2; => colors.MinBy(c => c.Item1 - rgb).Item2;
public override IColor DefaultForeground => _blackForeground;
public override IColor BlackForeground => _blackForeground; public override IColor BlackForeground => _blackForeground;
public override IColor BlueForeground => _blueForeground; public override IColor BlueForeground => _blueForeground;
public override IColor CyanForeground => _cyanForeground; public override IColor CyanForeground => _cyanForeground;
@@ -90,6 +91,7 @@ public class ConsoleColorProvider : ColorProviderBase
public override IColor WhiteForeground => _whiteForeground; public override IColor WhiteForeground => _whiteForeground;
public override IColor YellowForeground => _yellowForeground; public override IColor YellowForeground => _yellowForeground;
public override IColor DefaultBackground => _blackBackground;
public override IColor BlackBackground => _blackBackground; public override IColor BlackBackground => _blackBackground;
public override IColor BlueBackground => _blueBackground; public override IColor BlueBackground => _blueBackground;
public override IColor CyanBackground => _cyanBackground; public override IColor CyanBackground => _cyanBackground;

View File

@@ -5,6 +5,7 @@ public interface IColorProvider
IColor Parse(string color, ColorType type); IColor Parse(string color, ColorType type);
IColor FromRgb(Rgb rgb, ColorType type); IColor FromRgb(Rgb rgb, ColorType type);
IColor DefaultForeground { get; }
IColor BlackForeground { get; } IColor BlackForeground { get; }
IColor BlueForeground { get; } IColor BlueForeground { get; }
IColor CyanForeground { get; } IColor CyanForeground { get; }
@@ -22,7 +23,7 @@ public interface IColorProvider
IColor WhiteForeground { get; } IColor WhiteForeground { get; }
IColor YellowForeground { get; } IColor YellowForeground { get; }
IColor DefaultBackground { get; }
IColor BlackBackground { get; } IColor BlackBackground { get; }
IColor BlueBackground { get; } IColor BlueBackground { get; }
IColor CyanBackground { get; } IColor CyanBackground { get; }