@@ -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
9292COLORREF 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