1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-30 07:29:54 +00:00

fixed hex color parsing

This commit is contained in:
Zeno Rogue 2024-05-27 12:29:37 +02:00
parent 83575d9d7d
commit f823a53f84
3 changed files with 12 additions and 3 deletions

View File

@ -356,7 +356,7 @@ struct color_parameter : public val_parameter<color_t> {
cld get_cld() override { return has_alpha ? *value : (*value * 256 + 0xFF); } cld get_cld() override { return has_alpha ? *value : (*value * 256 + 0xFF); }
void load_from_raw(const string& s) override { *value = parsecolor(s, has_alpha); } void load_from_raw(const string& s) override { *value = parsecolor(s, has_alpha); }
string save() override { return itsh(*value); } string save() override { return has_alpha ? itsh8(*value) : itsh6(*value); }
shared_ptr<parameter> clone(struct local_parameter_set& lps, void *value) override; shared_ptr<parameter> clone(struct local_parameter_set& lps, void *value) override;
}; };

View File

@ -36,6 +36,7 @@ EX string s0;
EX string its(int i) { return hr::format("%d", i); } EX string its(int i) { return hr::format("%d", i); }
EX string itsh8(int i) { return hr::format("%08X", i); } EX string itsh8(int i) { return hr::format("%08X", i); }
EX string itsh6(int i) { return hr::format("%06X", i); }
EX string fts(ld x, int prec IS(6)) { EX string fts(ld x, int prec IS(6)) {
std::stringstream ss; std::stringstream ss;

View File

@ -578,8 +578,16 @@ color_t exp_parser::parsecolor(int prio) {
if(token == "black") return 0x000000FF; if(token == "black") return 0x000000FF;
if(token == "white") return 0xFFFFFFFF; if(token == "white") return 0xFFFFFFFF;
color_t res; color_t res;
int qty = sscanf(s.c_str(), "%x", &res); if(s.size() == 6) {
if(qty == 0) throw hr_parse_exception("color parse error"); int qty = sscanf(s.c_str(), "%x", &res);
if(qty == 0) throw hr_parse_exception("color parse error");
return res * 256 + 0xFF;
}
else if(s.size() == 8) {
int qty = sscanf(s.c_str(), "%x", &res);
if(qty == 0) throw hr_parse_exception("color parse error");
return res;
}
return res; return res;
} }