Now have a simple color palette system.

This commit is contained in:
Zed A. Shaw 2025-07-16 12:46:49 -04:00
parent 0272ba8540
commit 48a7f72411
6 changed files with 93 additions and 2 deletions

40
palette.cpp Normal file
View file

@ -0,0 +1,40 @@
#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;
};
static PaletteMgr COLOR;
void init(const string &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 already has a color path {}", 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) {
return COLOR.palettes.at(key);
}
}