yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
snes_color.cc
Go to the documentation of this file.
1
3
4#include <cstdint>
5#include <vector>
6
8#include "imgui/imgui.h"
9
10namespace yaze {
11namespace gfx {
12
13constexpr uint16_t SNES_RED_MASK = 32;
14constexpr uint16_t SNES_GREEN_MASK = 32;
15constexpr uint16_t SNES_BLUE_MASK = 32;
16
17constexpr uint16_t SNES_GREEN_SHIFT = 32;
18constexpr uint16_t SNES_BLUE_SHIFT = 1024;
19
20snes_color ConvertSnesToRgb(uint16_t color_snes) {
21 return {ExpandSnesChannel(color_snes & 31),
22 ExpandSnesChannel((color_snes >> 5) & 31),
23 ExpandSnesChannel((color_snes >> 10) & 31)};
24}
25
26uint16_t ConvertRgbToSnes(const snes_color& color) {
27 uint16_t red = color.red / 8;
28 uint16_t green = color.green / 8;
29 uint16_t blue = color.blue / 8;
30 return (blue * SNES_BLUE_SHIFT) + (green * SNES_GREEN_SHIFT) + red;
31}
32
33uint16_t ConvertRgbToSnes(const ImVec4& color) {
34 snes_color new_color;
35 new_color.red = color.x * kColorByteMax;
36 new_color.green = color.y * kColorByteMax;
37 new_color.blue = color.z * kColorByteMax;
38 return ConvertRgbToSnes(new_color);
39}
40
41SnesColor ReadColorFromRom(int offset, const uint8_t* rom) {
42 // Note: Bounds checking should be done by caller before calling this function
43 // This function assumes valid offset and rom pointer
44 const uint16_t color =
45 (static_cast<uint16_t>(rom[offset + 1]) << 8) | rom[offset];
46 return SnesColor(color);
47}
48
49std::vector<snes_color> Extract(const char* data, unsigned int offset,
50 unsigned int palette_size) {
51 std::vector<snes_color> palette(palette_size);
52 for (unsigned int i = 0; i < palette_size * 2; i += 2) {
53 uint16_t snes_color = (static_cast<uint8_t>(data[offset + i + 1]) << 8) |
54 static_cast<uint8_t>(data[offset + i]);
55 palette[i / 2] = ConvertSnesToRgb(snes_color);
56 }
57 return palette;
58}
59
60std::vector<char> Convert(const std::vector<snes_color>& palette) {
61 std::vector<char> data(palette.size() * 2);
62 for (unsigned int i = 0; i < palette.size(); i++) {
63 uint16_t snes_data = ConvertRgbToSnes(palette[i]);
64 data[i * 2] = snes_data & 0xFF;
65 data[i * 2 + 1] = snes_data >> 8;
66 }
67 return data;
68}
69
70SnesColor GetCgxColor(uint16_t color) {
71 return SnesColor(color);
72}
73
74std::vector<SnesColor> GetColFileData(uint8_t* data) {
75 std::vector<SnesColor> colors;
76 colors.reserve(256);
77 colors.resize(256);
78
79 for (int i = 0; i < 512; i += 2) {
80 colors[i / 2] = GetCgxColor((uint16_t)((data[i + 1] << 8) + data[i]));
81 }
82
83 return colors;
84}
85
86void SnesColor::set_rgb(const ImVec4 val) {
87 // IMPORTANT: Input val is expected to be in standard ImVec4 range (0.0-1.0)
88 // We convert to internal 0-255 storage format
89 rgb_.x = val.x * kColorByteMax;
90 rgb_.y = val.y * kColorByteMax;
91 rgb_.z = val.z * kColorByteMax;
92 rgb_.w = kColorByteMaxF; // Alpha always 255
93
94 // Update rom_color_ and snes_ representations
95 snes_color color;
96 color.red = static_cast<uint16_t>(rgb_.x);
97 color.green = static_cast<uint16_t>(rgb_.y);
98 color.blue = static_cast<uint16_t>(rgb_.z);
99
100 rom_color_ = color;
101 snes_ = ConvertRgbToSnes(color);
102 modified = true;
103}
104
105void SnesColor::set_snes(uint16_t val) {
106 // Store SNES 15-bit color
107 snes_ = val;
108
109 // Convert SNES to RGB (0-255)
110 snes_color col = ConvertSnesToRgb(val);
111
112 // Store 0-255 values in ImVec4 (unconventional but our internal format)
113 rgb_ = ImVec4(static_cast<float>(col.red), static_cast<float>(col.green),
114 static_cast<float>(col.blue), kColorByteMaxF);
115
116 rom_color_ = col;
117 modified = true;
118}
119
120} // namespace gfx
121} // namespace yaze
SNES Color container.
Definition snes_color.h:110
snes_color rom_color_
Definition snes_color.h:203
void set_snes(uint16_t val)
Set color from SNES 15-bit format.
void set_rgb(const ImVec4 val)
Set color from ImVec4 (0.0-1.0 range)
Definition snes_color.cc:86
snes_color ConvertSnesToRgb(uint16_t color_snes)
Convert SNES 15-bit color to RGB (0-255 range)
Definition snes_color.cc:20
std::vector< snes_color > Extract(const char *data, unsigned int offset, unsigned int palette_size)
Definition snes_color.cc:49
constexpr uint16_t SNES_GREEN_MASK
Definition snes_color.cc:14
uint16_t ConvertRgbToSnes(const snes_color &color)
Convert RGB (0-255) to SNES 15-bit color.
Definition snes_color.cc:26
std::vector< char > Convert(const std::vector< snes_color > &palette)
Definition snes_color.cc:60
SnesColor ReadColorFromRom(int offset, const uint8_t *rom)
Definition snes_color.cc:41
constexpr uint16_t SNES_BLUE_SHIFT
Definition snes_color.cc:18
constexpr uint16_t SNES_GREEN_SHIFT
Definition snes_color.cc:17
constexpr uint8_t ExpandSnesChannel(uint8_t value)
constexpr uint16_t SNES_RED_MASK
Definition snes_color.cc:13
constexpr uint16_t SNES_BLUE_MASK
Definition snes_color.cc:15
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:70
std::vector< SnesColor > GetColFileData(uint8_t *data)
Definition snes_color.cc:74
SNES color in 15-bit RGB format (BGR555)
uint16_t green
uint16_t red
uint16_t blue