From 1b7b56d7dc4003ab612c64bbb56d86331afdb446 Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 29 Dec 2024 10:43:59 -0500 Subject: [PATCH] Refactor color handling: update SnesColor references to gfx::SnesColor and add Color struct with utility functions --- src/app/gui/color.cc | 11 +++++------ src/app/gui/color.h | 44 +++++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/app/gui/color.cc b/src/app/gui/color.cc index 0b356e36..ab37f8d3 100644 --- a/src/app/gui/color.cc +++ b/src/app/gui/color.cc @@ -1,18 +1,17 @@ #include "color.h" -#include "imgui/imgui.h" - #include #include #include "app/gfx/bitmap.h" -#include "app/gfx/snes_palette.h" #include "app/gfx/snes_color.h" +#include "app/gfx/snes_palette.h" +#include "imgui/imgui.h" namespace yaze { namespace gui { -ImVec4 ConvertSnesColorToImVec4(const SnesColor& color) { +ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor& color) { return ImVec4(static_cast(color.rgb().x) / 255.0f, static_cast(color.rgb().y) / 255.0f, static_cast(color.rgb().z) / 255.0f, @@ -21,7 +20,7 @@ ImVec4 ConvertSnesColorToImVec4(const SnesColor& color) { ); } -IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor& color, +IMGUI_API bool SnesColorButton(absl::string_view id, gfx::SnesColor& color, ImGuiColorEditFlags flags, const ImVec2& size_arg) { // Convert the SNES color values to ImGui color values @@ -38,7 +37,7 @@ IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor& color, return pressed; } -IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor* color, +IMGUI_API bool SnesColorEdit4(absl::string_view label, gfx::SnesColor* color, ImGuiColorEditFlags flags) { ImVec4 displayColor = ConvertSnesColorToImVec4(*color); diff --git a/src/app/gui/color.h b/src/app/gui/color.h index 9c915a19..38c4c814 100644 --- a/src/app/gui/color.h +++ b/src/app/gui/color.h @@ -1,39 +1,53 @@ #ifndef YAZE_GUI_COLOR_H #define YAZE_GUI_COLOR_H -#include "imgui/imgui.h" - -#include +#include #include +#include "imgui/imgui.h" + #include "absl/status/status.h" -#include "app/gfx/bitmap.h" #include "app/gfx/snes_palette.h" namespace yaze { namespace gui { -using gfx::SnesColor; +struct Color { + float red; + float blue; + float green; + float alpha; +}; + +inline ImVec4 ConvertColorToImVec4(const Color &color) { + return ImVec4(color.red, color.green, color.blue, color.alpha); +} + +inline std::string ColorToHexString(const Color &color) { + return std::format( + "{:02X}{:02X}{:02X}{:02X}", static_cast(color.red * 255), + static_cast(color.green * 255), static_cast(color.blue * 255), + static_cast(color.alpha * 255)); +} // A utility function to convert an SnesColor object to an ImVec4 with // normalized color values -ImVec4 ConvertSnesColorToImVec4(const SnesColor& color); +ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor &color); // The wrapper function for ImGui::ColorButton that takes a SnesColor reference -IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor& color, +IMGUI_API bool SnesColorButton(absl::string_view id, gfx::SnesColor &color, ImGuiColorEditFlags flags = 0, - const ImVec2& size_arg = ImVec2(0, 0)); + const ImVec2 &size_arg = ImVec2(0, 0)); -IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor* color, +IMGUI_API bool SnesColorEdit4(absl::string_view label, gfx::SnesColor *color, ImGuiColorEditFlags flags = 0); -absl::Status DisplayPalette(gfx::SnesPalette& palette, bool loaded); +absl::Status DisplayPalette(gfx::SnesPalette &palette, bool loaded); -void SelectablePalettePipeline(uint64_t& palette_id, bool& refresh_graphics, - gfx::SnesPalette& palette); +void SelectablePalettePipeline(uint64_t &palette_id, bool &refresh_graphics, + gfx::SnesPalette &palette); -} // namespace gui - -} // namespace yaze +} // namespace gui +} // namespace yaze #endif