chore: parse hex colors in properties

This commit is contained in:
Almamu 2025-08-20 01:41:01 +02:00
parent cf3774c481
commit fae2b5e01e
3 changed files with 45 additions and 3 deletions

View File

@ -14,6 +14,30 @@ glm::vec3 ParseColor (std::string value) {
} }
if (value.find ('.') == std::string::npos && value != "0 0 0" && value != "1 1 1") { if (value.find ('.') == std::string::npos && value != "0 0 0" && value != "1 1 1") {
if (value.find ('#') == 0) {
// hex color, parse it to int color and then convert it to float
auto number = value.substr (1);
if (number.size () == 3) {
// CSS short-number, expand to the proper size
number = number[0] + number[0] + number[1] + number[1] + number[2] + number[2];
}
// remove alpha if it's present, should look into it more closely
if (number.size () > 6) {
sLog.error ("Color value has alpha channel, which is not supported");
number = number.substr (0, 6);
}
const auto color = std::stoi (number, nullptr, 16);
return {
(((color >> 16) & 0xFF) / 255.0),
(((color >> 8) & 0xFF) / 255.0),
(((color >> 0) & 0xFF) / 255.0)
};
}
const auto intcolor = VectorBuilder::parse <glm::ivec3> (value); const auto intcolor = VectorBuilder::parse <glm::ivec3> (value);
return {intcolor.r / 255.0, intcolor.g / 255.0, intcolor.b / 255.0}; return {intcolor.r / 255.0, intcolor.g / 255.0, intcolor.b / 255.0};

View File

@ -19,17 +19,23 @@ std::shared_ptr<CPropertyCombo> CPropertyCombo::fromJSON (const JSON& data, std:
if (!cur.is_object ()) if (!cur.is_object ())
continue; continue;
const auto valueIt = cur.require ("value", "Value is required for a property combo option");
auto value = valueIt.is_number () ? std::to_string (valueIt.get <int> ()) : valueIt.get <std::string> ();
// check for label and value to ensure they're there // check for label and value to ensure they're there
values.push_back ({ values.push_back ({
.label = cur.require ("label", "Label is required for a property combo option"), .label = cur.require ("label", "Label is required for a property combo option"),
.value = cur.require ("value", "Value is required for a property combo option") .value = value
}); });
} }
const auto valueIt = data.require ("value", "Value is required for a property combo");
auto value = valueIt.is_number () ? std::to_string (valueIt.get <int> ()) : valueIt.get <std::string> ();
return std::make_shared <CPropertyCombo> ( return std::make_shared <CPropertyCombo> (
std::move(name), std::move(name),
data.optional <std::string> ("text", ""), data.optional <std::string> ("text", ""),
data.require ("value", "Value is required for a property combo"), value,
values values
); );
} }

View File

@ -14,15 +14,27 @@ static int backgroundId = 0;
ProjectUniquePtr ProjectParser::parse (const JSON& data, ContainerUniquePtr container) { ProjectUniquePtr ProjectParser::parse (const JSON& data, ContainerUniquePtr container) {
const auto general = data.optional ("general"); const auto general = data.optional ("general");
const auto workshopId = data.optional ("workshopid");
auto actualWorkshopId = std::to_string (--backgroundId);
auto type = data.require <std::string> ("type", "Project type missing"); auto type = data.require <std::string> ("type", "Project type missing");
if (workshopId.has_value ()) {
if (workshopId->is_number ()) {
actualWorkshopId = std::to_string (workshopId->get <int> ());
} else if (workshopId->is_string ()) {
actualWorkshopId = workshopId->get <std::string> ();
} else {
sLog.error ("Invalid workshop id: ", workshopId->dump ());
}
}
// lowercase for consistency // lowercase for consistency
std::transform (type.begin (), type.end (), type.begin (), tolower); std::transform (type.begin (), type.end (), type.begin (), tolower);
auto result = std::make_unique <Project> (Project { auto result = std::make_unique <Project> (Project {
.title = data.require <std::string> ("title", "Project title missing"), .title = data.require <std::string> ("title", "Project title missing"),
.type = parseType (type), .type = parseType (type),
.workshopId = data.optional ("workshopid", std::to_string (--backgroundId)), .workshopId = actualWorkshopId,
.supportsAudioProcessing = general.has_value () && general.value ().optional ("supportsaudioprocessing", false), .supportsAudioProcessing = general.has_value () && general.value ().optional ("supportsaudioprocessing", false),
.properties = parseProperties (general), .properties = parseProperties (general),
.container = std::move(container), .container = std::move(container),