You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
#include <fmt/core.h>
|
|
#include "palette.hpp"
|
|
#include "config.hpp"
|
|
#include "dbc.hpp"
|
|
|
|
namespace palette {
|
|
using std::string;
|
|
using nlohmann::json;
|
|
|
|
struct PaletteMgr {
|
|
std::unordered_map<string, sf::Color> palettes;
|
|
std::string config;
|
|
};
|
|
|
|
static PaletteMgr COLOR;
|
|
|
|
void init(const string &json_file) {
|
|
COLOR.config = json_file;
|
|
Config config(json_file);
|
|
json& colors = config.json();
|
|
|
|
for(auto [key, value_specs] : colors.items()) {
|
|
const string& base_key = key;
|
|
|
|
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));
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
sf::Color get(const string& key) {
|
|
dbc::check(COLOR.palettes.contains(key),
|
|
fmt::format("COLOR {} is missing from {}", key, COLOR.config));
|
|
return COLOR.palettes.at(key);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|