Refactor color handling: update SnesColor references to gfx::SnesColor and add Color struct with utility functions

This commit is contained in:
scawful
2024-12-29 10:43:59 -05:00
parent 5050f358f0
commit 1b7b56d7dc
2 changed files with 34 additions and 21 deletions

View File

@@ -1,18 +1,17 @@
#include "color.h"
#include "imgui/imgui.h"
#include <cmath>
#include <string>
#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<float>(color.rgb().x) / 255.0f,
static_cast<float>(color.rgb().y) / 255.0f,
static_cast<float>(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);

View File

@@ -1,39 +1,53 @@
#ifndef YAZE_GUI_COLOR_H
#define YAZE_GUI_COLOR_H
#include "imgui/imgui.h"
#include <cmath>
#include <format>
#include <string>
#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<int>(color.red * 255),
static_cast<int>(color.green * 255), static_cast<int>(color.blue * 255),
static_cast<int>(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