Skip to content

Commit

Permalink
replace mutable public fields with properties
Browse files Browse the repository at this point in the history
The properties are made readonly where possible, which is possible in almost all cases.
  • Loading branch information
waf committed Apr 23, 2019
1 parent 7daea0a commit 05f518d
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 269 deletions.
27 changes: 18 additions & 9 deletions tools/ColorTool/ColorTool/ColorScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@

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 uint[] ColorTable { get; }
public ConsoleAttributes ConsoleAttributes { get; }

public Color this[int index] => UIntToColor(colorTable[index]);
public Color this[int index] => UIntToColor(ColorTable[index]);

private static Color UIntToColor(uint color)
{
Expand All @@ -25,7 +34,7 @@ private static Color UIntToColor(uint color)
}

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;

Expand Down Expand Up @@ -87,17 +96,17 @@ internal void Dump()

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);
}
}
}
Expand Down
207 changes: 91 additions & 116 deletions tools/ColorTool/ColorTool/ColorTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,103 +4,112 @@
//

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()
{
ConsoleColor[] colors = (ConsoleColor[])ConsoleColor.GetValues(typeof(ConsoleColor));
// 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(" ");
Expand All @@ -109,7 +118,7 @@ public static void PrintTable()
}
Console.WriteLine();

for (int fg = 0; fg < FGs.Length; fg++)
for (int fg = 0; fg < FGs.Count; fg++)
{
Console.ForegroundColor = currentForeground;
Console.BackgroundColor = currentBackground;
Expand All @@ -119,13 +128,13 @@ public static void PrintTable()
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");
Expand All @@ -140,42 +149,8 @@ public static void PrintTable()

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(" ");
Expand All @@ -184,7 +159,7 @@ public static void PrintTableWithVt()
}
Console.WriteLine();

for (int fg = 0; fg < FGs.Length; fg++)
for (int fg = 0; fg < FGs.Count; fg++)
{
Console.Write("\x1b[m");

Expand All @@ -202,7 +177,7 @@ public static void PrintTableWithVt()
Console.Write("\x1b[" + FGs[fg]);
}

for (int bg = 0; bg < BGs.Length; bg++)
for (int bg = 0; bg < BGs.Count; bg++)
{
if (bg > 0)
{
Expand All @@ -217,7 +192,7 @@ public static void PrintTableWithVt()
Console.Write("\x1b[" + BGs[bg]);
}

Console.Write(test);
Console.Write(TestText);
Console.Write("\x1b[49m");
}
Console.Write("\n");
Expand Down
2 changes: 1 addition & 1 deletion tools/ColorTool/ColorTool/ColorTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net46</TargetFramework>
<TargetFramework>net461</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
</Project>
17 changes: 12 additions & 5 deletions tools/ColorTool/ColorTool/ConsoleAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Loading

0 comments on commit 05f518d

Please sign in to comment.