yaze 0.2.0
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
7#include "imgui/imgui.h"
8
9namespace yaze {
10namespace app {
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 snes_color result;
22
23 result.red = (color_snes % SNES_RED_MASK) * 8;
24 result.green = ((color_snes / SNES_GREEN_MASK) % SNES_GREEN_MASK) * 8;
25 result.blue = ((color_snes / SNES_BLUE_SHIFT) % SNES_BLUE_MASK) * 8;
26
27 result.red += result.red / SNES_RED_MASK;
28 result.green += result.green / SNES_GREEN_MASK;
29 result.blue += result.blue / SNES_BLUE_MASK;
30
31 return result;
32}
33
34uint16_t ConvertRgbToSnes(const snes_color& color) {
35 uint16_t red = color.red / 8;
36 uint16_t green = color.green / 8;
37 uint16_t blue = color.blue / 8;
38 return (blue * SNES_BLUE_SHIFT) + (green * SNES_GREEN_SHIFT) + red;
39}
40
41uint16_t ConvertRgbToSnes(const ImVec4& color) {
42 snes_color new_color;
43 new_color.red = color.x * kColorByteMax;
44 new_color.green = color.y * kColorByteMax;
45 new_color.blue = color.z * kColorByteMax;
46 return ConvertRgbToSnes(new_color);
47}
48
49SnesColor ReadColorFromRom(int offset, const uint8_t* rom) {
50 short color = (uint16_t)((rom[offset + 1]) << 8) | rom[offset];
51 snes_color new_color;
52 new_color.red = (color & 0x1F) * 8;
53 new_color.green = ((color >> 5) & 0x1F) * 8;
54 new_color.blue = ((color >> 10) & 0x1F) * 8;
55 SnesColor snes_color(new_color);
56 return snes_color;
57}
58
59std::vector<snes_color> Extract(const char* data, unsigned int offset,
60 unsigned int palette_size) {
61 std::vector<snes_color> palette(palette_size);
62 for (unsigned int i = 0; i < palette_size * 2; i += 2) {
63 uint16_t snes_color = (static_cast<uint8_t>(data[offset + i + 1]) << 8) |
64 static_cast<uint8_t>(data[offset + i]);
65 palette[i / 2] = ConvertSnesToRgb(snes_color);
66 }
67 return palette;
68}
69
70std::vector<char> Convert(const std::vector<snes_color>& palette) {
71 std::vector<char> data(palette.size() * 2);
72 for (unsigned int i = 0; i < palette.size(); i++) {
73 uint16_t snes_data = ConvertRgbToSnes(palette[i]);
74 data[i * 2] = snes_data & 0xFF;
75 data[i * 2 + 1] = snes_data >> 8;
76 }
77 return data;
78}
79
80SnesColor GetCgxColor(uint16_t color) {
81 ImVec4 rgb;
82 rgb.x = (color & 0x1F) * 8;
83 rgb.y = ((color & 0x3E0) >> 5) * 8;
84 rgb.z = ((color & 0x7C00) >> 10) * 8;
85 SnesColor toret;
86 toret.set_rgb(rgb);
87 return toret;
88}
89
90std::vector<SnesColor> GetColFileData(uint8_t* data) {
91 std::vector<SnesColor> colors;
92 colors.reserve(256);
93 colors.resize(256);
94
95 for (int i = 0; i < 512; i += 2) {
96 colors[i / 2] = GetCgxColor((uint16_t)((data[i + 1] << 8) + data[i]));
97 }
98
99 return colors;
100}
101
102} // namespace gfx
103} // namespace app
104} // namespace yaze
SNES Color container.
Definition snes_color.h:39
void set_rgb(const ImVec4 val)
Definition snes_color.h:70
struct snes_color snes_color
Primitive of 16-bit RGB SNES color.
constexpr uint16_t SNES_GREEN_SHIFT
Definition snes_color.cc:17
SnesColor GetCgxColor(uint16_t color)
Definition snes_color.cc:80
SnesColor ReadColorFromRom(int offset, const uint8_t *rom)
Definition snes_color.cc:49
constexpr uint16_t SNES_RED_MASK
Definition snes_color.cc:13
std::vector< SnesColor > GetColFileData(uint8_t *data)
Definition snes_color.cc:90
constexpr uint16_t SNES_BLUE_MASK
Definition snes_color.cc:15
snes_color ConvertSnesToRgb(uint16_t color_snes)
Definition snes_color.cc:20
constexpr uint16_t SNES_GREEN_MASK
Definition snes_color.cc:14
std::vector< char > Convert(const std::vector< snes_color > &palette)
Definition snes_color.cc:70
uint16_t ConvertRgbToSnes(const snes_color &color)
Definition snes_color.cc:34
constexpr uint8_t kColorByteMax
Definition snes_color.h:26
constexpr uint16_t SNES_BLUE_SHIFT
Definition snes_color.cc:18
std::vector< snes_color > Extract(const char *data, unsigned int offset, unsigned int palette_size)
Definition snes_color.cc:59
Definition common.cc:22
Primitive of 16-bit RGB SNES color.
Definition snes_color.h:13
uint16_t green
Definition snes_color.h:16
uint16_t red
Definition snes_color.h:14
uint16_t blue
Definition snes_color.h:15