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

@@ -1,7 +1,10 @@
#ifndef CLRSYNC_CORE_PALETTE_COLOR_KEYS_HPP
#define CLRSYNC_CORE_PALETTE_COLOR_KEYS_HPP
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <unordered_map>
#include <string>
namespace clrsync::core
{
@@ -72,5 +75,67 @@ constexpr const char* COLOR_KEYS[] = {
};
constexpr size_t NUM_COLOR_KEYS = std::size(COLOR_KEYS);
inline const std::unordered_map<std::string, uint32_t> DEFAULT_COLORS = {
{"background", 0x1e1e1eff},
{"on_background", 0xd4d4d4ff},
{"surface", 0x252526ff},
{"on_surface", 0xe8e8e8ff},
{"surface_variant", 0x2d2d30ff},
{"on_surface_variant", 0xccccccff},
{"border_focused", 0x007accff},
{"border", 0x3e3e42ff},
{"foreground", 0xccccccff},
{"cursor", 0xaeafadff},
{"accent", 0x0e639cff},
{"success", 0x4ec9b0ff},
{"info", 0x4fc1ffff},
{"warning", 0xdcdcaaff},
{"error", 0xf48771ff},
{"on_success", 0x000000ff},
{"on_info", 0x000000ff},
{"on_warning", 0x000000ff},
{"on_error", 0xffffffff},
{"editor_background", 0x1e1e1eff},
{"editor_command", 0xd7ba7dff},
{"editor_comment", 0x6a9955ff},
{"editor_disabled", 0x808080ff},
{"editor_emphasis", 0x569cd6ff},
{"editor_error", 0xf44747ff},
{"editor_inactive", 0x858585ff},
{"editor_line_number", 0x858585ff},
{"editor_link", 0x3794ffff},
{"editor_main", 0xd4d4d4ff},
{"editor_selected", 0x264f78ff},
{"editor_selection_inactive", 0x3a3d41ff},
{"editor_string", 0xce9178ff},
{"editor_success", 0x89d185ff},
{"editor_warning", 0xcca700ff},
{"base00", 0x181818ff},
{"base01", 0x282828ff},
{"base02", 0x383838ff},
{"base03", 0x585858ff},
{"base04", 0xb8b8b8ff},
{"base05", 0xd8d8d8ff},
{"base06", 0xe8e8e8ff},
{"base07", 0xf8f8f8ff},
{"base08", 0xab4642ff},
{"base09", 0xdc9656ff},
{"base0A", 0xf7ca88ff},
{"base0B", 0xa1b56cff},
{"base0C", 0x86c1b9ff},
{"base0D", 0x7cafc2ff},
{"base0E", 0xba8bafff},
{"base0F", 0xa16946ff},
};
} // namespace clrsync::core
#endif

View File

@@ -5,6 +5,7 @@
#include <unordered_map>
#include <core/palette/color.hpp>
#include <core/palette/color_keys.hpp>
namespace clrsync::core
{
@@ -37,8 +38,15 @@ class palette
{
return it->second;
}
static color default_color{};
return default_color;
auto default_it = DEFAULT_COLORS.find(key);
if (default_it != DEFAULT_COLORS.end())
{
static color default_color;
default_color.set(default_it->second);
return default_color;
}
static color empty_color{};
return empty_color;
}
const std::unordered_map<std::string, color> &colors() const

View File

@@ -26,13 +26,25 @@ template <typename FileType> class palette_file
if (!m_file->parse())
return false;
m_palette.set_name(m_file->get_string_value("general", "name"));
for (const auto &color_key : COLOR_KEYS)
{
auto it = DEFAULT_COLORS.find(color_key);
if (it != DEFAULT_COLORS.end())
{
m_palette.set_color(color_key, core::color(it->second));
}
}
for (const auto &color_key : COLOR_KEYS)
{
auto color_str = m_file->get_string_value("colors", color_key);
core::color color{0x000000FF};
if (!color_str.empty())
{
core::color color;
color.from_hex_string(color_str);
m_palette.set_color(color_key, color);
m_palette.set_color(color_key, color);
}
}
return true;
}

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();