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:
@@ -98,5 +98,25 @@ std::vector<SnesColor> GetColFileData(uint8_t* data) {
|
|||||||
return colors;
|
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 gfx
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include <snes.h>
|
#include <snes.h>
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -69,24 +68,8 @@ class SnesColor {
|
|||||||
rom_color_ = color;
|
rom_color_ = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_rgb(const ImVec4 val) {
|
void set_rgb(const ImVec4 val);
|
||||||
rgb_.x = val.x / kColorByteMax;
|
void set_snes(uint16_t val);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr ImVec4 rgb() const { return rgb_; }
|
constexpr ImVec4 rgb() const { return rgb_; }
|
||||||
constexpr snes_color rom_color() const { return rom_color_; }
|
constexpr snes_color rom_color() const { return rom_color_; }
|
||||||
|
|||||||
@@ -17,6 +17,62 @@
|
|||||||
namespace yaze {
|
namespace yaze {
|
||||||
namespace gfx {
|
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
|
* @namespace yaze::gfx::palette_group_internal
|
||||||
* @brief Internal functions for loading palettes by group.
|
* @brief Internal functions for loading palettes by group.
|
||||||
|
|||||||
@@ -129,59 +129,26 @@ class SnesPalette {
|
|||||||
static constexpr size_t kMaxColors = 256;
|
static constexpr size_t kMaxColors = 256;
|
||||||
using ColorArray = std::array<SnesColor, kMaxColors>;
|
using ColorArray = std::array<SnesColor, kMaxColors>;
|
||||||
|
|
||||||
// Default constructor
|
|
||||||
SnesPalette() : size_(0) {}
|
SnesPalette() : size_(0) {}
|
||||||
|
SnesPalette(char* data);
|
||||||
// Constructor from vector of uint16_t (SNES color values)
|
SnesPalette(const unsigned char* snes_pal);
|
||||||
explicit SnesPalette(const std::vector<uint16_t>& colors) : size_(0) {
|
SnesPalette(const char* data, size_t length);
|
||||||
for (const auto& color : colors) {
|
SnesPalette(const std::vector<uint16_t>& colors);
|
||||||
if (size_ < kMaxColors) {
|
SnesPalette(const std::vector<SnesColor>& colors);
|
||||||
colors_[size_++] = SnesColor(color);
|
SnesPalette(const std::vector<ImVec4>& colors);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const SnesColor& operator[](size_t index) const { return colors_[index]; }
|
const SnesColor& operator[](size_t index) const { return colors_[index]; }
|
||||||
|
|
||||||
SnesColor& operator[](size_t index) { 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_; }
|
size_t size() const { return size_; }
|
||||||
bool empty() const { return size_ == 0; }
|
bool empty() const { return size_ == 0; }
|
||||||
|
|
||||||
// Iterators
|
|
||||||
auto begin() { return colors_.begin(); }
|
auto begin() { return colors_.begin(); }
|
||||||
auto end() { return colors_.begin() + size_; }
|
auto end() { return colors_.begin() + size_; }
|
||||||
auto begin() const { return colors_.begin(); }
|
auto begin() const { return colors_.begin(); }
|
||||||
auto end() const { return colors_.begin() + size_; }
|
auto end() const { return colors_.begin() + size_; }
|
||||||
|
|
||||||
// Color manipulation
|
|
||||||
void AddColor(const SnesColor& color) {
|
void AddColor(const SnesColor& color) {
|
||||||
if (size_ < kMaxColors) {
|
if (size_ < kMaxColors) {
|
||||||
colors_[size_++] = color;
|
colors_[size_++] = color;
|
||||||
@@ -196,7 +163,6 @@ class SnesPalette {
|
|||||||
|
|
||||||
void clear() { size_ = 0; }
|
void clear() { size_ = 0; }
|
||||||
|
|
||||||
// Sub-palette creation
|
|
||||||
SnesPalette sub_palette(size_t start, size_t length) const {
|
SnesPalette sub_palette(size_t start, size_t length) const {
|
||||||
SnesPalette result;
|
SnesPalette result;
|
||||||
if (start >= size_) {
|
if (start >= size_) {
|
||||||
@@ -209,7 +175,6 @@ class SnesPalette {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comparison operators
|
|
||||||
bool operator==(const SnesPalette& other) const {
|
bool operator==(const SnesPalette& other) const {
|
||||||
if (size_ != other.size_) {
|
if (size_ != other.size_) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user