1#ifndef YAZE_GUI_COLOR_H
2#define YAZE_GUI_COLOR_H
8#include "absl/status/status.h"
9#include "absl/strings/str_format.h"
11#include "imgui/imgui.h"
34 float delta = max_val - min_val;
37 hsl.
l = (max_val + min_val) / 2.0f;
39 if (delta < 0.00001f) {
43 hsl.
s = hsl.
l > 0.5f ? delta / (2.0f - max_val - min_val)
44 : delta / (max_val + min_val);
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);
51 hsl.
h = 60.0f * ((
red -
green) / delta + 4.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;
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);
94 return FromHSL(hsl.
h, std::clamp(new_sat, 0.0f, 1.0f), hsl.
l,
alpha);
99 return FromHSL(hsl.
h, hsl.
s, std::clamp(new_light, 0.0f, 1.0f),
alpha);
104 return FromHSL(hsl.
h, hsl.
s, std::clamp(hsl.
l + amount, 0.0f, 1.0f),
alpha);
109 return FromHSL(hsl.
h, hsl.
s, std::clamp(hsl.
l - amount, 0.0f, 1.0f),
alpha);
114 return FromHSL(hsl.
h, std::clamp(hsl.
s + amount, 0.0f, 1.0f), hsl.
l,
alpha);
119 return FromHSL(hsl.
h, std::clamp(hsl.
s - amount, 0.0f, 1.0f), hsl.
l,
alpha);
124 float new_hue = fmodf(hsl.
h + degrees, 360.0f);
125 if (new_hue < 0.0f) new_hue += 360.0f;
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));
154 ImGuiColorEditFlags flags = 0,
155 const ImVec2& size_arg = ImVec2(0, 0));
158 ImGuiColorEditFlags flags = 0);
173 int* selected_index =
nullptr);
183 const std::string& title =
"",
184 ImGuiColorEditFlags flags = 0);
195 ImGuiColorEditFlags flags = 0);
201 const std::string& title =
"",
202 bool show_color_picker =
false,
203 int colors_per_row = 8,
204 ImGuiColorEditFlags flags = 0);
211 bool is_selected,
bool is_modified,
212 const ImVec2& size = ImVec2(28, 28),
213 ImGuiColorEditFlags flags = 0);
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
ImVec4 ConvertColorToImVec4(const Color &color)
IMGUI_API bool InlinePaletteSelector(gfx::SnesPalette &palette, int num_colors, int *selected_index)
Small inline palette selector - just color buttons for selection.
IMGUI_API bool SnesColorButton(absl::string_view id, gfx::SnesColor &color, ImGuiColorEditFlags flags, const ImVec2 &size_arg)
IMGUI_API bool PopupPaletteEditor(const char *popup_id, gfx::SnesPalette &palette, ImGuiColorEditFlags flags)
Popup palette editor - same as inline but in a popup.
IMGUI_API absl::Status InlinePaletteEditor(gfx::SnesPalette &palette, const std::string &title, ImGuiColorEditFlags flags)
Full inline palette editor with color picker and copy options.
IMGUI_API bool PaletteColorButton(const char *id, const gfx::SnesColor &color, bool is_selected, bool is_modified, const ImVec2 &size, ImGuiColorEditFlags flags)
ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor &color)
Convert SnesColor to standard ImVec4 for display.
void SelectablePalettePipeline(uint64_t &palette_id, bool &refresh_graphics, gfx::SnesPalette &palette)
IMGUI_API bool SnesColorEdit4(absl::string_view label, gfx::SnesColor *color, ImGuiColorEditFlags flags)
std::string ColorToHexString(const Color &color)
IMGUI_API bool DisplayPalette(gfx::SnesPalette &palette, bool loaded)
gfx::SnesColor ConvertImVec4ToSnesColor(const ImVec4 &color)
Convert standard ImVec4 to SnesColor.
absl::Status DisplayEditablePalette(gfx::SnesPalette &palette, const std::string &title, bool show_color_picker, int colors_per_row, ImGuiColorEditFlags flags)
Color WithHue(float new_hue) const
Color Darker(float amount) const
Color WithLightness(float new_light) const
Color WithAlpha(float new_alpha) const
Color WithSaturation(float new_sat) const
static Color FromHSL(float h, float s, float l, float a=1.0f)
Color Lighter(float amount) const
Color Desaturate(float amount) const
Color ShiftHue(float degrees) const
Color Saturate(float amount) const