feat: init palettes with default colorscheme to avoid messed up UI

This commit is contained in:
2025-12-17 09:50:14 +03:00
parent 5bb8a687ea
commit e6bac8e220
7 changed files with 114 additions and 64 deletions

View File

@@ -7,12 +7,7 @@ void color_table_renderer::render_color_row(const std::string &name,
palette_controller& controller,
const OnColorChangedCallback& on_changed)
{
const auto &colors = current.colors();
auto it = colors.find(name);
if (it == colors.end())
return;
const clrsync::core::color &col = it->second;
const clrsync::core::color &col = current.get_color(name);
ImGui::TableNextRow();

View File

@@ -30,9 +30,12 @@ void palette_controller::select_palette(const std::string& name)
void palette_controller::create_palette(const std::string& name)
{
std::filesystem::path template_path = clrsync::core::config::get_data_dir() / "palettes" / "cursed.toml";
clrsync::core::palette new_palette = m_palette_manager.load_palette_from_file(template_path.string());
new_palette.set_name(name);
clrsync::core::palette new_palette(name);
for (const auto& [key, hex_value] : clrsync::core::DEFAULT_COLORS)
{
new_palette.set_color(key, clrsync::core::color(hex_value));
}
auto dir = clrsync::core::config::instance().palettes_path();
m_palette_manager.save_palette_to_file(new_palette, dir);

View File

@@ -89,15 +89,10 @@ void preview_renderer::render_code_preview()
void preview_renderer::render_terminal_preview(const clrsync::core::palette& current)
{
auto get_color = [&](const std::string &key) -> ImVec4 {
auto it = current.colors().find(key);
if (it != current.colors().end())
{
const auto &col = it->second;
const uint32_t hex = col.hex();
return {((hex >> 24) & 0xFF) / 255.0f, ((hex >> 16) & 0xFF) / 255.0f,
((hex >> 8) & 0xFF) / 255.0f, ((hex) & 0xFF) / 255.0f};
}
return {1, 1, 1, 1};
const auto &col = current.get_color(key);
const uint32_t hex = col.hex();
return {((hex >> 24) & 0xFF) / 255.0f, ((hex >> 16) & 0xFF) / 255.0f,
((hex >> 8) & 0xFF) / 255.0f, ((hex) & 0xFF) / 255.0f};
};
const ImVec4 editor_bg = get_color("editor_background");

View File

@@ -1,34 +1,20 @@
#include "theme_applier.hpp"
#include "imgui.h"
#include <iostream>
namespace theme_applier
{
void apply_to_editor(TextEditor& editor, const clrsync::core::palette& current)
{
if (current.colors().empty())
return;
auto get_color_u32 = [&](const std::string &key, const std::string &fallback = "") -> uint32_t {
auto it = current.colors().find(key);
if (it == current.colors().end() && !fallback.empty())
{
it = current.colors().find(fallback);
}
if (it != current.colors().end())
{
const auto &col = it->second;
const uint32_t hex = col.hex();
// Convert from RRGGBBAA to AABBGGRR (ImGui format)
const uint32_t r = (hex >> 24) & 0xFF;
const uint32_t g = (hex >> 16) & 0xFF;
const uint32_t b = (hex >> 8) & 0xFF;
const uint32_t a = hex & 0xFF;
return (a << 24) | (b << 16) | (g << 8) | r;
}
return 0xFFFFFFFF;
auto get_color_u32 = [&](const std::string &key) -> uint32_t {
const auto &col = current.get_color(key);
const uint32_t hex = col.hex();
// Convert from RRGGBBAA to AABBGGRR (ImGui format)
const uint32_t r = (hex >> 24) & 0xFF;
const uint32_t g = (hex >> 16) & 0xFF;
const uint32_t b = (hex >> 8) & 0xFF;
const uint32_t a = hex & 0xFF;
return (a << 24) | (b << 16) | (g << 8) | r;
};
auto palette = editor.GetPalette();
@@ -65,25 +51,11 @@ void apply_to_editor(TextEditor& editor, const clrsync::core::palette& current)
void apply_to_imgui(const clrsync::core::palette& current)
{
if (current.colors().empty())
return;
auto getColor = [&](const std::string &key, const std::string &fallback = "") -> ImVec4 {
auto it = current.colors().find(key);
if (it == current.colors().end() && !fallback.empty())
{
it = current.colors().find(fallback);
}
if (it != current.colors().end())
{
const uint32_t hex = it->second.hex();
return {((hex >> 24) & 0xFF) / 255.0f, ((hex >> 16) & 0xFF) / 255.0f,
((hex >> 8) & 0xFF) / 255.0f, ((hex) & 0xFF) / 255.0f};
}
std::cout << "WARNING: Color key '" << key << "' not found!\n";
return {1, 1, 1, 1};
auto getColor = [&](const std::string &key) -> ImVec4 {
const auto &col = current.get_color(key);
const uint32_t hex = col.hex();
return {((hex >> 24) & 0xFF) / 255.0f, ((hex >> 16) & 0xFF) / 255.0f,
((hex >> 8) & 0xFF) / 255.0f, ((hex) & 0xFF) / 255.0f};
};
ImGuiStyle &style = ImGui::GetStyle();