yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
color.cc
Go to the documentation of this file.
1#include "color.h"
2
3#include "imgui/imgui.h"
4
5#include <cmath>
6#include <string>
7
8#include "app/gfx/bitmap.h"
10#include "app/gfx/snes_color.h"
11
12namespace yaze {
13namespace app {
14namespace gui {
15
16ImVec4 ConvertSnesColorToImVec4(const SnesColor& color) {
17 return ImVec4(static_cast<float>(color.rgb().x) / 255.0f,
18 static_cast<float>(color.rgb().y) / 255.0f,
19 static_cast<float>(color.rgb().z) / 255.0f,
20 1.0f // Assuming alpha is always fully opaque for SNES colors,
21 // adjust if necessary
22 );
23}
24
25IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor& color,
26 ImGuiColorEditFlags flags,
27 const ImVec2& size_arg) {
28 // Convert the SNES color values to ImGui color values
29 ImVec4 displayColor = ConvertSnesColorToImVec4(color);
30
31 // Call the original ImGui::ColorButton with the converted color
32 bool pressed = ImGui::ColorButton(id.data(), displayColor, flags, size_arg);
33 // Add the SNES color representation to the tooltip
34 if (ImGui::IsItemHovered()) {
35 ImGui::BeginTooltip();
36 ImGui::Text("SNES: $%04X", color.snes());
37 ImGui::EndTooltip();
38 }
39 return pressed;
40}
41
42IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor* color,
43 ImGuiColorEditFlags flags) {
44 ImVec4 displayColor = ConvertSnesColorToImVec4(*color);
45
46 // Call the original ImGui::ColorEdit4 with the converted color
47 bool pressed =
48 ImGui::ColorEdit4(label.data(), (float*)&displayColor.x, flags);
49
50 color->set_rgb(displayColor);
51 color->set_snes(gfx::ConvertRGBtoSNES(displayColor));
52
53 return pressed;
54}
55
56absl::Status DisplayPalette(app::gfx::SnesPalette& palette, bool loaded) {
57 static ImVec4 color = ImVec4(0, 0, 0, 255.f);
58 ImGuiColorEditFlags misc_flags = ImGuiColorEditFlags_AlphaPreview |
59 ImGuiColorEditFlags_NoDragDrop |
60 ImGuiColorEditFlags_NoOptions;
61
62 // Generate a default palette. The palette will persist and can be edited.
63 static bool init = false;
64 static ImVec4 saved_palette[32] = {};
65 if (loaded && !init) {
66 for (int n = 0; n < palette.size(); n++) {
67 ASSIGN_OR_RETURN(auto color, palette.GetColor(n));
68 saved_palette[n].x = color.rgb().x / 255;
69 saved_palette[n].y = color.rgb().y / 255;
70 saved_palette[n].z = color.rgb().z / 255;
71 saved_palette[n].w = 255; // Alpha
72 }
73 init = true;
74 }
75
76 static ImVec4 backup_color;
77 ImGui::Text("Current ==>");
78 ImGui::SameLine();
79 ImGui::Text("Previous");
80
81 ImGui::ColorButton(
82 "##current", color,
83 ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf,
84 ImVec2(60, 40));
85 ImGui::SameLine();
86
87 if (ImGui::ColorButton(
88 "##previous", backup_color,
89 ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf,
90 ImVec2(60, 40)))
91 color = backup_color;
92 ImGui::Separator();
93
94 ImGui::BeginGroup(); // Lock X position
95 ImGui::Text("Palette");
96 for (int n = 0; n < IM_ARRAYSIZE(saved_palette); n++) {
97 ImGui::PushID(n);
98 if ((n % 4) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
99
100 ImGuiColorEditFlags palette_button_flags = ImGuiColorEditFlags_NoAlpha |
101 ImGuiColorEditFlags_NoPicker |
102 ImGuiColorEditFlags_NoTooltip;
103 if (ImGui::ColorButton("##palette", saved_palette[n], palette_button_flags,
104 ImVec2(20, 20)))
105 color = ImVec4(saved_palette[n].x, saved_palette[n].y, saved_palette[n].z,
106 color.w); // Preserve alpha!
107
108 if (ImGui::BeginDragDropTarget()) {
109 if (const ImGuiPayload* payload =
110 ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F))
111 memcpy((float*)&saved_palette[n], payload->Data, sizeof(float) * 3);
112 if (const ImGuiPayload* payload =
113 ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F))
114 memcpy((float*)&saved_palette[n], payload->Data, sizeof(float) * 4);
115 ImGui::EndDragDropTarget();
116 }
117
118 ImGui::PopID();
119 }
120 ImGui::EndGroup();
121 ImGui::SameLine();
122
123 ImGui::ColorPicker4("##picker", (float*)&color,
124 misc_flags | ImGuiColorEditFlags_NoSidePreview |
125 ImGuiColorEditFlags_NoSmallPreview);
126 return absl::OkStatus();
127}
128
129void SelectablePalettePipeline(uint64_t& palette_id, bool& refresh_graphics,
130 gfx::SnesPalette& palette) {
131 const auto palette_row_size = 7;
132 if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)100);
133 ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
134 ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
135 ImGui::BeginGroup(); // Lock X position
136 ImGui::Text("Palette");
137 for (int n = 0; n < palette.size(); n++) {
138 ImGui::PushID(n);
139 if ((n % palette_row_size) != 0)
140 ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
141
142 // Check if the current row is selected
143 bool is_selected = (palette_id == n / palette_row_size);
144
145 // Add outline rectangle to the selected row
146 if (is_selected) {
147 ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1.0f, 1.0f, 0.0f, 1.0f));
148 ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 2.0f);
149 }
150
151 if (gui::SnesColorButton("##palette", palette[n],
152 ImGuiColorEditFlags_NoAlpha |
153 ImGuiColorEditFlags_NoPicker |
154 ImGuiColorEditFlags_NoTooltip,
155 ImVec2(20, 20))) {
156 palette_id = n / palette_row_size;
157 refresh_graphics = true;
158 }
159
160 if (is_selected) {
161 ImGui::PopStyleColor();
162 ImGui::PopStyleVar();
163 }
164
165 ImGui::PopID();
166 }
167 ImGui::EndGroup();
168 }
169 ImGui::EndChild();
170}
171
172} // namespace gui
173} // namespace app
174} // namespace yaze
SNES Color container.
Definition snes_color.h:38
void set_rgb(const ImVec4 val)
Definition snes_color.h:69
uint16_t snes() const
Definition snes_color.h:90
void set_snes(uint16_t val)
Definition snes_color.h:82
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
absl::StatusOr< SnesColor > GetColor(int i) const
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition constants.h:77
uint16_t ConvertRGBtoSNES(const snes_color &color)
Definition snes_color.cc:34
IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor *color, ImGuiColorEditFlags flags)
Definition color.cc:42
absl::Status DisplayPalette(app::gfx::SnesPalette &palette, bool loaded)
Definition color.cc:56
void SelectablePalettePipeline(uint64_t &palette_id, bool &refresh_graphics, gfx::SnesPalette &palette)
Definition color.cc:129
ImVec4 ConvertSnesColorToImVec4(const SnesColor &color)
Definition color.cc:16
IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor &color, ImGuiColorEditFlags flags, const ImVec2 &size_arg)
Definition color.cc:25
Definition common.cc:21