yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
snes_color.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_SNES_COLOR_H_
2#define YAZE_APP_GFX_SNES_COLOR_H_
3
4#include <yaze.h>
5
6#include <cstdint>
7#include <vector>
8
9#include "imgui/imgui.h"
10
11namespace yaze {
12namespace gfx {
13
14constexpr int NumberOfColors = 3143;
15
16// ============================================================================
17// SNES Color Conversion Functions
18// ============================================================================
19//
20// Color Format Guide:
21// - SNES Color (uint16_t): 15-bit BGR format (0bbbbbgggggrrrrr)
22// - snes_color struct: RGB values in 0-255 range
23// - ImVec4: RGBA values in 0.0-1.0 range (standard for ImGui)
24// - SDL_Color: RGBA values in 0-255 range
25//
26// Conversion paths:
27// 1. SNES (uint16_t) <-> snes_color (0-255) <-> ImVec4 (0.0-1.0)
28// 2. Use these functions to convert between formats explicitly
29// ============================================================================
30
37
43uint16_t ConvertRgbToSnes(const snes_color& color);
44
50uint16_t ConvertRgbToSnes(const ImVec4& color);
51
57inline ImVec4 SnesColorToImVec4(const snes_color& color) {
58 return ImVec4(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f,
59 1.0f);
60}
61
67inline snes_color ImVec4ToSnesColor(const ImVec4& color) {
68 snes_color result;
69 result.red = static_cast<uint16_t>(color.x * 255.0f);
70 result.green = static_cast<uint16_t>(color.y * 255.0f);
71 result.blue = static_cast<uint16_t>(color.z * 255.0f);
72 return result;
73}
74
80inline ImVec4 SnesTo8bppColor(uint16_t color_value) {
81 snes_color rgb = ConvertSnesToRgb(color_value);
82 return SnesColorToImVec4(rgb);
83}
84
85std::vector<snes_color> Extract(const char* data, unsigned int offset,
86 unsigned int palette_size);
87
88std::vector<char> Convert(const std::vector<snes_color>& palette);
89
90constexpr uint8_t kColorByteMax = 255;
91constexpr float kColorByteMaxF = 255.f;
92
111 public:
112 constexpr SnesColor()
113 : rgb_({0.f, 0.f, 0.f, 255.f}), snes_(0), rom_color_({0, 0, 0}) {}
114
119 explicit SnesColor(const ImVec4 val) {
120 // Convert from ImGui's 0-1 range to internal 0-255 storage
121 rgb_.x = val.x * kColorByteMax;
122 rgb_.y = val.y * kColorByteMax;
123 rgb_.z = val.z * kColorByteMax;
124 rgb_.w = kColorByteMaxF; // Alpha always 255
125
126 snes_color color;
127 color.red = static_cast<uint16_t>(rgb_.x);
128 color.green = static_cast<uint16_t>(rgb_.y);
129 color.blue = static_cast<uint16_t>(rgb_.z);
130 rom_color_ = color;
131 snes_ = ConvertRgbToSnes(color);
132 }
133
138 explicit SnesColor(const uint16_t val) : snes_(val) {
139 snes_color color = ConvertSnesToRgb(val); // Returns 0-255 RGB
140 // Store 0-255 values in ImVec4 (unconventional but internal)
141 rgb_ = ImVec4(color.red, color.green, color.blue, kColorByteMaxF);
142 rom_color_ = color;
143 }
144
149 explicit SnesColor(const snes_color val)
150 : rgb_(val.red, val.green, val.blue, kColorByteMaxF),
152 rom_color_(val) {}
153
157 SnesColor(uint8_t r, uint8_t g, uint8_t b) {
158 rgb_ = ImVec4(r, g, b, kColorByteMaxF);
159 snes_color color;
160 color.red = r;
161 color.green = g;
162 color.blue = b;
163 snes_ = ConvertRgbToSnes(color);
164 rom_color_ = color;
165 }
166
171 void set_rgb(const ImVec4 val);
172
177 void set_snes(uint16_t val);
178
183 constexpr ImVec4 rgb() const { return rgb_; }
184
188 constexpr snes_color rom_color() const { return rom_color_; }
189
193 constexpr uint16_t snes() const { return snes_; }
194
195 constexpr bool is_modified() const { return modified; }
196 constexpr bool is_transparent() const { return transparent; }
197 constexpr void set_transparent(bool t) { transparent = t; }
198 constexpr void set_modified(bool m) { modified = m; }
199
200 private:
201 ImVec4 rgb_; // Stores 0-255 values (unconventional!)
202 uint16_t snes_; // 15-bit SNES format
203 snes_color rom_color_; // 0-255 RGB struct
204 bool modified = false;
205 bool transparent = false;
206};
207
208SnesColor ReadColorFromRom(int offset, const uint8_t* rom);
209
210SnesColor GetCgxColor(uint16_t color);
211std::vector<SnesColor> GetColFileData(uint8_t* data);
212
213} // namespace gfx
214} // namespace yaze
215
216#endif // YAZE_APP_GFX_SNES_COLOR_H_
SNES Color container.
Definition snes_color.h:110
constexpr snes_color rom_color() const
Get snes_color struct (0-255 RGB)
Definition snes_color.h:188
constexpr void set_modified(bool m)
Definition snes_color.h:198
constexpr ImVec4 rgb() const
Get RGB values (WARNING: stored as 0-255 in ImVec4)
Definition snes_color.h:183
constexpr bool is_transparent() const
Definition snes_color.h:196
SnesColor(const ImVec4 val)
Construct from ImVec4 (0.0-1.0 range)
Definition snes_color.h:119
snes_color rom_color_
Definition snes_color.h:203
constexpr bool is_modified() const
Definition snes_color.h:195
SnesColor(const snes_color val)
Construct from snes_color struct (0-255 range)
Definition snes_color.h:149
SnesColor(uint8_t r, uint8_t g, uint8_t b)
Construct from RGB byte values (0-255)
Definition snes_color.h:157
SnesColor(const uint16_t val)
Construct from SNES 15-bit color.
Definition snes_color.h:138
constexpr void set_transparent(bool t)
Definition snes_color.h:197
constexpr SnesColor()
Definition snes_color.h:112
void set_snes(uint16_t val)
Set color from SNES 15-bit format.
constexpr uint16_t snes() const
Get SNES 15-bit color.
Definition snes_color.h:193
void set_rgb(const ImVec4 val)
Set color from ImVec4 (0.0-1.0 range)
snes_color ConvertSnesToRgb(uint16_t color_snes)
Convert SNES 15-bit color to RGB (0-255 range)
Definition snes_color.cc:19
ImVec4 SnesColorToImVec4(const snes_color &color)
Convert snes_color (0-255) to ImVec4 (0.0-1.0)
Definition snes_color.h:57
std::vector< snes_color > Extract(const char *data, unsigned int offset, unsigned int palette_size)
Definition snes_color.cc:60
uint16_t ConvertRgbToSnes(const snes_color &color)
Convert RGB (0-255) to SNES 15-bit color.
Definition snes_color.cc:33
std::vector< char > Convert(const std::vector< snes_color > &palette)
Definition snes_color.cc:71
snes_color ImVec4ToSnesColor(const ImVec4 &color)
Convert ImVec4 (0.0-1.0) to snes_color (0-255)
Definition snes_color.h:67
SnesColor ReadColorFromRom(int offset, const uint8_t *rom)
Definition snes_color.cc:48
ImVec4 SnesTo8bppColor(uint16_t color_value)
Convert SNES 15-bit color directly to ImVec4 (0.0-1.0)
Definition snes_color.h:80
constexpr int NumberOfColors
Definition snes_color.h:14
constexpr float kColorByteMaxF
Definition snes_color.h:91
constexpr uint8_t kColorByteMax
Definition snes_color.h:90
SnesColor GetCgxColor(uint16_t color)
Definition snes_color.cc:81
std::vector< SnesColor > GetColFileData(uint8_t *data)
Definition snes_color.cc:91
SNES color in 15-bit RGB format (BGR555)
Definition yaze.h:218
uint16_t green
Definition yaze.h:220
uint16_t red
Definition yaze.h:219
uint16_t blue
Definition yaze.h:221
Yet Another Zelda3 Editor (YAZE) - Public C API.