Skip to content

Commit 05f518d

Browse files
committed
replace mutable public fields with properties
The properties are made readonly where possible, which is possible in almost all cases.
1 parent 7daea0a commit 05f518d

File tree

10 files changed

+264
-269
lines changed

10 files changed

+264
-269
lines changed

tools/ColorTool/ColorTool/ColorScheme.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@
99

1010
namespace ColorTool
1111
{
12+
/// <summary>
13+
/// Represents a colorscheme that can be applied to a console.
14+
/// </summary>
1215
public class ColorScheme
1316
{
14-
public uint[] colorTable = null;
15-
public ConsoleAttributes consoleAttributes;
17+
public ColorScheme(uint[] colorTable, ConsoleAttributes consoleAttributes)
18+
{
19+
ColorTable = colorTable;
20+
ConsoleAttributes = consoleAttributes;
21+
}
22+
23+
public uint[] ColorTable { get; }
24+
public ConsoleAttributes ConsoleAttributes { get; }
1625

17-
public Color this[int index] => UIntToColor(colorTable[index]);
26+
public Color this[int index] => UIntToColor(ColorTable[index]);
1827

1928
private static Color UIntToColor(uint color)
2029
{
@@ -25,7 +34,7 @@ private static Color UIntToColor(uint color)
2534
}
2635

2736
public int CalculateIndex(uint value) =>
28-
colorTable.Select((color, idx) => Tuple.Create(color, idx))
37+
ColorTable.Select((color, idx) => Tuple.Create(color, idx))
2938
.OrderBy(Difference(value))
3039
.First().Item2;
3140

@@ -87,17 +96,17 @@ internal void Dump()
8796

8897
for (int i = 0; i < 16; ++i)
8998
{
90-
_dump($"Color[{i}]", colorTable[i]);
99+
_dump($"Color[{i}]", ColorTable[i]);
91100
}
92101

93-
if (consoleAttributes.foreground != null)
102+
if (ConsoleAttributes.Foreground != null)
94103
{
95-
_dump("FG ", consoleAttributes.foreground.Value);
104+
_dump("FG ", ConsoleAttributes.Foreground.Value);
96105
}
97106

98-
if (consoleAttributes.background != null)
107+
if (ConsoleAttributes.Background != null)
99108
{
100-
_dump("BG ", consoleAttributes.background.Value);
109+
_dump("BG ", ConsoleAttributes.Background.Value);
101110
}
102111
}
103112
}

tools/ColorTool/ColorTool/ColorTable.cs

Lines changed: 91 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,103 +4,112 @@
44
//
55

66
using System;
7+
using System.Collections.Generic;
78

89
namespace ColorTool
910
{
11+
/// <summary>
12+
/// Displays the color table that demonstrates the current colorscheme.
13+
/// </summary>
1014
class ColorTable
1115
{
12-
static int DARK_BLACK = 0;
13-
static int DARK_BLUE = 1;
14-
static int DARK_GREEN = 2;
15-
static int DARK_CYAN = 3;
16-
static int DARK_RED = 4;
17-
static int DARK_MAGENTA = 5;
18-
static int DARK_YELLOW = 6;
19-
static int DARK_WHITE = 7;
20-
static int BRIGHT_BLACK = 8;
21-
static int BRIGHT_BLUE = 9;
22-
static int BRIGHT_GREEN = 10;
23-
static int BRIGHT_CYAN = 11;
24-
static int BRIGHT_RED = 12;
25-
static int BRIGHT_MAGENTA = 13;
26-
static int BRIGHT_YELLOW = 14;
27-
static int BRIGHT_WHITE = 15;
16+
const int DARK_BLACK = 0;
17+
const int DARK_BLUE = 1;
18+
const int DARK_GREEN = 2;
19+
const int DARK_CYAN = 3;
20+
const int DARK_RED = 4;
21+
const int DARK_MAGENTA = 5;
22+
const int DARK_YELLOW = 6;
23+
const int DARK_WHITE = 7;
24+
const int BRIGHT_BLACK = 8;
25+
const int BRIGHT_BLUE = 9;
26+
const int BRIGHT_GREEN = 10;
27+
const int BRIGHT_CYAN = 11;
28+
const int BRIGHT_RED = 12;
29+
const int BRIGHT_MAGENTA = 13;
30+
const int BRIGHT_YELLOW = 14;
31+
const int BRIGHT_WHITE = 15;
2832

2933
// This is the order of colors when output by the table.
30-
static int[] outputFgs = {
31-
BRIGHT_WHITE ,
32-
DARK_BLACK ,
33-
BRIGHT_BLACK ,
34-
DARK_RED ,
35-
BRIGHT_RED ,
36-
DARK_GREEN ,
37-
BRIGHT_GREEN ,
38-
DARK_YELLOW ,
39-
BRIGHT_YELLOW ,
40-
DARK_BLUE ,
41-
BRIGHT_BLUE ,
42-
DARK_MAGENTA ,
43-
BRIGHT_MAGENTA ,
44-
DARK_CYAN ,
45-
BRIGHT_CYAN ,
46-
DARK_WHITE ,
34+
private static readonly IReadOnlyList<int> outputFgs = new[]
35+
{
36+
BRIGHT_WHITE,
37+
DARK_BLACK,
38+
BRIGHT_BLACK,
39+
DARK_RED,
40+
BRIGHT_RED,
41+
DARK_GREEN,
42+
BRIGHT_GREEN,
43+
DARK_YELLOW,
44+
BRIGHT_YELLOW,
45+
DARK_BLUE,
46+
BRIGHT_BLUE,
47+
DARK_MAGENTA,
48+
BRIGHT_MAGENTA,
49+
DARK_CYAN,
50+
BRIGHT_CYAN,
51+
DARK_WHITE,
4752
BRIGHT_WHITE
4853
};
4954

55+
private const string TestText = " gYw ";
5056

51-
static int[] saneBgs = {
52-
DARK_BLACK ,
53-
DARK_RED ,
54-
DARK_GREEN ,
55-
DARK_YELLOW ,
56-
DARK_BLUE ,
57-
DARK_MAGENTA ,
58-
DARK_CYAN ,
59-
DARK_WHITE
57+
private static readonly IReadOnlyList<string> FGs = new[]
58+
{
59+
"m",
60+
"1m",
61+
"30m",
62+
"1;30m",
63+
"31m",
64+
"1;31m",
65+
"32m",
66+
"1;32m",
67+
"33m",
68+
"1;33m",
69+
"34m",
70+
"1;34m",
71+
"35m",
72+
"1;35m",
73+
"36m",
74+
"1;36m",
75+
"37m",
76+
"1;37m"
6077
};
6178

79+
private static readonly IReadOnlyList<string> BGs = new[]
80+
{
81+
"m",
82+
"40m",
83+
"41m",
84+
"42m",
85+
"43m",
86+
"44m",
87+
"45m",
88+
"46m",
89+
"47m"
90+
};
91+
92+
private static readonly IReadOnlyList<int> saneBgs = new[]
93+
{
94+
DARK_BLACK,
95+
DARK_RED,
96+
DARK_GREEN,
97+
DARK_YELLOW,
98+
DARK_BLUE,
99+
DARK_MAGENTA,
100+
DARK_CYAN,
101+
DARK_WHITE
102+
};
62103

63104
public static void PrintTable()
64105
{
65106
ConsoleColor[] colors = (ConsoleColor[])ConsoleColor.GetValues(typeof(ConsoleColor));
66107
// Save the current background and foreground colors.
67108
ConsoleColor currentBackground = Console.BackgroundColor;
68109
ConsoleColor currentForeground = Console.ForegroundColor;
69-
string test = " gYw ";
70-
string[] FGs = {
71-
"m",
72-
"1m",
73-
"30m",
74-
"1;30m",
75-
"31m",
76-
"1;31m",
77-
"32m",
78-
"1;32m",
79-
"33m",
80-
"1;33m",
81-
"34m",
82-
"1;34m",
83-
"35m",
84-
"1;35m",
85-
"36m",
86-
"1;36m",
87-
"37m",
88-
"1;37m"
89-
};
90-
string[] BGs = {
91-
"m",
92-
"40m",
93-
"41m",
94-
"42m",
95-
"43m",
96-
"44m",
97-
"45m",
98-
"46m",
99-
"47m"
100-
};
101110

102111
Console.Write("\t");
103-
for (int bg = 0; bg < BGs.Length; bg++)
112+
for (int bg = 0; bg < BGs.Count; bg++)
104113
{
105114
if (bg > 0) Console.Write(" ");
106115
Console.Write(" ");
@@ -109,7 +118,7 @@ public static void PrintTable()
109118
}
110119
Console.WriteLine();
111120

112-
for (int fg = 0; fg < FGs.Length; fg++)
121+
for (int fg = 0; fg < FGs.Count; fg++)
113122
{
114123
Console.ForegroundColor = currentForeground;
115124
Console.BackgroundColor = currentBackground;
@@ -119,13 +128,13 @@ public static void PrintTable()
119128
if (fg == 0) Console.ForegroundColor = currentForeground;
120129
else Console.ForegroundColor = colors[outputFgs[fg - 1]];
121130

122-
for (int bg = 0; bg < BGs.Length; bg++)
131+
for (int bg = 0; bg < BGs.Count; bg++)
123132
{
124133
if (bg > 0) Console.Write(" ");
125134
if (bg == 0)
126135
Console.BackgroundColor = currentBackground;
127136
else Console.BackgroundColor = colors[saneBgs[bg - 1]];
128-
Console.Write(test);
137+
Console.Write(TestText);
129138
Console.BackgroundColor = currentBackground;
130139
}
131140
Console.Write("\n");
@@ -140,42 +149,8 @@ public static void PrintTable()
140149

141150
public static void PrintTableWithVt()
142151
{
143-
// Save the current background and foreground colors.
144-
string test = " gYw ";
145-
string[] FGs = {
146-
"m",
147-
"1m",
148-
"30m",
149-
"1;30m",
150-
"31m",
151-
"1;31m",
152-
"32m",
153-
"1;32m",
154-
"33m",
155-
"1;33m",
156-
"34m",
157-
"1;34m",
158-
"35m",
159-
"1;35m",
160-
"36m",
161-
"1;36m",
162-
"37m",
163-
"1;37m"
164-
};
165-
string[] BGs = {
166-
"m",
167-
"40m",
168-
"41m",
169-
"42m",
170-
"43m",
171-
"44m",
172-
"45m",
173-
"46m",
174-
"47m"
175-
};
176-
177152
Console.Write("\t");
178-
for (int bg = 0; bg < BGs.Length; bg++)
153+
for (int bg = 0; bg < BGs.Count; bg++)
179154
{
180155
if (bg > 0) Console.Write(" ");
181156
Console.Write(" ");
@@ -184,7 +159,7 @@ public static void PrintTableWithVt()
184159
}
185160
Console.WriteLine();
186161

187-
for (int fg = 0; fg < FGs.Length; fg++)
162+
for (int fg = 0; fg < FGs.Count; fg++)
188163
{
189164
Console.Write("\x1b[m");
190165

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

205-
for (int bg = 0; bg < BGs.Length; bg++)
180+
for (int bg = 0; bg < BGs.Count; bg++)
206181
{
207182
if (bg > 0)
208183
{
@@ -217,7 +192,7 @@ public static void PrintTableWithVt()
217192
Console.Write("\x1b[" + BGs[bg]);
218193
}
219194

220-
Console.Write(test);
195+
Console.Write(TestText);
221196
Console.Write("\x1b[49m");
222197
}
223198
Console.Write("\n");

tools/ColorTool/ColorTool/ColorTool.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net46</TargetFramework>
5+
<TargetFramework>net461</TargetFramework>
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
77
</PropertyGroup>
88
</Project>

tools/ColorTool/ColorTool/ConsoleAttributes.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55

66
namespace ColorTool
77
{
8-
public struct ConsoleAttributes
8+
public readonly struct ConsoleAttributes
99
{
10-
public uint? foreground;
11-
public uint? background;
10+
public ConsoleAttributes(uint? background, uint? foreground, uint? popupBackground, uint? popupForeground)
11+
{
12+
Background = background;
13+
Foreground = foreground;
14+
PopupBackground = popupBackground;
15+
PopupForeground = popupForeground;
16+
}
1217

13-
public uint? popupForeground;
14-
public uint? popupBackground;
18+
public uint? Foreground { get; }
19+
public uint? Background { get; }
20+
public uint? PopupForeground { get; }
21+
public uint? PopupBackground { get; }
1522
}
1623
}

0 commit comments

Comments
 (0)