Skip to content

Commit 51f5353

Browse files
kojoruDHowett
authored andcommitted
Add support for short hex color codes like #CCC (#2658)
This adds a few lines to support shorthand color hex codes like #ABC. They are treated as equivalent of #AABBCC. Fixes #2639.
1 parent 21067a7 commit 51f5353

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/cascadia/ut_app/JsonTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace TerminalAppUnitTests
7979
"\"name\" : \"Campbell\","
8080
"\"purple\" : \"#881798\","
8181
"\"red\" : \"#C50F1F\","
82-
"\"white\" : \"#CCCCCC\","
82+
"\"white\" : \"#CCC\","
8383
"\"yellow\" : \"#C19C00\""
8484
"}" };
8585

src/types/utils.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,33 @@ std::string Utils::ColorToHexString(const COLORREF color)
8383
}
8484

8585
// Function Description:
86-
// - Parses a color from a string. The string should be in the format "#RRGGBB"
86+
// - Parses a color from a string. The string should be in the format "#RRGGBB" or "#RGB"
8787
// Arguments:
8888
// - str: a string representation of the COLORREF to parse
8989
// Return Value:
9090
// - A COLORREF if the string could successfully be parsed. If the string is not
9191
// the correct format, throws E_INVALIDARG
9292
COLORREF Utils::ColorFromHexString(const std::string str)
9393
{
94-
THROW_HR_IF(E_INVALIDARG, str.size() < 7 || str.size() >= 8);
94+
THROW_HR_IF(E_INVALIDARG, str.size() != 7 && str.size() != 4);
9595
THROW_HR_IF(E_INVALIDARG, str[0] != '#');
9696

97-
std::string rStr{ &str[1], 2 };
98-
std::string gStr{ &str[3], 2 };
99-
std::string bStr{ &str[5], 2 };
97+
std::string rStr;
98+
std::string gStr;
99+
std::string bStr;
100+
101+
if (str.size() == 4)
102+
{
103+
rStr = std::string(2, str[1]);
104+
gStr = std::string(2, str[2]);
105+
bStr = std::string(2, str[3]);
106+
}
107+
else
108+
{
109+
rStr = std::string(&str[1], 2);
110+
gStr = std::string(&str[3], 2);
111+
bStr = std::string(&str[5], 2);
112+
}
100113

101114
BYTE r = static_cast<BYTE>(std::stoul(rStr, nullptr, 16));
102115
BYTE g = static_cast<BYTE>(std::stoul(gStr, nullptr, 16));

0 commit comments

Comments
 (0)