Phase 4: live-apply typed rgb(...) colours (parse_hex_color)

Verification find: typing rgb(255,0,0) into a StockChart up-color row
stored the value in the model but never redrew -- parse_hex_color()
accepted only #rrggbb, while the XFrame Inspector stores the raw entry
text and the Phase 4 colour defaults/display use the rgb(...) form
(the Pick... chooser stores hex, so chooser edits worked). Extend the
parser to the same set the generator's parse_color() accepts -- named
colours, #rgb/#rrggbb, rgb(r,g,b) (clamped) -- so live-apply and
generation can never disagree on a colour again. Affects every colour
prop edited by typing, not just Stock/Inst: background, foreground,
border-color go through the same parser.
This commit is contained in:
cnmcdee committed 2026-11-30 15:35:34 +00:00
1 parent 9681fbae94
commit 563870f9b7
1 file changed
+62 -10
+62 -10
View File
@@ -21,6 +21,7 @@
#include "xframe_canvas_core.h"
#include <X11/Xutil.h>
#include <tuple>
#include <X11/keysym.h>
#include <time.h>
@@ -115,18 +116,69 @@ XFrameCanvasCore::Placed* find_placed(XFrameCanvasCore* c, int id) {
}
// --- Phase 1 live-apply helpers ----------------------------------------
// Prop accessors (the Inspector stores canonical strings: Bool is
// "true"/"false", Color is "#rrggbb", Int/Double are plain numbers).
// Prop accessors (Bool is "true"/"false", Int/Double are plain numbers).
// Colours arrive as the raw Inspector entry text, so accept everything the
// generator's parse_color() accepts: named colours, "#rgb"/"#rrggbb" and
// "rgb(r,g,b)" (the Phase 4 table defaults/display use the rgb(...) form;
// the Pick... chooser stores hex). Live-apply and generation must never
// disagree on a colour.
bool parse_hex_color(const std::string& s, Srgb& out) {
if (s.size() != 7 || s[0] != '#') return false;
try {
out.r = std::stoi(s.substr(1, 2), nullptr, 16);
out.g = std::stoi(s.substr(3, 2), nullptr, 16);
out.b = std::stoi(s.substr(5, 2), nullptr, 16);
} catch (...) {
return false;
auto clamp255 = [](int v) { return v < 0 ? 0 : (v > 255 ? 255 : v); };
std::string t;
for (char c : s)
if (!std::isspace((unsigned char)c)) t += c;
if (t.empty()) return false;
std::string low = t;
for (char& c : low) c = (char)std::tolower((unsigned char)c);
static const std::map<std::string, std::tuple<int, int, int>> kNamed = {
{"black", {0, 0, 0}}, {"white", {255, 255, 255}},
{"red", {255, 0, 0}}, {"green", {0, 128, 0}},
{"blue", {0, 0, 255}}, {"yellow", {255, 255, 0}},
{"cyan", {0, 255, 255}}, {"magenta", {255, 0, 255}},
{"gray", {128, 128, 128}}, {"grey", {128, 128, 128}},
{"orange", {255, 165, 0}}, {"purple", {128, 0, 128}},
};
auto ni = kNamed.find(low);
if (ni != kNamed.end()) {
std::tie(out.r, out.g, out.b) = ni->second;
return true;
}
return true;
if (t[0] == '#') {
std::string h = t.substr(1);
if (h.size() == 3) h = {h[0], h[0], h[1], h[1], h[2], h[2]};
if (h.size() != 6) return false;
try {
out.r = std::stoi(h.substr(0, 2), nullptr, 16);
out.g = std::stoi(h.substr(2, 2), nullptr, 16);
out.b = std::stoi(h.substr(4, 2), nullptr, 16);
} catch (...) {
return false;
}
return true;
}
if (low.rfind("rgb(", 0) == 0 && !t.empty() && t.back() == ')') {
std::string inner = t.substr(4, t.size() - 5);
std::vector<std::string> parts;
size_t pos = 0;
while (true) {
size_t comma = inner.find(',', pos);
parts.push_back(inner.substr(
pos, comma == std::string::npos ? std::string::npos
: comma - pos));
if (comma == std::string::npos) break;
pos = comma + 1;
}
if (parts.size() < 3) return false;
try {
out.r = clamp255(std::stoi(parts[0]));
out.g = clamp255(std::stoi(parts[1]));
out.b = clamp255(std::stoi(parts[2]));
} catch (...) {
return false;
}
return true;
}
return false;
}
std::string prop_str(const XFrameCanvasCore::Placed* p,