mirror of
https://github.com/Almamu/linux-wallpaperengine.git
synced 2025-09-14 05:46:48 +08:00
chore: parse hex colors in properties
This commit is contained in:
parent
cf3774c481
commit
fae2b5e01e
@ -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 ('#') == 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);
|
||||
|
||||
return {intcolor.r / 255.0, intcolor.g / 255.0, intcolor.b / 255.0};
|
||||
|
@ -19,17 +19,23 @@ std::shared_ptr<CPropertyCombo> CPropertyCombo::fromJSON (const JSON& data, std:
|
||||
if (!cur.is_object ())
|
||||
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
|
||||
values.push_back ({
|
||||
.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> (
|
||||
std::move(name),
|
||||
data.optional <std::string> ("text", ""),
|
||||
data.require ("value", "Value is required for a property combo"),
|
||||
value,
|
||||
values
|
||||
);
|
||||
}
|
||||
|
@ -14,15 +14,27 @@ static int backgroundId = 0;
|
||||
|
||||
ProjectUniquePtr ProjectParser::parse (const JSON& data, ContainerUniquePtr container) {
|
||||
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");
|
||||
|
||||
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
|
||||
std::transform (type.begin (), type.end (), type.begin (), tolower);
|
||||
|
||||
auto result = std::make_unique <Project> (Project {
|
||||
.title = data.require <std::string> ("title", "Project title missing"),
|
||||
.type = parseType (type),
|
||||
.workshopId = data.optional ("workshopid", std::to_string (--backgroundId)),
|
||||
.workshopId = actualWorkshopId,
|
||||
.supportsAudioProcessing = general.has_value () && general.value ().optional ("supportsaudioprocessing", false),
|
||||
.properties = parseProperties (general),
|
||||
.container = std::move(container),
|
||||
|
Loading…
Reference in New Issue
Block a user