Skip to content

Commit

Permalink
add support for writing foreground / background indices to registry
Browse files Browse the repository at this point in the history
This functionality was implemented for the "current console" but was never implemented for writing to the registry, which affects all future consoles.
  • Loading branch information
waf committed Apr 23, 2019
1 parent b61cb83 commit 12fff31
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
27 changes: 24 additions & 3 deletions tools/ColorTool/ColorTool/ColorScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public ColorScheme(uint[] colorTable, ConsoleAttributes consoleAttributes)
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)
Expand All @@ -33,6 +45,18 @@ private static Color UIntToColor(uint color)
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))
Expand All @@ -55,9 +79,6 @@ private static double WeightedRGBSimilarity(uint c1, uint c2)
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)
Expand Down
12 changes: 4 additions & 8 deletions tools/ColorTool/ColorTool/ConsoleTargets/CurrentConsoleTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ public void ApplyColorScheme(ColorScheme colorScheme, bool quietMode)
{
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ class DefaultConsoleTarget : IConsoleTarget
{
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);
}
}
Expand Down
19 changes: 16 additions & 3 deletions tools/ColorTool/ColorTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ColorTool.ConsoleTargets;
using ColorTool.SchemeWriters;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ColorTool
Expand Down Expand Up @@ -117,19 +118,31 @@ static void Main(string[] args)
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();
}
}
}
Expand Down

0 comments on commit 12fff31

Please sign in to comment.