Merge pull request #400 from LokiMidgard/back-and-forground-index-export

Back and forground index export
This commit is contained in:
Michael Niksa 2019-04-02 10:21:23 -07:00 committed by GitHub
commit 619a80ea14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 126 additions and 18 deletions

View file

@ -11,8 +11,8 @@ namespace ColorTool
public class ColorScheme
{
public uint[] colorTable = null;
public uint? foreground = null;
public uint? background = null;
public ConsoleAttributes consoleAttributes;
public int CalculateIndex(uint value) =>
colorTable.Select((color, idx) => Tuple.Create(color, idx))
@ -80,14 +80,14 @@ namespace ColorTool
_dump($"Color[{i}]", colorTable[i]);
}
if (foreground != null)
if (consoleAttributes.foreground != null)
{
_dump("FG ", foreground.Value);
_dump("FG ", consoleAttributes.foreground.Value);
}
if (background != null)
if (consoleAttributes.background != null)
{
_dump("BG ", background.Value);
_dump("BG ", consoleAttributes.background.Value);
}
}
}

View file

@ -0,0 +1,17 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//
namespace ColorTool
{
public struct ConsoleAttributes
{
public uint? foreground;
public uint? background;
public uint? popupForeground;
public uint? popupBackground;
}
}

View file

@ -9,6 +9,7 @@ using System.Text;
using System.Runtime.InteropServices;
using static ColorTool.ConsoleAPI;
using System.IO;
using System.Collections.Generic;
namespace ColorTool
{
@ -85,6 +86,10 @@ namespace ColorTool
string[] tableStrings = new string[COLOR_TABLE_SIZE];
uint[] colorTable = null;
uint? foregroundColor = null;
uint? backgroundColor = null;
uint? popupForegroundColor = null;
uint? popupBackgroundColor = null;
for (int i = 0; i < COLOR_TABLE_SIZE; i++)
{
@ -113,6 +118,28 @@ namespace ColorTool
{
colorTable[i] = ParseColor(tableStrings[i]);
}
if (ReadAttributes("popup", out var foreground, out var background))
{
var foregroundIndex = (COLOR_NAMES as IList<string>).IndexOf(foreground);
var backgroundIndex = (COLOR_NAMES as IList<string>).IndexOf(background);
if (foregroundIndex != -1 && backgroundIndex != -1)
{
popupForegroundColor = colorTable[foregroundIndex];
popupBackgroundColor = colorTable[backgroundIndex];
}
}
if (ReadAttributes("screen", out foreground, out background))
{
var foregroundIndex = (COLOR_NAMES as IList<string>).IndexOf(foreground);
var backgroundIndex = (COLOR_NAMES as IList<string>).IndexOf(background);
if (foregroundIndex != -1 && backgroundIndex != -1)
{
foregroundColor = colorTable[foregroundIndex];
backgroundColor = colorTable[backgroundIndex];
}
}
}
catch (Exception /*e*/)
{
@ -127,12 +154,34 @@ namespace ColorTool
if (colorTable != null)
{
return new ColorScheme { colorTable = colorTable };
var consoleAttributes = new ConsoleAttributes { background = backgroundColor, foreground = foregroundColor, popupBackground = popupBackgroundColor, popupForeground = popupForegroundColor };
return new ColorScheme { colorTable = colorTable, consoleAttributes = consoleAttributes };
}
else
{
return null;
}
bool ReadAttributes(string section, out string foreground, out string background)
{
foreground = null;
background = null;
StringBuilder buffer = new StringBuilder(512);
GetPrivateProfileString(section, "FOREGROUND", null, buffer, 512, filename);
foreground = buffer.ToString();
if (!COLOR_NAMES.Contains(foreground))
return false;
buffer = new StringBuilder(512);
GetPrivateProfileString(section, "BACKGROUND", null, buffer, 512, filename);
background = buffer.ToString();
if (!COLOR_NAMES.Contains(background))
return false;
return true;
}
}
}
}

View file

@ -77,6 +77,26 @@ namespace ColorTool
colorTable[i] = ParseColor(node.InnerText);
}
uint? popupForeground = null;
uint? popupBackground = null;
var popupNode = children.OfType<XmlNode>().Where(n => n.Name == "popup_colors").SingleOrDefault();
if (popupNode != null)
{
var parts = popupNode.InnerText.Split(',');
if (parts.Length == 2)
{
var foregroundIndex = (CONCFG_COLOR_NAMES as IList<string>).IndexOf(parts[0]);
var backgroundIndex = (CONCFG_COLOR_NAMES as IList<string>).IndexOf(parts[1]);
if (foregroundIndex != -1 && backgroundIndex != -1)
{
popupForeground = colorTable[foregroundIndex];
popupBackground = colorTable[backgroundIndex];
}
}
}
uint? screenForeground = null;
uint? screenBackground = null;
@ -96,7 +116,8 @@ namespace ColorTool
}
}
return new ColorScheme { colorTable = colorTable, background = screenBackground, foreground = screenForeground };
var consoleAttributes = new ConsoleAttributes { background = screenBackground, foreground = screenForeground, popupBackground = popupBackground, popupForeground = popupForeground };
return new ColorScheme { colorTable = colorTable, consoleAttributes = consoleAttributes };
}
catch (Exception /*e*/)
{

View file

@ -206,7 +206,7 @@ namespace ColorTool
Console.ForegroundColor = currentForeground;
Console.BackgroundColor = currentBackground;
}
static void PrintTableWithVt()
{
// Save the current background and foreground colors.
@ -242,7 +242,7 @@ namespace ColorTool
"46m",
"47m"
};
Console.Write("\t");
for (int bg = 0; bg < BGs.Length; bg++)
{
@ -283,7 +283,7 @@ namespace ColorTool
}
else
{
Console.Write("\x1b[" +BGs[bg]);
Console.Write("\x1b[" + BGs[bg]);
}
Console.Write(test);
@ -370,12 +370,18 @@ namespace ColorTool
{
csbiex.ColorTable[i] = colorScheme.colorTable[i];
}
if(colorScheme.background != null && colorScheme.foreground != null)
if (colorScheme.consoleAttributes.background != null && colorScheme.consoleAttributes.foreground != null)
{
int fgidx = colorScheme.CalculateIndex(colorScheme.foreground.Value);
int bgidx = colorScheme.CalculateIndex(colorScheme.background.Value);
int fgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.foreground.Value);
int bgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.background.Value);
csbiex.wAttributes = (ushort)(fgidx | (bgidx << 4));
}
if (colorScheme.consoleAttributes.popupBackground != null && colorScheme.consoleAttributes.popupForeground != null)
{
int fgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.popupForeground.Value);
int bgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.popupBackground.Value);
csbiex.wPopupAttributes = (ushort)(fgidx | (bgidx << 4));
}
SetConsoleScreenBufferInfoEx(hOut, ref csbiex);
}
if (success)
@ -457,6 +463,20 @@ namespace ColorTool
line += r + "," + g + "," + b;
file.WriteLine(line);
}
file.WriteLine();
file.WriteLine("[screen]");
var forgroundIndex = csbiex.wAttributes & 0xF;
var backgroundIndex = csbiex.wAttributes >> 4;
file.WriteLine($"FOREGROUND = {IniSchemeParser.COLOR_NAMES[forgroundIndex]}");
file.WriteLine($"BACKGROUND = {IniSchemeParser.COLOR_NAMES[backgroundIndex]}");
file.WriteLine();
file.WriteLine("[popup]");
forgroundIndex = csbiex.wPopupAttributes & 0xF;
backgroundIndex = csbiex.wPopupAttributes >> 4;
file.WriteLine($"FOREGROUND = {IniSchemeParser.COLOR_NAMES[forgroundIndex]}");
file.WriteLine($"BACKGROUND = {IniSchemeParser.COLOR_NAMES[backgroundIndex]}");
}
}
catch (Exception ex)
@ -524,7 +544,7 @@ namespace ColorTool
break;
case "-o":
case "--output":
if (i+1 < args.Length)
if (i + 1 < args.Length)
{
ExportCurrentAsIni(args[i + 1]);
}
@ -575,7 +595,7 @@ namespace ColorTool
.Where(t => !t.IsAbstract && typeof(ISchemeParser).IsAssignableFrom(t))
.Select(t => (ISchemeParser)Activator.CreateInstance(t));
}
private static ColorScheme GetScheme(string schemeName, bool reportErrors = false)
{
foreach (var parser in GetParsers())
@ -587,6 +607,6 @@ namespace ColorTool
}
}
return null;
}
}
}
}

View file

@ -135,7 +135,8 @@ namespace ColorTool
return null;
}
return new ColorScheme { colorTable = colorTable, foreground = fgColor, background = bgColor };
var consoleAttributes = new ConsoleAttributes { foreground = fgColor, background = bgColor };
return new ColorScheme { colorTable = colorTable, consoleAttributes = consoleAttributes };
}
}
}