diff --git a/src/app/gfx/snes_color.cc b/src/app/gfx/snes_color.cc index 34fa10b0..221477d7 100644 --- a/src/app/gfx/snes_color.cc +++ b/src/app/gfx/snes_color.cc @@ -98,5 +98,25 @@ std::vector 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 diff --git a/src/app/gfx/snes_color.h b/src/app/gfx/snes_color.h index 78569113..bb3e05ec 100644 --- a/src/app/gfx/snes_color.h +++ b/src/app/gfx/snes_color.h @@ -3,7 +3,6 @@ #include -#include #include #include @@ -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_; } diff --git a/src/app/gfx/snes_palette.cc b/src/app/gfx/snes_palette.cc index 8a7e67d0..22f86053 100644 --- a/src/app/gfx/snes_palette.cc +++ b/src/app/gfx/snes_palette.cc @@ -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(data[i + 1]) << 8); + col.set_snes(col.snes() | static_cast(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(data[i + 1]) << 8) | + static_cast(data[i]); + colors_[size_++] = SnesColor(color); + } +} + +SnesPalette::SnesPalette(const std::vector &colors) : size_(0) { + for (const auto &color : colors) { + if (size_ < kMaxColors) { + colors_[size_++] = SnesColor(color); + } + } +} + +SnesPalette::SnesPalette(const std::vector &colors) : size_(0) { + for (const auto &color : colors) { + if (size_ < kMaxColors) { + colors_[size_++] = color; + } + } +} + +SnesPalette::SnesPalette(const std::vector &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. diff --git a/src/app/gfx/snes_palette.h b/src/app/gfx/snes_palette.h index 08674558..becb2ab6 100644 --- a/src/app/gfx/snes_palette.h +++ b/src/app/gfx/snes_palette.h @@ -129,59 +129,26 @@ class SnesPalette { static constexpr size_t kMaxColors = 256; using ColorArray = std::array; - // Default constructor SnesPalette() : size_(0) {} - - // Constructor from vector of uint16_t (SNES color values) - explicit SnesPalette(const std::vector& 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& 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(data[i + 1]) << 8) | - static_cast(data[i]); - colors_[size_++] = SnesColor(color); - } - } - - // Constructor from ImVec4 colors - explicit SnesPalette(const std::vector& 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& colors); + SnesPalette(const std::vector& colors); + SnesPalette(const std::vector& 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;