Skip to content

Commit

Permalink
Move color mapping to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Smertig committed Jun 18, 2022
1 parent 80d6a05 commit 3e73762
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
16 changes: 15 additions & 1 deletion res/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,19 @@
"path": "res/body/{color}.png",
"pixel_width": 182,
"origin": [ 98, 123 ]
}
},
"colors": [
"#C51111",
"#132ED1",
"#117F2D",
"#ED54BA",
"#EF7D0D",
"#F5F557",
"#3F474E",
"#D6E0F0",
"#6B2FBB",
"#71491E",
"#38FEDC",
"#50EF39"
]
}
16 changes: 15 additions & 1 deletion res_hq/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,19 @@
"path": "res/body/{color}.png",
"pixel_width": 182,
"origin": [ 98, 123 ]
}
},
"colors": [
"#C51111",
"#132ED1",
"#117F2D",
"#ED54BA",
"#EF7D0D",
"#F5F557",
"#3F474E",
"#D6E0F0",
"#6B2FBB",
"#71491E",
"#38FEDC",
"#50EF39"
]
}
18 changes: 18 additions & 0 deletions source/resources/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ resources::config::config() {
if (m_maps.size() != 3) {
throw std::runtime_error("expected 3 maps in config");
}

const auto color_strings = config.at("colors").get<std::vector<std::string>>();
for (const auto& color_string : color_strings) {
if (color_string.size() != 7 || color_string[0] != '#') {
throw std::runtime_error("expected color in form #RRGGBB");
}

m_colors.emplace_back(std::stol(color_string.substr(1), nullptr, 16));
}
}

const config &config::instance() {
Expand Down Expand Up @@ -126,4 +135,13 @@ std::string config::get_body_path(int color) {
return fmt::format(instance().m_body.path, fmt::arg("color", color));
}

std::optional<std::uint32_t> config::try_get_color(int color_id) {
const auto& colors = instance().m_colors;
if (color_id >= colors.size()) {
return std::nullopt;
}

return colors[color_id];
}

} // namespace resources
4 changes: 4 additions & 0 deletions source/resources/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include <string>
#include <functional>
#include <optional>

namespace resources {

Expand All @@ -31,6 +32,7 @@ class config {
texture_config m_player;
texture_config m_ghost;
texture_config m_body;
std::vector<std::uint32_t> m_colors;

config();

Expand All @@ -53,6 +55,8 @@ class config {
static std::string get_player_path(int color);
static std::string get_ghost_path(int color);
static std::string get_body_path(int color);

static std::optional<std::uint32_t> try_get_color(int color_id);
};

} // namespace resources
Expand Down
19 changes: 4 additions & 15 deletions source/ui/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "utils.hpp"

#include <resources/config.hpp>

namespace ui::utils {

const char* get_map_name(int map_id) {
Expand All @@ -12,21 +14,8 @@ const char* get_map_name(int map_id) {
}

ImColor ui::utils::convert_color(int color_id) {
switch (color_id) {
case 0: return ImColor(0xC5, 0x11, 0x11);
case 1: return ImColor(0x13, 0x2E, 0xD1);
case 2: return ImColor(0x11, 0x7F, 0x2D);
case 3: return ImColor(0xED, 0x54, 0xBA);
case 4: return ImColor(0xEF, 0x7D, 0x0D);
case 5: return ImColor(0xF5, 0xF5, 0x57);
case 6: return ImColor(0x3F, 0x47, 0x4E);
case 7: return ImColor(0xD6, 0xE0, 0xF0);
case 8: return ImColor(0x6B, 0x2F, 0xBB);
case 9: return ImColor(0x71, 0x49, 0x1E);
case 10: return ImColor(0x38, 0xFE, 0xDC);
case 11: return ImColor(0x50, 0xEF, 0x39);
default: return ImColor();
}
const auto color = resources::config::try_get_color(color_id);
return color ? ImColor(*color) : ImColor();
}

} // namespace ui::utils

0 comments on commit 3e73762

Please sign in to comment.