Can now set a color to another already existing color.
This commit is contained in:
parent
f4fa50a413
commit
379060b8c7
5 changed files with 69 additions and 81 deletions
53
palette.cpp
53
palette.cpp
|
@ -10,30 +10,50 @@ namespace palette {
|
|||
struct PaletteMgr {
|
||||
std::unordered_map<string, sf::Color> palettes;
|
||||
std::string config;
|
||||
std::unordered_map<string, string> pending_refs;
|
||||
bool initialized = false;
|
||||
};
|
||||
|
||||
static PaletteMgr COLOR;
|
||||
|
||||
void init(const string &json_file) {
|
||||
COLOR.config = json_file;
|
||||
Config config(json_file);
|
||||
json& colors = config.json();
|
||||
if(!COLOR.initialized) {
|
||||
COLOR.initialized = true;
|
||||
|
||||
for(auto [key, value_specs] : colors.items()) {
|
||||
const string& base_key = key;
|
||||
COLOR.config = json_file;
|
||||
Config config(json_file);
|
||||
json& colors = config.json();
|
||||
|
||||
for(auto [value, rgba] : value_specs.items()) {
|
||||
auto color_path = base_key + ":" + value;
|
||||
dbc::check(!COLOR.palettes.contains(color_path),
|
||||
fmt::format("PALLETES config {} already has a color path {}", COLOR.config, color_path));
|
||||
for(auto [key, value_specs] : colors.items()) {
|
||||
const string& base_key = key;
|
||||
|
||||
uint8_t alpha = rgba.size() == 3 ? 255 : (uint8_t)rgba[3];
|
||||
for(auto [value, rgba] : value_specs.items()) {
|
||||
auto color_path = base_key + ":" + value;
|
||||
dbc::check(!COLOR.palettes.contains(color_path),
|
||||
fmt::format("PALLETES config {} already has a color path {}", COLOR.config, color_path));
|
||||
|
||||
sf::Color color{rgba[0], rgba[1], rgba[2], alpha};
|
||||
|
||||
COLOR.palettes.try_emplace(color_path, color);
|
||||
if(rgba.type() == json::value_t::string) {
|
||||
COLOR.pending_refs.try_emplace(color_path, rgba);
|
||||
} else {
|
||||
uint8_t alpha = rgba.size() == 3 ? 255 : (uint8_t)rgba[3];
|
||||
sf::Color color{rgba[0], rgba[1], rgba[2], alpha};
|
||||
COLOR.palettes.try_emplace(color_path, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto [color_path, ref] : COLOR.pending_refs) {
|
||||
dbc::check(COLOR.palettes.contains(ref),
|
||||
fmt::format("In {} you have {} referring to {} but {} doesn't exist.",
|
||||
COLOR.config, color_path, ref, ref));
|
||||
dbc::check(!COLOR.palettes.contains(color_path),
|
||||
fmt::format("Color {} with ref {} is duplicated.", color_path, ref));
|
||||
|
||||
auto color = COLOR.palettes.at(ref);
|
||||
|
||||
COLOR.palettes.try_emplace(color_path, color);
|
||||
}
|
||||
}
|
||||
|
||||
sf::Color get(const string& key) {
|
||||
|
@ -43,11 +63,6 @@ namespace palette {
|
|||
}
|
||||
|
||||
sf::Color get(const string& key, const string& value) {
|
||||
std::string color{key + ":" + value};
|
||||
|
||||
dbc::check(COLOR.palettes.contains(color),
|
||||
fmt::format("COLOR {} is missing from {}", color, COLOR.config));
|
||||
|
||||
return COLOR.palettes.at(color);
|
||||
return get(key + ":" + value);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue