add support for writing foreground / background indices to registry

This functionality was implemented for the "current console" but was never implemented for writing to the registry, which affects all future consoles.
This commit is contained in:
Will Fuqua 2019-04-23 22:42:32 +07:00
parent b61cb830c3
commit 12fff3126b
No known key found for this signature in database
GPG key ID: 67B6F489167C16A3
4 changed files with 53 additions and 15 deletions

View file

@ -23,6 +23,18 @@ namespace ColorTool
public uint[] ColorTable { get; }
public ConsoleAttributes ConsoleAttributes { get; }
public ushort? ScreenColorAttributes =>
CalculateBackgroundForegroundAttributes(
this.ConsoleAttributes.Background,
this.ConsoleAttributes.Foreground
);
public ushort? PopupColorAttributes =>
CalculateBackgroundForegroundAttributes(
this.ConsoleAttributes.PopupBackground,
this.ConsoleAttributes.PopupForeground
);
public Color this[int index] => UIntToColor(ColorTable[index]);
private static Color UIntToColor(uint color)
@ -33,6 +45,18 @@ namespace ColorTool
return Color.FromArgb(r, g, b);
}
private ushort? CalculateBackgroundForegroundAttributes(uint? background, uint? foreground)
{
if(!(background.HasValue && foreground.HasValue))
{
return null;
}
int fgidx = this.CalculateIndex(foreground.Value);
int bgidx = this.CalculateIndex(background.Value);
var attributes = (ushort)(fgidx | (bgidx << 4));
return attributes;
}
public int CalculateIndex(uint value) =>
ColorTable.Select((color, idx) => Tuple.Create(color, idx))
.OrderBy(Difference(value))
@ -55,9 +79,6 @@ namespace ColorTool
return Math.Sqrt(dist[0] * (2 + rbar / 256.0) + dist[1] * 4 + dist[2] * (2 + (255 - rbar) / 256.0));
}
private static double Distance(uint[] c1c, uint[] c2c)
=> Math.Sqrt(c1c.Zip(c2c, (a, b) => Math.Pow((int)a - (int)b, 2)).Sum());
internal static uint[] RGB(uint c) => new[] { c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF };
internal static uint[] HSV(uint c)

View file

@ -28,17 +28,13 @@ namespace ColorTool.ConsoleTargets
{
csbiex.ColorTable[i] = colorScheme.ColorTable[i];
}
if (colorScheme.ConsoleAttributes.Background != null && colorScheme.ConsoleAttributes.Foreground != null)
if(colorScheme.ScreenColorAttributes is ushort wAttrs)
{
int fgidx = colorScheme.CalculateIndex(colorScheme.ConsoleAttributes.Foreground.Value);
int bgidx = colorScheme.CalculateIndex(colorScheme.ConsoleAttributes.Background.Value);
csbiex.wAttributes = (ushort)(fgidx | (bgidx << 4));
csbiex.wAttributes = wAttrs;
}
if (colorScheme.ConsoleAttributes.PopupBackground != null && colorScheme.ConsoleAttributes.PopupForeground != null)
if(colorScheme.PopupColorAttributes is ushort wPopupAttrs)
{
int fgidx = colorScheme.CalculateIndex(colorScheme.ConsoleAttributes.PopupForeground.Value);
int bgidx = colorScheme.CalculateIndex(colorScheme.ConsoleAttributes.PopupBackground.Value);
csbiex.wPopupAttributes = (ushort)(fgidx | (bgidx << 4));
csbiex.wPopupAttributes = wPopupAttrs;
}
SetConsoleScreenBufferInfoEx(hOut, ref csbiex);

View file

@ -15,13 +15,21 @@ namespace ColorTool.ConsoleTargets
{
public void ApplyColorScheme(ColorScheme colorScheme, bool quietMode)
{
//TODO
RegistryKey consoleKey = Registry.CurrentUser.OpenSubKey("Console", true);
for (int i = 0; i < colorScheme.ColorTable.Length; i++)
{
string valueName = "ColorTable" + (i < 10 ? "0" : "") + i;
consoleKey.SetValue(valueName, colorScheme.ColorTable[i], RegistryValueKind.DWord);
}
if(colorScheme.ScreenColorAttributes is ushort screenColors)
{
consoleKey.SetValue("ScreenColors", screenColors, RegistryValueKind.DWord);
}
if(colorScheme.PopupColorAttributes is ushort popupColors)
{
consoleKey.SetValue("PopupColors", popupColors, RegistryValueKind.DWord);
}
Console.WriteLine(Resources.WroteToDefaults);
}
}

View file

@ -6,6 +6,7 @@
using ColorTool.ConsoleTargets;
using ColorTool.SchemeWriters;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ColorTool
@ -117,19 +118,31 @@ namespace ColorTool
return;
}
foreach (var target in GetConsoleTargets())
{
target.ApplyColorScheme(colorScheme, quietMode);
}
}
/// <summary>
/// Returns an enumerable of consoles that we want to apply the colorscheme to.
/// The contents of this enumerable depends on the user's provided command line flags.
/// </summary>
public static IEnumerable<IConsoleTarget> GetConsoleTargets()
{
if (setDefaults)
{
new DefaultConsoleTarget().ApplyColorScheme(colorScheme, quietMode);
yield return new DefaultConsoleTarget();
}
if (setProperties)
{
if (setUnixStyle)
{
new VirtualTerminalConsoleTarget().ApplyColorScheme(colorScheme, quietMode);
yield return new VirtualTerminalConsoleTarget();
}
else
{
new CurrentConsoleTarget().ApplyColorScheme(colorScheme, quietMode);
yield return new CurrentConsoleTarget();
}
}
}