replace mutable public fields with properties
The properties are made readonly where possible, which is possible in almost all cases.
This commit is contained in:
parent
7daea0a25c
commit
05f518db5b
|
@ -9,12 +9,21 @@ using System.Linq;
|
|||
|
||||
namespace ColorTool
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a colorscheme that can be applied to a console.
|
||||
/// </summary>
|
||||
public class ColorScheme
|
||||
{
|
||||
public uint[] colorTable = null;
|
||||
public ConsoleAttributes consoleAttributes;
|
||||
public ColorScheme(uint[] colorTable, ConsoleAttributes consoleAttributes)
|
||||
{
|
||||
ColorTable = colorTable;
|
||||
ConsoleAttributes = consoleAttributes;
|
||||
}
|
||||
|
||||
public Color this[int index] => UIntToColor(colorTable[index]);
|
||||
public uint[] ColorTable { get; }
|
||||
public ConsoleAttributes ConsoleAttributes { get; }
|
||||
|
||||
public Color this[int index] => UIntToColor(ColorTable[index]);
|
||||
|
||||
private static Color UIntToColor(uint color)
|
||||
{
|
||||
|
@ -25,7 +34,7 @@ namespace ColorTool
|
|||
}
|
||||
|
||||
public int CalculateIndex(uint value) =>
|
||||
colorTable.Select((color, idx) => Tuple.Create(color, idx))
|
||||
ColorTable.Select((color, idx) => Tuple.Create(color, idx))
|
||||
.OrderBy(Difference(value))
|
||||
.First().Item2;
|
||||
|
||||
|
@ -87,17 +96,17 @@ namespace ColorTool
|
|||
|
||||
for (int i = 0; i < 16; ++i)
|
||||
{
|
||||
_dump($"Color[{i}]", colorTable[i]);
|
||||
_dump($"Color[{i}]", ColorTable[i]);
|
||||
}
|
||||
|
||||
if (consoleAttributes.foreground != null)
|
||||
if (ConsoleAttributes.Foreground != null)
|
||||
{
|
||||
_dump("FG ", consoleAttributes.foreground.Value);
|
||||
_dump("FG ", ConsoleAttributes.Foreground.Value);
|
||||
}
|
||||
|
||||
if (consoleAttributes.background != null)
|
||||
if (ConsoleAttributes.Background != null)
|
||||
{
|
||||
_dump("BG ", consoleAttributes.background.Value);
|
||||
_dump("BG ", ConsoleAttributes.Background.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,61 +4,102 @@
|
|||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ColorTool
|
||||
{
|
||||
/// <summary>
|
||||
/// Displays the color table that demonstrates the current colorscheme.
|
||||
/// </summary>
|
||||
class ColorTable
|
||||
{
|
||||
static int DARK_BLACK = 0;
|
||||
static int DARK_BLUE = 1;
|
||||
static int DARK_GREEN = 2;
|
||||
static int DARK_CYAN = 3;
|
||||
static int DARK_RED = 4;
|
||||
static int DARK_MAGENTA = 5;
|
||||
static int DARK_YELLOW = 6;
|
||||
static int DARK_WHITE = 7;
|
||||
static int BRIGHT_BLACK = 8;
|
||||
static int BRIGHT_BLUE = 9;
|
||||
static int BRIGHT_GREEN = 10;
|
||||
static int BRIGHT_CYAN = 11;
|
||||
static int BRIGHT_RED = 12;
|
||||
static int BRIGHT_MAGENTA = 13;
|
||||
static int BRIGHT_YELLOW = 14;
|
||||
static int BRIGHT_WHITE = 15;
|
||||
const int DARK_BLACK = 0;
|
||||
const int DARK_BLUE = 1;
|
||||
const int DARK_GREEN = 2;
|
||||
const int DARK_CYAN = 3;
|
||||
const int DARK_RED = 4;
|
||||
const int DARK_MAGENTA = 5;
|
||||
const int DARK_YELLOW = 6;
|
||||
const int DARK_WHITE = 7;
|
||||
const int BRIGHT_BLACK = 8;
|
||||
const int BRIGHT_BLUE = 9;
|
||||
const int BRIGHT_GREEN = 10;
|
||||
const int BRIGHT_CYAN = 11;
|
||||
const int BRIGHT_RED = 12;
|
||||
const int BRIGHT_MAGENTA = 13;
|
||||
const int BRIGHT_YELLOW = 14;
|
||||
const int BRIGHT_WHITE = 15;
|
||||
|
||||
// This is the order of colors when output by the table.
|
||||
static int[] outputFgs = {
|
||||
BRIGHT_WHITE ,
|
||||
DARK_BLACK ,
|
||||
BRIGHT_BLACK ,
|
||||
DARK_RED ,
|
||||
BRIGHT_RED ,
|
||||
DARK_GREEN ,
|
||||
BRIGHT_GREEN ,
|
||||
DARK_YELLOW ,
|
||||
BRIGHT_YELLOW ,
|
||||
DARK_BLUE ,
|
||||
BRIGHT_BLUE ,
|
||||
DARK_MAGENTA ,
|
||||
BRIGHT_MAGENTA ,
|
||||
DARK_CYAN ,
|
||||
BRIGHT_CYAN ,
|
||||
DARK_WHITE ,
|
||||
private static readonly IReadOnlyList<int> outputFgs = new[]
|
||||
{
|
||||
BRIGHT_WHITE,
|
||||
DARK_BLACK,
|
||||
BRIGHT_BLACK,
|
||||
DARK_RED,
|
||||
BRIGHT_RED,
|
||||
DARK_GREEN,
|
||||
BRIGHT_GREEN,
|
||||
DARK_YELLOW,
|
||||
BRIGHT_YELLOW,
|
||||
DARK_BLUE,
|
||||
BRIGHT_BLUE,
|
||||
DARK_MAGENTA,
|
||||
BRIGHT_MAGENTA,
|
||||
DARK_CYAN,
|
||||
BRIGHT_CYAN,
|
||||
DARK_WHITE,
|
||||
BRIGHT_WHITE
|
||||
};
|
||||
|
||||
private const string TestText = " gYw ";
|
||||
|
||||
static int[] saneBgs = {
|
||||
DARK_BLACK ,
|
||||
DARK_RED ,
|
||||
DARK_GREEN ,
|
||||
DARK_YELLOW ,
|
||||
DARK_BLUE ,
|
||||
DARK_MAGENTA ,
|
||||
DARK_CYAN ,
|
||||
DARK_WHITE
|
||||
private static readonly IReadOnlyList<string> FGs = new[]
|
||||
{
|
||||
"m",
|
||||
"1m",
|
||||
"30m",
|
||||
"1;30m",
|
||||
"31m",
|
||||
"1;31m",
|
||||
"32m",
|
||||
"1;32m",
|
||||
"33m",
|
||||
"1;33m",
|
||||
"34m",
|
||||
"1;34m",
|
||||
"35m",
|
||||
"1;35m",
|
||||
"36m",
|
||||
"1;36m",
|
||||
"37m",
|
||||
"1;37m"
|
||||
};
|
||||
|
||||
private static readonly IReadOnlyList<string> BGs = new[]
|
||||
{
|
||||
"m",
|
||||
"40m",
|
||||
"41m",
|
||||
"42m",
|
||||
"43m",
|
||||
"44m",
|
||||
"45m",
|
||||
"46m",
|
||||
"47m"
|
||||
};
|
||||
|
||||
private static readonly IReadOnlyList<int> saneBgs = new[]
|
||||
{
|
||||
DARK_BLACK,
|
||||
DARK_RED,
|
||||
DARK_GREEN,
|
||||
DARK_YELLOW,
|
||||
DARK_BLUE,
|
||||
DARK_MAGENTA,
|
||||
DARK_CYAN,
|
||||
DARK_WHITE
|
||||
};
|
||||
|
||||
public static void PrintTable()
|
||||
{
|
||||
|
@ -66,41 +107,9 @@ namespace ColorTool
|
|||
// Save the current background and foreground colors.
|
||||
ConsoleColor currentBackground = Console.BackgroundColor;
|
||||
ConsoleColor currentForeground = Console.ForegroundColor;
|
||||
string test = " gYw ";
|
||||
string[] FGs = {
|
||||
"m",
|
||||
"1m",
|
||||
"30m",
|
||||
"1;30m",
|
||||
"31m",
|
||||
"1;31m",
|
||||
"32m",
|
||||
"1;32m",
|
||||
"33m",
|
||||
"1;33m",
|
||||
"34m",
|
||||
"1;34m",
|
||||
"35m",
|
||||
"1;35m",
|
||||
"36m",
|
||||
"1;36m",
|
||||
"37m",
|
||||
"1;37m"
|
||||
};
|
||||
string[] BGs = {
|
||||
"m",
|
||||
"40m",
|
||||
"41m",
|
||||
"42m",
|
||||
"43m",
|
||||
"44m",
|
||||
"45m",
|
||||
"46m",
|
||||
"47m"
|
||||
};
|
||||
|
||||
Console.Write("\t");
|
||||
for (int bg = 0; bg < BGs.Length; bg++)
|
||||
for (int bg = 0; bg < BGs.Count; bg++)
|
||||
{
|
||||
if (bg > 0) Console.Write(" ");
|
||||
Console.Write(" ");
|
||||
|
@ -109,7 +118,7 @@ namespace ColorTool
|
|||
}
|
||||
Console.WriteLine();
|
||||
|
||||
for (int fg = 0; fg < FGs.Length; fg++)
|
||||
for (int fg = 0; fg < FGs.Count; fg++)
|
||||
{
|
||||
Console.ForegroundColor = currentForeground;
|
||||
Console.BackgroundColor = currentBackground;
|
||||
|
@ -119,13 +128,13 @@ namespace ColorTool
|
|||
if (fg == 0) Console.ForegroundColor = currentForeground;
|
||||
else Console.ForegroundColor = colors[outputFgs[fg - 1]];
|
||||
|
||||
for (int bg = 0; bg < BGs.Length; bg++)
|
||||
for (int bg = 0; bg < BGs.Count; bg++)
|
||||
{
|
||||
if (bg > 0) Console.Write(" ");
|
||||
if (bg == 0)
|
||||
Console.BackgroundColor = currentBackground;
|
||||
else Console.BackgroundColor = colors[saneBgs[bg - 1]];
|
||||
Console.Write(test);
|
||||
Console.Write(TestText);
|
||||
Console.BackgroundColor = currentBackground;
|
||||
}
|
||||
Console.Write("\n");
|
||||
|
@ -140,42 +149,8 @@ namespace ColorTool
|
|||
|
||||
public static void PrintTableWithVt()
|
||||
{
|
||||
// Save the current background and foreground colors.
|
||||
string test = " gYw ";
|
||||
string[] FGs = {
|
||||
"m",
|
||||
"1m",
|
||||
"30m",
|
||||
"1;30m",
|
||||
"31m",
|
||||
"1;31m",
|
||||
"32m",
|
||||
"1;32m",
|
||||
"33m",
|
||||
"1;33m",
|
||||
"34m",
|
||||
"1;34m",
|
||||
"35m",
|
||||
"1;35m",
|
||||
"36m",
|
||||
"1;36m",
|
||||
"37m",
|
||||
"1;37m"
|
||||
};
|
||||
string[] BGs = {
|
||||
"m",
|
||||
"40m",
|
||||
"41m",
|
||||
"42m",
|
||||
"43m",
|
||||
"44m",
|
||||
"45m",
|
||||
"46m",
|
||||
"47m"
|
||||
};
|
||||
|
||||
Console.Write("\t");
|
||||
for (int bg = 0; bg < BGs.Length; bg++)
|
||||
for (int bg = 0; bg < BGs.Count; bg++)
|
||||
{
|
||||
if (bg > 0) Console.Write(" ");
|
||||
Console.Write(" ");
|
||||
|
@ -184,7 +159,7 @@ namespace ColorTool
|
|||
}
|
||||
Console.WriteLine();
|
||||
|
||||
for (int fg = 0; fg < FGs.Length; fg++)
|
||||
for (int fg = 0; fg < FGs.Count; fg++)
|
||||
{
|
||||
Console.Write("\x1b[m");
|
||||
|
||||
|
@ -202,7 +177,7 @@ namespace ColorTool
|
|||
Console.Write("\x1b[" + FGs[fg]);
|
||||
}
|
||||
|
||||
for (int bg = 0; bg < BGs.Length; bg++)
|
||||
for (int bg = 0; bg < BGs.Count; bg++)
|
||||
{
|
||||
if (bg > 0)
|
||||
{
|
||||
|
@ -217,7 +192,7 @@ namespace ColorTool
|
|||
Console.Write("\x1b[" + BGs[bg]);
|
||||
}
|
||||
|
||||
Console.Write(test);
|
||||
Console.Write(TestText);
|
||||
Console.Write("\x1b[49m");
|
||||
}
|
||||
Console.Write("\n");
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net46</TargetFramework>
|
||||
<TargetFramework>net461</TargetFramework>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
|
@ -5,12 +5,19 @@
|
|||
|
||||
namespace ColorTool
|
||||
{
|
||||
public struct ConsoleAttributes
|
||||
public readonly struct ConsoleAttributes
|
||||
{
|
||||
public uint? foreground;
|
||||
public uint? background;
|
||||
public ConsoleAttributes(uint? background, uint? foreground, uint? popupBackground, uint? popupForeground)
|
||||
{
|
||||
Background = background;
|
||||
Foreground = foreground;
|
||||
PopupBackground = popupBackground;
|
||||
PopupForeground = popupForeground;
|
||||
}
|
||||
|
||||
public uint? popupForeground;
|
||||
public uint? popupBackground;
|
||||
public uint? Foreground { get; }
|
||||
public uint? Background { get; }
|
||||
public uint? PopupForeground { get; }
|
||||
public uint? PopupBackground { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,18 +26,18 @@ namespace ColorTool.ConsoleTargets
|
|||
csbiex.srWindow.Bottom++;
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
csbiex.ColorTable[i] = colorScheme.colorTable[i];
|
||||
csbiex.ColorTable[i] = colorScheme.ColorTable[i];
|
||||
}
|
||||
if (colorScheme.consoleAttributes.background != null && colorScheme.consoleAttributes.foreground != null)
|
||||
if (colorScheme.ConsoleAttributes.Background != null && colorScheme.ConsoleAttributes.Foreground != null)
|
||||
{
|
||||
int fgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.foreground.Value);
|
||||
int bgidx = colorScheme.CalculateIndex(colorScheme.consoleAttributes.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)
|
||||
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);
|
||||
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);
|
||||
|
|
|
@ -17,10 +17,10 @@ namespace ColorTool.ConsoleTargets
|
|||
{
|
||||
//TODO
|
||||
RegistryKey consoleKey = Registry.CurrentUser.OpenSubKey("Console", true);
|
||||
for (int i = 0; i < colorScheme.colorTable.Length; i++)
|
||||
for (int i = 0; i < colorScheme.ColorTable.Length; i++)
|
||||
{
|
||||
string valueName = "ColorTable" + (i < 10 ? "0" : "") + i;
|
||||
consoleKey.SetValue(valueName, colorScheme.colorTable[i], RegistryValueKind.DWord);
|
||||
consoleKey.SetValue(valueName, colorScheme.ColorTable[i], RegistryValueKind.DWord);
|
||||
}
|
||||
Console.WriteLine(Resources.WroteToDefaults);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using static ColorTool.ConsoleAPI;
|
||||
|
||||
|
@ -15,7 +16,8 @@ namespace ColorTool.ConsoleTargets
|
|||
class VirtualTerminalConsoleTarget : IConsoleTarget
|
||||
{
|
||||
// Use a Console index in to get a VT index out.
|
||||
static readonly int[] VT_INDICIES = {
|
||||
public static readonly IReadOnlyList<int> VT_INDICIES = new[]
|
||||
{
|
||||
0, // DARK_BLACK
|
||||
4, // DARK_BLUE
|
||||
2, // DARK_GREEN
|
||||
|
@ -31,7 +33,7 @@ namespace ColorTool.ConsoleTargets
|
|||
8+1, // BRIGHT_RED
|
||||
8+5, // BRIGHT_MAGENTA
|
||||
8+3, // BRIGHT_YELLOW
|
||||
8+7,// BRIGHT_WHITE
|
||||
8+7, // BRIGHT_WHITE
|
||||
};
|
||||
|
||||
public void ApplyColorScheme(ColorScheme colorScheme, bool quietMode)
|
||||
|
@ -46,7 +48,7 @@ namespace ColorTool.ConsoleTargets
|
|||
SetConsoleMode(hOut, requestedMode);
|
||||
}
|
||||
|
||||
for (int i = 0; i < colorScheme.colorTable.Length; i++)
|
||||
for (int i = 0; i < colorScheme.ColorTable.Length; i++)
|
||||
{
|
||||
int vtIndex = VT_INDICIES[i];
|
||||
Color color = colorScheme[i];
|
||||
|
|
|
@ -19,7 +19,8 @@ namespace ColorTool.SchemeParsers
|
|||
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
|
||||
|
||||
// These are in Windows Color table order - BRG, not RGB.
|
||||
public static string[] COLOR_NAMES = {
|
||||
internal static readonly IReadOnlyList<string> COLOR_NAMES = new[]
|
||||
{
|
||||
"DARK_BLACK",
|
||||
"DARK_BLUE",
|
||||
"DARK_GREEN",
|
||||
|
@ -38,44 +39,7 @@ namespace ColorTool.SchemeParsers
|
|||
"BRIGHT_WHITE"
|
||||
};
|
||||
|
||||
public string Name => "INI File Parser";
|
||||
|
||||
static uint ParseHex(string arg)
|
||||
{
|
||||
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(arg);
|
||||
return RGB(col.R, col.G, col.B);
|
||||
}
|
||||
|
||||
static uint ParseRgb(string arg)
|
||||
{
|
||||
int[] components = { 0, 0, 0 };
|
||||
string[] args = arg.Split(',');
|
||||
if (args.Length != components.Length) throw new Exception("Invalid color format \"" + arg + "\"");
|
||||
if (args.Length != 3) throw new Exception("Invalid color format \"" + arg + "\"");
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
components[i] = Int32.Parse(args[i]);
|
||||
}
|
||||
|
||||
return RGB(components[0], components[1], components[2]);
|
||||
}
|
||||
|
||||
static uint ParseColor(string arg)
|
||||
{
|
||||
if (arg[0] == '#')
|
||||
{
|
||||
return ParseHex(arg.Substring(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
return ParseRgb(arg);
|
||||
}
|
||||
}
|
||||
|
||||
static string FindIniScheme(string schemeName)
|
||||
{
|
||||
return SchemeManager.GetSearchPaths(schemeName, ".ini").FirstOrDefault(File.Exists);
|
||||
}
|
||||
public string Name { get; } = "INI File Parser";
|
||||
|
||||
public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
|
||||
{
|
||||
|
@ -154,8 +118,8 @@ namespace ColorTool.SchemeParsers
|
|||
|
||||
if (colorTable != null)
|
||||
{
|
||||
var consoleAttributes = new ConsoleAttributes { background = backgroundColor, foreground = foregroundColor, popupBackground = popupBackgroundColor, popupForeground = popupForegroundColor };
|
||||
return new ColorScheme { colorTable = colorTable, consoleAttributes = consoleAttributes };
|
||||
var consoleAttributes = new ConsoleAttributes(backgroundColor, foregroundColor, popupBackgroundColor, popupForegroundColor);
|
||||
return new ColorScheme(colorTable, consoleAttributes);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -183,5 +147,42 @@ namespace ColorTool.SchemeParsers
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static uint ParseHex(string arg)
|
||||
{
|
||||
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(arg);
|
||||
return RGB(col.R, col.G, col.B);
|
||||
}
|
||||
|
||||
private static uint ParseRgb(string arg)
|
||||
{
|
||||
int[] components = { 0, 0, 0 };
|
||||
string[] args = arg.Split(',');
|
||||
if (args.Length != components.Length) throw new Exception("Invalid color format \"" + arg + "\"");
|
||||
if (args.Length != 3) throw new Exception("Invalid color format \"" + arg + "\"");
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
components[i] = Int32.Parse(args[i]);
|
||||
}
|
||||
|
||||
return RGB(components[0], components[1], components[2]);
|
||||
}
|
||||
|
||||
private static uint ParseColor(string arg)
|
||||
{
|
||||
if (arg[0] == '#')
|
||||
{
|
||||
return ParseHex(arg.Substring(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
return ParseRgb(arg);
|
||||
}
|
||||
}
|
||||
|
||||
private static string FindIniScheme(string schemeName)
|
||||
{
|
||||
return SchemeManager.GetSearchPaths(schemeName, ".ini").FirstOrDefault(File.Exists);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,8 @@ namespace ColorTool.SchemeParsers
|
|||
{
|
||||
class JsonParser : ISchemeParser
|
||||
{
|
||||
static string[] CONCFG_COLOR_NAMES = {
|
||||
private static IReadOnlyList<string> CONCFG_COLOR_NAMES = new[]
|
||||
{
|
||||
"black", // DARK_BLACK
|
||||
"dark_blue", // DARK_BLUE
|
||||
"dark_green", // DARK_GREEN
|
||||
|
@ -34,34 +35,7 @@ namespace ColorTool.SchemeParsers
|
|||
"white" // BRIGHT_WHITE
|
||||
};
|
||||
|
||||
public string Name => "concfg Parser";
|
||||
|
||||
static uint ParseColor(string arg)
|
||||
{
|
||||
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(arg);
|
||||
return RGB(col.R, col.G, col.B);
|
||||
}
|
||||
|
||||
static XmlDocument loadJsonFile(string schemeName)
|
||||
{
|
||||
XmlDocument xmlDoc = new XmlDocument();
|
||||
foreach (string path in SchemeManager.GetSearchPaths(schemeName, ".json")
|
||||
.Where(File.Exists))
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = File.ReadAllBytes(path);
|
||||
var reader = JsonReaderWriterFactory.CreateJsonReader(data, System.Xml.XmlDictionaryReaderQuotas.Max);
|
||||
xmlDoc.Load(reader);
|
||||
return xmlDoc;
|
||||
}
|
||||
catch (XmlException /*e*/) { /* failed to parse */ }
|
||||
catch (IOException /*e*/) { /* failed to find */ }
|
||||
catch (UnauthorizedAccessException /*e*/) { /* unauthorized */ }
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public string Name { get; } = "concfg Parser";
|
||||
|
||||
public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
|
||||
{
|
||||
|
@ -119,18 +93,45 @@ namespace ColorTool.SchemeParsers
|
|||
}
|
||||
}
|
||||
|
||||
var consoleAttributes = new ConsoleAttributes { background = screenBackground, foreground = screenForeground, popupBackground = popupBackground, popupForeground = popupForeground };
|
||||
return new ColorScheme { colorTable = colorTable, consoleAttributes = consoleAttributes };
|
||||
var consoleAttributes = new ConsoleAttributes(screenBackground, screenForeground, popupBackground, popupForeground);
|
||||
return new ColorScheme(colorTable, consoleAttributes);
|
||||
}
|
||||
catch (Exception /*e*/)
|
||||
{
|
||||
if (reportErrors)
|
||||
{
|
||||
Console.WriteLine("failes to load json scheme");
|
||||
Console.WriteLine("failed to load json scheme");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static uint ParseColor(string arg)
|
||||
{
|
||||
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(arg);
|
||||
return RGB(col.R, col.G, col.B);
|
||||
}
|
||||
|
||||
private static XmlDocument loadJsonFile(string schemeName)
|
||||
{
|
||||
XmlDocument xmlDoc = new XmlDocument();
|
||||
foreach (string path in SchemeManager.GetSearchPaths(schemeName, ".json")
|
||||
.Where(File.Exists))
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = File.ReadAllBytes(path);
|
||||
var reader = JsonReaderWriterFactory.CreateJsonReader(data, System.Xml.XmlDictionaryReaderQuotas.Max);
|
||||
xmlDoc.Load(reader);
|
||||
return xmlDoc;
|
||||
}
|
||||
catch (XmlException /*e*/) { /* failed to parse */ }
|
||||
catch (IOException /*e*/) { /* failed to find */ }
|
||||
catch (UnauthorizedAccessException /*e*/) { /* unauthorized */ }
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
@ -15,8 +16,9 @@ namespace ColorTool.SchemeParsers
|
|||
class XmlSchemeParser : ISchemeParser
|
||||
{
|
||||
// In Windows Color Table order
|
||||
static string[] PLIST_COLOR_NAMES = {
|
||||
"Ansi 0 Color", // DARK_BLACK
|
||||
private static readonly string[] PLIST_COLOR_NAMES =
|
||||
{
|
||||
"Ansi 0 Color", // DARK_BLACK
|
||||
"Ansi 4 Color", // DARK_BLUE
|
||||
"Ansi 2 Color", // DARK_GREEN
|
||||
"Ansi 6 Color", // DARK_CYAN
|
||||
|
@ -31,17 +33,57 @@ namespace ColorTool.SchemeParsers
|
|||
"Ansi 9 Color", // BRIGHT_RED
|
||||
"Ansi 13 Color", // BRIGHT_MAGENTA
|
||||
"Ansi 11 Color", // BRIGHT_YELLOW
|
||||
"Ansi 15 Color" // BRIGHT_WHITE
|
||||
"Ansi 15 Color" // BRIGHT_WHITE
|
||||
};
|
||||
static string FG_KEY = "Foreground Color";
|
||||
static string BG_KEY = "Background Color";
|
||||
static string RED_KEY = "Red Component";
|
||||
static string GREEN_KEY = "Green Component";
|
||||
static string BLUE_KEY = "Blue Component";
|
||||
private const string FG_KEY = "Foreground Color";
|
||||
private const string BG_KEY = "Background Color";
|
||||
private const string RED_KEY = "Red Component";
|
||||
private const string GREEN_KEY = "Green Component";
|
||||
private const string BLUE_KEY = "Blue Component";
|
||||
|
||||
public string Name => "iTerm Parser";
|
||||
public string Name { get; } = "iTerm Parser";
|
||||
|
||||
static bool parseRgbFromXml(XmlNode components, ref uint rgb)
|
||||
public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
|
||||
{
|
||||
XmlDocument xmlDoc = LoadXmlScheme(schemeName); // Create an XML document object
|
||||
if (xmlDoc == null) return null;
|
||||
XmlNode root = xmlDoc.GetElementsByTagName("dict")[0];
|
||||
XmlNodeList children = root.ChildNodes;
|
||||
|
||||
uint[] colorTable = new uint[COLOR_TABLE_SIZE];
|
||||
uint? fgColor = null, bgColor = null;
|
||||
int colorsFound = 0;
|
||||
bool success = false;
|
||||
foreach (var tableEntry in children.OfType<XmlNode>().Where(_ => _.Name == "key"))
|
||||
{
|
||||
uint rgb = 0;
|
||||
int index = -1;
|
||||
XmlNode components = tableEntry.NextSibling;
|
||||
success = ParseRgbFromXml(components, ref rgb);
|
||||
if (!success) { break; }
|
||||
else if (tableEntry.InnerText == FG_KEY) { fgColor = rgb; }
|
||||
else if (tableEntry.InnerText == BG_KEY) { bgColor = rgb; }
|
||||
else if (-1 != (index = Array.IndexOf(PLIST_COLOR_NAMES, tableEntry.InnerText)))
|
||||
{ colorTable[index] = rgb; colorsFound++; }
|
||||
}
|
||||
if (colorsFound < COLOR_TABLE_SIZE)
|
||||
{
|
||||
if (reportErrors)
|
||||
{
|
||||
Console.WriteLine(Resources.InvalidNumberOfColors);
|
||||
}
|
||||
success = false;
|
||||
}
|
||||
if (!success)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var consoleAttributes = new ConsoleAttributes(bgColor, fgColor, null, null);
|
||||
return new ColorScheme(colorTable, consoleAttributes);
|
||||
}
|
||||
|
||||
private static bool ParseRgbFromXml(XmlNode components, ref uint rgb)
|
||||
{
|
||||
int r = -1;
|
||||
int g = -1;
|
||||
|
@ -78,8 +120,7 @@ namespace ColorTool.SchemeParsers
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
static XmlDocument loadXmlScheme(string schemeName)
|
||||
private static XmlDocument LoadXmlScheme(string schemeName)
|
||||
{
|
||||
XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object
|
||||
foreach (string path in SchemeManager.GetSearchPaths(schemeName, ".itermcolors")
|
||||
|
@ -97,46 +138,5 @@ namespace ColorTool.SchemeParsers
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
|
||||
{
|
||||
XmlDocument xmlDoc = loadXmlScheme(schemeName); // Create an XML document object
|
||||
if (xmlDoc == null) return null;
|
||||
XmlNode root = xmlDoc.GetElementsByTagName("dict")[0];
|
||||
XmlNodeList children = root.ChildNodes;
|
||||
|
||||
uint[] colorTable = new uint[COLOR_TABLE_SIZE];
|
||||
uint? fgColor = null, bgColor = null;
|
||||
int colorsFound = 0;
|
||||
bool success = false;
|
||||
foreach (var tableEntry in children.OfType<XmlNode>().Where(_ => _.Name == "key"))
|
||||
{
|
||||
uint rgb = 0;
|
||||
int index = -1;
|
||||
XmlNode components = tableEntry.NextSibling;
|
||||
success = parseRgbFromXml(components, ref rgb);
|
||||
if (!success) { break; }
|
||||
else if (tableEntry.InnerText == FG_KEY) { fgColor = rgb; }
|
||||
else if (tableEntry.InnerText == BG_KEY) { bgColor = rgb; }
|
||||
else if (-1 != (index = Array.IndexOf(PLIST_COLOR_NAMES, tableEntry.InnerText)))
|
||||
{ colorTable[index] = rgb; colorsFound++; }
|
||||
}
|
||||
if (colorsFound < COLOR_TABLE_SIZE)
|
||||
{
|
||||
if (reportErrors)
|
||||
{
|
||||
Console.WriteLine(Resources.InvalidNumberOfColors);
|
||||
}
|
||||
success = false;
|
||||
}
|
||||
if (!success)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var consoleAttributes = new ConsoleAttributes { foreground = fgColor, background = bgColor };
|
||||
return new ColorScheme { colorTable = colorTable, consoleAttributes = consoleAttributes };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue