Skip to content

Commit c97cccb

Browse files
zadjii-msftmsftbot[bot]
authored andcommitted
Initializes conhost's Campbell color scheme in conhost order instead of ANSI/VT order (microsoft#1237)
* Fix this * Swap the elements instead of having two whole tables * Add a unittest to make @miniksa happy
1 parent 63347f4 commit c97cccb

4 files changed

Lines changed: 92 additions & 27 deletions

File tree

src/host/settings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Settings::Settings() :
8484
gsl::span<COLORREF> tableView = { _ColorTable, gsl::narrow<ptrdiff_t>(COLOR_TABLE_SIZE) };
8585
gsl::span<COLORREF> xtermTableView = { _XtermColorTable, gsl::narrow<ptrdiff_t>(XTERM_COLOR_TABLE_SIZE) };
8686
::Microsoft::Console::Utils::Initialize256ColorTable(xtermTableView);
87-
::Microsoft::Console::Utils::InitializeCampbellColorTable(tableView);
87+
::Microsoft::Console::Utils::InitializeCampbellColorTableForConhost(tableView);
8888
}
8989

9090
// Routine Description:
@@ -124,7 +124,7 @@ void Settings::ApplyDesktopSpecificDefaults()
124124
_bHistoryNoDup = FALSE;
125125

126126
gsl::span<COLORREF> tableView = { _ColorTable, gsl::narrow<ptrdiff_t>(COLOR_TABLE_SIZE) };
127-
::Microsoft::Console::Utils::InitializeCampbellColorTable(tableView);
127+
::Microsoft::Console::Utils::InitializeCampbellColorTableForConhost(tableView);
128128

129129
_fTrimLeadingZeros = false;
130130
_fEnableColorSelection = false;

src/types/inc/utils.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace Microsoft::Console::Utils
2525
COLORREF ColorFromHexString(const std::string wstr);
2626

2727
void InitializeCampbellColorTable(gsl::span<COLORREF>& table);
28+
void InitializeCampbellColorTableForConhost(gsl::span<COLORREF>& table);
29+
void SwapANSIColorOrderForConhost(gsl::span<COLORREF>& table);
2830
void Initialize256ColorTable(gsl::span<COLORREF>& table);
2931
void SetColorTableAlpha(gsl::span<COLORREF>& table, const BYTE newAlpha);
3032

src/types/ut_types/UtilsTests.cpp

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "..\..\inc\consoletaeftemplates.hpp"
77

88
#include "..\inc\utils.hpp"
9+
#include <conattrs.hpp>
910

1011
using namespace WEX::Common;
1112
using namespace WEX::Logging;
@@ -17,28 +18,60 @@ class UtilsTests
1718
{
1819
TEST_CLASS(UtilsTests);
1920

20-
TEST_METHOD(TestClampToShortMax)
21-
{
22-
const short min = 1;
23-
24-
// Test outside the lower end of the range
25-
const short minExpected = min;
26-
auto minActual = ClampToShortMax(0, min);
27-
VERIFY_ARE_EQUAL(minExpected, minActual);
28-
29-
// Test negative numbers
30-
const short negativeExpected = min;
31-
auto negativeActual = ClampToShortMax(-1, min);
32-
VERIFY_ARE_EQUAL(negativeExpected, negativeActual);
33-
34-
// Test outside the upper end of the range
35-
const short maxExpected = SHRT_MAX;
36-
auto maxActual = ClampToShortMax(50000, min);
37-
VERIFY_ARE_EQUAL(maxExpected, maxActual);
38-
39-
// Test within the range
40-
const short withinRangeExpected = 100;
41-
auto withinRangeActual = ClampToShortMax(withinRangeExpected, min);
42-
VERIFY_ARE_EQUAL(withinRangeExpected, withinRangeActual);
43-
}
21+
TEST_METHOD(TestClampToShortMax);
22+
TEST_METHOD(TestSwapColorPalette);
4423
};
24+
25+
void UtilsTests::TestClampToShortMax()
26+
{
27+
const short min = 1;
28+
29+
// Test outside the lower end of the range
30+
const short minExpected = min;
31+
auto minActual = ClampToShortMax(0, min);
32+
VERIFY_ARE_EQUAL(minExpected, minActual);
33+
34+
// Test negative numbers
35+
const short negativeExpected = min;
36+
auto negativeActual = ClampToShortMax(-1, min);
37+
VERIFY_ARE_EQUAL(negativeExpected, negativeActual);
38+
39+
// Test outside the upper end of the range
40+
const short maxExpected = SHRT_MAX;
41+
auto maxActual = ClampToShortMax(50000, min);
42+
VERIFY_ARE_EQUAL(maxExpected, maxActual);
43+
44+
// Test within the range
45+
const short withinRangeExpected = 100;
46+
auto withinRangeActual = ClampToShortMax(withinRangeExpected, min);
47+
VERIFY_ARE_EQUAL(withinRangeExpected, withinRangeActual);
48+
}
49+
void UtilsTests::TestSwapColorPalette()
50+
{
51+
std::array<COLORREF, COLOR_TABLE_SIZE> terminalTable;
52+
std::array<COLORREF, COLOR_TABLE_SIZE> consoleTable;
53+
54+
gsl::span<COLORREF> terminalTableView = { &terminalTable[0], gsl::narrow<ptrdiff_t>(terminalTable.size()) };
55+
gsl::span<COLORREF> consoleTableleView = { &consoleTable[0], gsl::narrow<ptrdiff_t>(consoleTable.size()) };
56+
57+
// First set up the colors
58+
InitializeCampbellColorTable(terminalTableView);
59+
InitializeCampbellColorTableForConhost(consoleTableleView);
60+
61+
VERIFY_ARE_EQUAL(terminalTable[0], consoleTable[0]);
62+
VERIFY_ARE_EQUAL(terminalTable[1], consoleTable[4]);
63+
VERIFY_ARE_EQUAL(terminalTable[2], consoleTable[2]);
64+
VERIFY_ARE_EQUAL(terminalTable[3], consoleTable[6]);
65+
VERIFY_ARE_EQUAL(terminalTable[4], consoleTable[1]);
66+
VERIFY_ARE_EQUAL(terminalTable[5], consoleTable[5]);
67+
VERIFY_ARE_EQUAL(terminalTable[6], consoleTable[3]);
68+
VERIFY_ARE_EQUAL(terminalTable[7], consoleTable[7]);
69+
VERIFY_ARE_EQUAL(terminalTable[8], consoleTable[8]);
70+
VERIFY_ARE_EQUAL(terminalTable[9], consoleTable[12]);
71+
VERIFY_ARE_EQUAL(terminalTable[10], consoleTable[10]);
72+
VERIFY_ARE_EQUAL(terminalTable[11], consoleTable[14]);
73+
VERIFY_ARE_EQUAL(terminalTable[12], consoleTable[9]);
74+
VERIFY_ARE_EQUAL(terminalTable[13], consoleTable[13]);
75+
VERIFY_ARE_EQUAL(terminalTable[14], consoleTable[11]);
76+
VERIFY_ARE_EQUAL(terminalTable[15], consoleTable[15]);
77+
}

src/types/utils.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ bool Utils::IsValidHandle(const HANDLE handle) noexcept
117117
}
118118

119119
// Function Description:
120-
// - Fill the first 16 entries of a given color table with the Campbell color scheme
120+
// - Fill the first 16 entries of a given color table with the Campbell color
121+
// scheme, in the ANSI/VT RGB order.
121122
// Arguments:
122123
// - table: a color table with at least 16 entries
123124
// Return Value:
@@ -146,6 +147,35 @@ void Utils::InitializeCampbellColorTable(gsl::span<COLORREF>& table)
146147
// clang-format on
147148
}
148149

150+
// Function Description:
151+
// - Fill the first 16 entries of a given color table with the Campbell color
152+
// scheme, in the Windows BGR order.
153+
// Arguments:
154+
// - table: a color table with at least 16 entries
155+
// Return Value:
156+
// - <none>, throws if the table has less that 16 entries
157+
void Utils::InitializeCampbellColorTableForConhost(gsl::span<COLORREF>& table)
158+
{
159+
THROW_HR_IF(E_INVALIDARG, table.size() < 16);
160+
InitializeCampbellColorTable(table);
161+
SwapANSIColorOrderForConhost(table);
162+
}
163+
164+
// Function Description:
165+
// - modifies in-place the given color table from ANSI (RGB) order to Console order (BRG).
166+
// Arguments:
167+
// - table: a color table with at least 16 entries
168+
// Return Value:
169+
// - <none>, throws if the table has less that 16 entries
170+
void Utils::SwapANSIColorOrderForConhost(gsl::span<COLORREF>& table)
171+
{
172+
THROW_HR_IF(E_INVALIDARG, table.size() < 16);
173+
std::swap(table[1], table[4]);
174+
std::swap(table[3], table[6]);
175+
std::swap(table[9], table[12]);
176+
std::swap(table[11], table[14]);
177+
}
178+
149179
// Function Description:
150180
// - Fill the first 255 entries of a given color table with the default values
151181
// of a full 256-color table

0 commit comments

Comments
 (0)