yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1#ifndef YAZE_GUI_COLOR_H
2#define YAZE_GUI_COLOR_H
3
4#include <algorithm>
5#include <cmath>
6#include <string>
7
8#include "absl/status/status.h"
9#include "absl/strings/str_format.h"
11#include "imgui/imgui.h"
12
13namespace yaze {
14namespace gui {
15
16struct Color {
17 float red = 0.0f;
18 float green = 0.0f;
19 float blue = 0.0f;
20 float alpha = 1.0f;
21
22 operator ImVec4() const { return ImVec4(red, green, blue, alpha); }
23
24 // HSL conversion utilities for theme generation
25 struct HSL {
26 float h = 0.0f; // 0-360
27 float s = 0.0f; // 0-1
28 float l = 0.0f; // 0-1
29 };
30
31 HSL ToHSL() const {
32 float max_val = std::max({red, green, blue});
33 float min_val = std::min({red, green, blue});
34 float delta = max_val - min_val;
35
36 HSL hsl;
37 hsl.l = (max_val + min_val) / 2.0f;
38
39 if (delta < 0.00001f) {
40 hsl.h = 0.0f;
41 hsl.s = 0.0f;
42 } else {
43 hsl.s = hsl.l > 0.5f ? delta / (2.0f - max_val - min_val)
44 : delta / (max_val + min_val);
45
46 if (max_val == red) {
47 hsl.h = 60.0f * fmodf((green - blue) / delta, 6.0f);
48 } else if (max_val == green) {
49 hsl.h = 60.0f * ((blue - red) / delta + 2.0f);
50 } else {
51 hsl.h = 60.0f * ((red - green) / delta + 4.0f);
52 }
53 if (hsl.h < 0.0f) {
54 hsl.h += 360.0f;
55 }
56 }
57 return hsl;
58 }
59
60 static Color FromHSL(float h, float s, float l, float a = 1.0f) {
61 auto hue_to_rgb = [](float p, float q, float t) {
62 if (t < 0.0f) t += 1.0f;
63 if (t > 1.0f) t -= 1.0f;
64 if (t < 1.0f / 6.0f) return p + (q - p) * 6.0f * t;
65 if (t < 1.0f / 2.0f) return q;
66 if (t < 2.0f / 3.0f) return p + (q - p) * (2.0f / 3.0f - t) * 6.0f;
67 return p;
68 };
69
70 Color result;
71 result.alpha = a;
72
73 if (s < 0.00001f) {
74 result.red = result.green = result.blue = l;
75 } else {
76 float h_norm = h / 360.0f;
77 float q = l < 0.5f ? l * (1.0f + s) : l + s - l * s;
78 float p = 2.0f * l - q;
79 result.red = hue_to_rgb(p, q, h_norm + 1.0f / 3.0f);
80 result.green = hue_to_rgb(p, q, h_norm);
81 result.blue = hue_to_rgb(p, q, h_norm - 1.0f / 3.0f);
82 }
83 return result;
84 }
85
86 // Derive a new color by adjusting HSL components
87 Color WithHue(float new_hue) const {
88 HSL hsl = ToHSL();
89 return FromHSL(new_hue, hsl.s, hsl.l, alpha);
90 }
91
92 Color WithSaturation(float new_sat) const {
93 HSL hsl = ToHSL();
94 return FromHSL(hsl.h, std::clamp(new_sat, 0.0f, 1.0f), hsl.l, alpha);
95 }
96
97 Color WithLightness(float new_light) const {
98 HSL hsl = ToHSL();
99 return FromHSL(hsl.h, hsl.s, std::clamp(new_light, 0.0f, 1.0f), alpha);
100 }
101
102 Color Lighter(float amount) const {
103 HSL hsl = ToHSL();
104 return FromHSL(hsl.h, hsl.s, std::clamp(hsl.l + amount, 0.0f, 1.0f), alpha);
105 }
106
107 Color Darker(float amount) const {
108 HSL hsl = ToHSL();
109 return FromHSL(hsl.h, hsl.s, std::clamp(hsl.l - amount, 0.0f, 1.0f), alpha);
110 }
111
112 Color Saturate(float amount) const {
113 HSL hsl = ToHSL();
114 return FromHSL(hsl.h, std::clamp(hsl.s + amount, 0.0f, 1.0f), hsl.l, alpha);
115 }
116
117 Color Desaturate(float amount) const {
118 HSL hsl = ToHSL();
119 return FromHSL(hsl.h, std::clamp(hsl.s - amount, 0.0f, 1.0f), hsl.l, alpha);
120 }
121
122 Color ShiftHue(float degrees) const {
123 HSL hsl = ToHSL();
124 float new_hue = fmodf(hsl.h + degrees, 360.0f);
125 if (new_hue < 0.0f) new_hue += 360.0f;
126 return FromHSL(new_hue, hsl.s, hsl.l, alpha);
127 }
128
129 Color WithAlpha(float new_alpha) const {
130 return Color{red, green, blue, std::clamp(new_alpha, 0.0f, 1.0f)};
131 }
132};
133
134inline ImVec4 ConvertColorToImVec4(const Color& color) {
135 return ImVec4(color.red, color.green, color.blue, color.alpha);
136}
137
138inline std::string ColorToHexString(const Color& color) {
139 return absl::StrFormat("%02X%02X%02X%02X", static_cast<int>(color.red * 255),
140 static_cast<int>(color.green * 255),
141 static_cast<int>(color.blue * 255),
142 static_cast<int>(color.alpha * 255));
143}
144
145// A utility function to convert an SnesColor object to an ImVec4 with
146// normalized color values
147ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor& color);
148
149// A utility function to convert an ImVec4 to an SnesColor object
150gfx::SnesColor ConvertImVec4ToSnesColor(const ImVec4& color);
151
152// The wrapper function for ImGui::ColorButton that takes a SnesColor reference
153IMGUI_API bool SnesColorButton(absl::string_view id, gfx::SnesColor& color,
154 ImGuiColorEditFlags flags = 0,
155 const ImVec2& size_arg = ImVec2(0, 0));
156
157IMGUI_API bool SnesColorEdit4(absl::string_view label, gfx::SnesColor* color,
158 ImGuiColorEditFlags flags = 0);
159
160// ============================================================================
161// Palette Widget Functions
162// ============================================================================
163
171IMGUI_API bool InlinePaletteSelector(gfx::SnesPalette& palette,
172 int num_colors = 8,
173 int* selected_index = nullptr);
174
182IMGUI_API absl::Status InlinePaletteEditor(gfx::SnesPalette& palette,
183 const std::string& title = "",
184 ImGuiColorEditFlags flags = 0);
185
193IMGUI_API bool PopupPaletteEditor(const char* popup_id,
194 gfx::SnesPalette& palette,
195 ImGuiColorEditFlags flags = 0);
196
197// Legacy functions (kept for compatibility, will be deprecated)
198IMGUI_API bool DisplayPalette(gfx::SnesPalette& palette, bool loaded);
199
200IMGUI_API absl::Status DisplayEditablePalette(gfx::SnesPalette& palette,
201 const std::string& title = "",
202 bool show_color_picker = false,
203 int colors_per_row = 8,
204 ImGuiColorEditFlags flags = 0);
205
206void SelectablePalettePipeline(uint64_t& palette_id, bool& refresh_graphics,
207 gfx::SnesPalette& palette);
208
209// Palette color button with selection and modification indicators
210IMGUI_API bool PaletteColorButton(const char* id, const gfx::SnesColor& color,
211 bool is_selected, bool is_modified,
212 const ImVec2& size = ImVec2(28, 28),
213 ImGuiColorEditFlags flags = 0);
214
215} // namespace gui
216} // namespace yaze
217
218#endif
SNES Color container.
Definition snes_color.h:110
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
ImVec4 ConvertColorToImVec4(const Color &color)
Definition color.h:134
IMGUI_API bool InlinePaletteSelector(gfx::SnesPalette &palette, int num_colors, int *selected_index)
Small inline palette selector - just color buttons for selection.
Definition color.cc:81
IMGUI_API bool SnesColorButton(absl::string_view id, gfx::SnesColor &color, ImGuiColorEditFlags flags, const ImVec2 &size_arg)
Definition color.cc:40
IMGUI_API bool PopupPaletteEditor(const char *popup_id, gfx::SnesPalette &palette, ImGuiColorEditFlags flags)
Popup palette editor - same as inline but in a popup.
Definition color.cc:187
IMGUI_API absl::Status InlinePaletteEditor(gfx::SnesPalette &palette, const std::string &title, ImGuiColorEditFlags flags)
Full inline palette editor with color picker and copy options.
Definition color.cc:121
IMGUI_API bool PaletteColorButton(const char *id, const gfx::SnesColor &color, bool is_selected, bool is_modified, const ImVec2 &size, ImGuiColorEditFlags flags)
Definition color.cc:452
ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor &color)
Convert SnesColor to standard ImVec4 for display.
Definition color.cc:22
void SelectablePalettePipeline(uint64_t &palette_id, bool &refresh_graphics, gfx::SnesPalette &palette)
Definition color.cc:312
IMGUI_API bool SnesColorEdit4(absl::string_view label, gfx::SnesColor *color, ImGuiColorEditFlags flags)
Definition color.cc:57
std::string ColorToHexString(const Color &color)
Definition color.h:138
IMGUI_API bool DisplayPalette(gfx::SnesPalette &palette, bool loaded)
Definition color.cc:238
gfx::SnesColor ConvertImVec4ToSnesColor(const ImVec4 &color)
Convert standard ImVec4 to SnesColor.
Definition color.cc:35
absl::Status DisplayEditablePalette(gfx::SnesPalette &palette, const std::string &title, bool show_color_picker, int colors_per_row, ImGuiColorEditFlags flags)
Definition color.cc:355
Color WithHue(float new_hue) const
Definition color.h:87
HSL ToHSL() const
Definition color.h:31
Color Darker(float amount) const
Definition color.h:107
Color WithLightness(float new_light) const
Definition color.h:97
Color WithAlpha(float new_alpha) const
Definition color.h:129
Color WithSaturation(float new_sat) const
Definition color.h:92
static Color FromHSL(float h, float s, float l, float a=1.0f)
Definition color.h:60
Color Lighter(float amount) const
Definition color.h:102
Color Desaturate(float amount) const
Definition color.h:117
Color ShiftHue(float degrees) const
Definition color.h:122
Color Saturate(float amount) const
Definition color.h:112
float alpha
Definition color.h:20
float green
Definition color.h:18