From f823a53f841ccd2033c24eca824e06c8fe9d6e6b Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Mon, 27 May 2024 12:29:37 +0200 Subject: [PATCH] fixed hex color parsing --- config.cpp | 2 +- hprint.cpp | 1 + util.cpp | 12 ++++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/config.cpp b/config.cpp index 02c65c4e..77e59f1a 100644 --- a/config.cpp +++ b/config.cpp @@ -356,7 +356,7 @@ struct color_parameter : public val_parameter { cld get_cld() override { return has_alpha ? *value : (*value * 256 + 0xFF); } 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 clone(struct local_parameter_set& lps, void *value) override; }; diff --git a/hprint.cpp b/hprint.cpp index b56eb754..d6fb75c9 100644 --- a/hprint.cpp +++ b/hprint.cpp @@ -36,6 +36,7 @@ EX string s0; EX string its(int i) { return hr::format("%d", 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)) { std::stringstream ss; diff --git a/util.cpp b/util.cpp index 08856bed..3b1a5de8 100644 --- a/util.cpp +++ b/util.cpp @@ -578,8 +578,16 @@ color_t exp_parser::parsecolor(int prio) { if(token == "black") return 0x000000FF; if(token == "white") return 0xFFFFFFFF; color_t res; - int qty = sscanf(s.c_str(), "%x", &res); - if(qty == 0) throw hr_parse_exception("color parse error"); + if(s.size() == 6) { + 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; }