Can now set a color to another already existing color.

master
Zed A. Shaw 2 days ago
parent f4fa50a413
commit 379060b8c7
  1. 15
      assets/config.json
  2. 31
      assets/palette.json
  3. 47
      backend.cpp
  4. 53
      palette.cpp
  5. 4
      tests/palette.cpp

@ -229,24 +229,11 @@
"NW": 8598 "NW": 8598
}, },
"theme": { "theme": {
"black": [0, 0, 0, 255], "NOTE": "colors are in assets/palette.json",
"dark_dark": [10, 10, 10, 255],
"dark_mid": [30, 30, 30, 255],
"dark_light": [60, 60, 60, 255],
"mid": [100, 100, 100, 255],
"light_dark": [150, 150, 150, 255],
"light_mid": [200, 200, 200, 255],
"light_light": [230, 230, 230, 255],
"white": [255, 255, 255, 255],
"padding": 3, "padding": 3,
"border_px": 1, "border_px": 1,
"text_size": 20, "text_size": 20,
"label_size": 20, "label_size": 20,
"fill_color": "dark_mid",
"text_color": "light_light",
"bg_color": "mid",
"border_color": "dark_dark",
"bg_color_dark": "black",
"font_file_name": "assets/text.otf" "font_file_name": "assets/text.otf"
} }
} }

@ -2,22 +2,21 @@
"color": { "color": {
"transparent": [255, 255, 255, 255] "transparent": [255, 255, 255, 255]
}, },
"gui/line": { "gui/theme": {
"light": [200,200,200], "black": [0, 0, 0, 255],
"mid": [100,100,100], "dark_dark": [10, 10, 10, 255],
"dark": [10,10,10] "dark_mid": [30, 30, 30, 255],
}, "dark_light": [60, 60, 60, 255],
"mid": [100, 100, 100, 255],
"gui/text": { "light_dark": [150, 150, 150, 255],
"light": [200,200,200], "light_mid": [200, 200, 200, 255],
"mid": [100,100,100], "light_light": [230, 230, 230, 255],
"dark": [10,10,10] "white": [255, 255, 255, 255],
}, "fill_color": "gui/theme:dark_mid",
"text_color": "gui/theme:light_light",
"gui/accent": { "bg_color": "gui/theme:mid",
"light": [200,200,200], "border_color": "gui/theme:dark_dark",
"mid": [100,100,100], "bg_color_dark": "gui/theme:black"
"dark": [10,10,10]
}, },
"items/fg": { "items/fg": {
"flame": [24, 120, 189], "flame": [24, 120, 189],

@ -3,6 +3,7 @@
#include "sound.hpp" #include "sound.hpp"
#include "textures.hpp" #include "textures.hpp"
#include "config.hpp" #include "config.hpp"
#include "palette.hpp"
namespace sfml { namespace sfml {
using namespace nlohmann; using namespace nlohmann;
@ -39,46 +40,32 @@ namespace sfml {
} }
} }
inline sf::Color to_color(json& config, const std::string& name) {
json& val = config[name];
if(val.type() == json::value_t::array) {
return sf::Color{val[0], val[1], val[2], val[3]};
} else if(val.type() == json::value_t::string) {
json& array = config[val];
return sf::Color{array[0], array[1], array[2], array[3]};
} else {
dbc::sentinel(fmt::format(
"theme config {} has invalid color setting,"
"either use an array of 4 ints or a string"
"referencing another config with 4 ints.", name));
}
}
guecs::Theme Backend::theme() { guecs::Theme Backend::theme() {
palette::init();
auto config = Config("assets/config.json")["theme"]; auto config = Config("assets/config.json")["theme"];
guecs::Theme theme { guecs::Theme theme {
.BLACK=to_color(config, "black"), .BLACK=palette::get("gui/theme:black"),
.DARK_DARK=to_color(config, "dark_dark"), .DARK_DARK=palette::get("gui/theme:dark_dark"),
.DARK_MID=to_color(config, "dark_mid"), .DARK_MID=palette::get("gui/theme:dark_mid"),
.DARK_LIGHT=to_color(config, "dark_light"), .DARK_LIGHT=palette::get("gui/theme:dark_light"),
.MID=to_color(config, "mid"), .MID=palette::get("gui/theme:mid"),
.LIGHT_DARK=to_color(config, "light_dark"), .LIGHT_DARK=palette::get("gui/theme:light_dark"),
.LIGHT_MID=to_color(config, "light_mid"), .LIGHT_MID=palette::get("gui/theme:light_mid"),
.LIGHT_LIGHT=to_color(config, "light_light"), .LIGHT_LIGHT=palette::get("gui/theme:light_light"),
.WHITE=to_color(config, "white"), .WHITE=palette::get("gui/theme:white"),
.TRANSPARENT = sf::Color::Transparent .TRANSPARENT = palette::get("color:transparent")
}; };
theme.PADDING = config["padding"]; theme.PADDING = config["padding"];
theme.BORDER_PX = config["border_px"]; theme.BORDER_PX = config["border_px"];
theme.TEXT_SIZE = config["text_size"]; theme.TEXT_SIZE = config["text_size"];
theme.LABEL_SIZE = config["label_size"]; theme.LABEL_SIZE = config["label_size"];
theme.FILL_COLOR = to_color(config, "fill_color"); theme.FILL_COLOR = palette::get("gui/theme:fill_color");
theme.TEXT_COLOR = to_color(config, "text_color"); theme.TEXT_COLOR = palette::get("gui/theme:text_color");
theme.BG_COLOR = to_color(config, "bg_color"); theme.BG_COLOR = palette::get("gui/theme:bg_color");
theme.BORDER_COLOR = to_color(config, "border_color"); theme.BORDER_COLOR = palette::get("gui/theme:border_color");
theme.BG_COLOR_DARK = to_color(config, "bg_color_dark"); theme.BG_COLOR_DARK = palette::get("gui/theme:bg_color_dark");
theme.FONT_FILE_NAME = Config::path_to(config["font_file_name"]).string(); theme.FONT_FILE_NAME = Config::path_to(config["font_file_name"]).string();
return theme; return theme;

@ -10,30 +10,50 @@ namespace palette {
struct PaletteMgr { struct PaletteMgr {
std::unordered_map<string, sf::Color> palettes; std::unordered_map<string, sf::Color> palettes;
std::string config; std::string config;
std::unordered_map<string, string> pending_refs;
bool initialized = false;
}; };
static PaletteMgr COLOR; static PaletteMgr COLOR;
void init(const string &json_file) { void init(const string &json_file) {
COLOR.config = json_file; if(!COLOR.initialized) {
Config config(json_file); COLOR.initialized = true;
json& colors = config.json();
for(auto [key, value_specs] : colors.items()) { COLOR.config = json_file;
const string& base_key = key; Config config(json_file);
json& colors = config.json();
for(auto [value, rgba] : value_specs.items()) { for(auto [key, value_specs] : colors.items()) {
auto color_path = base_key + ":" + value; const string& base_key = key;
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]; 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}; if(rgba.type() == json::value_t::string) {
COLOR.pending_refs.try_emplace(color_path, rgba);
COLOR.palettes.try_emplace(color_path, color); } 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) { sf::Color get(const string& key) {
@ -43,11 +63,6 @@ namespace palette {
} }
sf::Color get(const string& key, const string& value) { sf::Color get(const string& key, const string& value) {
std::string color{key + ":" + value}; return get(key + ":" + value);
dbc::check(COLOR.palettes.contains(color),
fmt::format("COLOR {} is missing from {}", color, COLOR.config));
return COLOR.palettes.at(color);
} }
} }

@ -9,10 +9,10 @@ TEST_CASE("color palette test", "[color-palette]") {
palette::init(); palette::init();
sf::Color expect{10, 10, 10, 255}; sf::Color expect{10, 10, 10, 255};
auto gui_text = palette::get("gui/text:dark"); auto gui_text = palette::get("gui/theme:dark_dark");
REQUIRE(gui_text == expect); REQUIRE(gui_text == expect);
gui_text = palette::get("gui/text", "mid"); gui_text = palette::get("gui/theme", "mid");
REQUIRE(gui_text != expect); REQUIRE(gui_text != expect);
expect = {100, 100, 100, 255}; expect = {100, 100, 100, 255};

Loading…
Cancel
Save