Implement SnesColor and SnesPalette constructors for enhanced color initialization; add methods for setting RGB and SNES values in SnesColor, improving color management and conversion capabilities.

This commit is contained in:
scawful
2025-04-18 19:04:59 -04:00
parent 6c6f7a9581
commit 8c207dc292
4 changed files with 85 additions and 61 deletions

View File

@@ -98,5 +98,25 @@ std::vector<SnesColor> GetColFileData(uint8_t* data) {
return colors;
}
void SnesColor::set_rgb(const ImVec4 val) {
rgb_.x = val.x / kColorByteMax;
rgb_.y = val.y / kColorByteMax;
rgb_.z = val.z / kColorByteMax;
snes_color color;
color.red = val.x;
color.green = val.y;
color.blue = val.z;
rom_color_ = color;
snes_ = ConvertRgbToSnes(color);
modified = true;
}
void SnesColor::set_snes(uint16_t val) {
snes_ = val;
snes_color col = ConvertSnesToRgb(val);
rgb_ = ImVec4(col.red, col.green, col.blue, kColorByteMaxF);
modified = true;
}
} // namespace gfx
} // namespace yaze

View File

@@ -3,7 +3,6 @@
#include <snes.h>
#include <array>
#include <cstdint>
#include <vector>
@@ -69,24 +68,8 @@ class SnesColor {
rom_color_ = color;
}
void set_rgb(const ImVec4 val) {
rgb_.x = val.x / kColorByteMax;
rgb_.y = val.y / kColorByteMax;
rgb_.z = val.z / kColorByteMax;
snes_color color;
color.red = val.x;
color.green = val.y;
color.blue = val.z;
rom_color_ = color;
snes_ = ConvertRgbToSnes(color);
modified = true;
}
constexpr void set_snes(uint16_t val) {
snes_ = val;
snes_color col = ConvertSnesToRgb(val);
rgb_ = ImVec4(col.red, col.green, col.blue, 0.f);
modified = true;
}
void set_rgb(const ImVec4 val);
void set_snes(uint16_t val);
constexpr ImVec4 rgb() const { return rgb_; }
constexpr snes_color rom_color() const { return rom_color_; }

View File

@@ -17,6 +17,62 @@
namespace yaze {
namespace gfx {
SnesPalette::SnesPalette(char *data) {
assert((sizeof(data) % 4 == 0) && (sizeof(data) <= 32));
for (unsigned i = 0; i < sizeof(data); i += 2) {
SnesColor col;
col.set_snes(static_cast<uint8_t>(data[i + 1]) << 8);
col.set_snes(col.snes() | static_cast<uint8_t>(data[i]));
snes_color mColor = ConvertSnesToRgb(col.snes());
col.set_rgb(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
colors_[size_++] = col;
}
}
SnesPalette::SnesPalette(const unsigned char *snes_pal) {
assert((sizeof(snes_pal) % 4 == 0) && (sizeof(snes_pal) <= 32));
for (unsigned i = 0; i < sizeof(snes_pal); i += 2) {
SnesColor col;
col.set_snes(snes_pal[i + 1] << (uint16_t)8);
col.set_snes(col.snes() | snes_pal[i]);
snes_color mColor = ConvertSnesToRgb(col.snes());
col.set_rgb(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
colors_[size_++] = col;
}
}
SnesPalette::SnesPalette(const char *data, size_t length) : size_(0) {
for (size_t i = 0; i < length && size_ < kMaxColors; i += 2) {
uint16_t color = (static_cast<uint8_t>(data[i + 1]) << 8) |
static_cast<uint8_t>(data[i]);
colors_[size_++] = SnesColor(color);
}
}
SnesPalette::SnesPalette(const std::vector<uint16_t> &colors) : size_(0) {
for (const auto &color : colors) {
if (size_ < kMaxColors) {
colors_[size_++] = SnesColor(color);
}
}
}
SnesPalette::SnesPalette(const std::vector<SnesColor> &colors) : size_(0) {
for (const auto &color : colors) {
if (size_ < kMaxColors) {
colors_[size_++] = color;
}
}
}
SnesPalette::SnesPalette(const std::vector<ImVec4> &colors) : size_(0) {
for (const auto &color : colors) {
if (size_ < kMaxColors) {
colors_[size_++] = SnesColor(color);
}
}
}
/**
* @namespace yaze::gfx::palette_group_internal
* @brief Internal functions for loading palettes by group.

View File

@@ -129,59 +129,26 @@ class SnesPalette {
static constexpr size_t kMaxColors = 256;
using ColorArray = std::array<SnesColor, kMaxColors>;
// Default constructor
SnesPalette() : size_(0) {}
// Constructor from vector of uint16_t (SNES color values)
explicit SnesPalette(const std::vector<uint16_t>& colors) : size_(0) {
for (const auto& color : colors) {
if (size_ < kMaxColors) {
colors_[size_++] = SnesColor(color);
}
}
}
// Constructor from vector of SnesColor
explicit SnesPalette(const std::vector<SnesColor>& colors) : size_(0) {
for (const auto& color : colors) {
if (size_ < kMaxColors) {
colors_[size_++] = color;
}
}
}
// Constructor from raw SNES palette data
explicit SnesPalette(const char* data, size_t length) : size_(0) {
for (size_t i = 0; i < length && size_ < kMaxColors; i += 2) {
uint16_t color = (static_cast<uint8_t>(data[i + 1]) << 8) |
static_cast<uint8_t>(data[i]);
colors_[size_++] = SnesColor(color);
}
}
// Constructor from ImVec4 colors
explicit SnesPalette(const std::vector<ImVec4>& colors) : size_(0) {
for (const auto& color : colors) {
if (size_ < kMaxColors) {
colors_[size_++] = SnesColor(color);
}
}
}
SnesPalette(char* data);
SnesPalette(const unsigned char* snes_pal);
SnesPalette(const char* data, size_t length);
SnesPalette(const std::vector<uint16_t>& colors);
SnesPalette(const std::vector<SnesColor>& colors);
SnesPalette(const std::vector<ImVec4>& colors);
const SnesColor& operator[](size_t index) const { return colors_[index]; }
SnesColor& operator[](size_t index) { return colors_[index]; }
void set_size(size_t size) { size_ = size; }
size_t size() const { return size_; }
bool empty() const { return size_ == 0; }
// Iterators
auto begin() { return colors_.begin(); }
auto end() { return colors_.begin() + size_; }
auto begin() const { return colors_.begin(); }
auto end() const { return colors_.begin() + size_; }
// Color manipulation
void AddColor(const SnesColor& color) {
if (size_ < kMaxColors) {
colors_[size_++] = color;
@@ -196,7 +163,6 @@ class SnesPalette {
void clear() { size_ = 0; }
// Sub-palette creation
SnesPalette sub_palette(size_t start, size_t length) const {
SnesPalette result;
if (start >= size_) {
@@ -209,7 +175,6 @@ class SnesPalette {
return result;
}
// Comparison operators
bool operator==(const SnesPalette& other) const {
if (size_ != other.size_) {
return false;