Refactor SnesColor class to use constexpr for constructors and methods, improving compile-time evaluation and performance; include <array> for better type handling.

This commit is contained in:
scawful
2025-04-17 21:49:55 -04:00
parent 44e13cf4bb
commit 9ad41f9f8b

View File

@@ -3,6 +3,7 @@
#include <snes.h> #include <snes.h>
#include <array>
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
@@ -37,24 +38,28 @@ constexpr float kColorByteMaxF = 255.f;
*/ */
class SnesColor { class SnesColor {
public: public:
SnesColor() : rgb_(0.f, 0.f, 0.f, 0.f), snes_(0), rom_color_({0, 0, 0}) {} constexpr SnesColor()
explicit SnesColor(const ImVec4 val) : rgb_(val) { : rgb_({0.f, 0.f, 0.f, 0.f}), snes_(0), rom_color_({0, 0, 0}) {}
constexpr explicit SnesColor(const ImVec4 val) : rgb_(val) {
snes_color color; snes_color color;
color.red = val.x / kColorByteMax; color.red = val.x / kColorByteMax;
color.green = val.y / kColorByteMax; color.green = val.y / kColorByteMax;
color.blue = val.z / kColorByteMax; color.blue = val.z / kColorByteMax;
snes_ = ConvertRgbToSnes(color); snes_ = ConvertRgbToSnes(color);
} }
explicit SnesColor(const uint16_t val) : snes_(val) {
constexpr explicit SnesColor(const uint16_t val) : snes_(val) {
snes_color color = ConvertSnesToRgb(val); snes_color color = ConvertSnesToRgb(val);
rgb_ = ImVec4(color.red, color.green, color.blue, 0.f); rgb_ = ImVec4(color.red, color.green, color.blue, 0.f);
} }
explicit SnesColor(const snes_color val) explicit SnesColor(const snes_color val)
: rgb_(val.red, val.green, val.blue, kColorByteMaxF), : rgb_(val.red, val.green, val.blue, kColorByteMaxF),
snes_(ConvertRgbToSnes(val)), snes_(ConvertRgbToSnes(val)),
rom_color_(val) {} rom_color_(val) {}
SnesColor(uint8_t r, uint8_t g, uint8_t b) { constexpr SnesColor(uint8_t r, uint8_t g, uint8_t b) {
rgb_ = ImVec4(r, g, b, kColorByteMaxF); rgb_ = ImVec4(r, g, b, kColorByteMaxF);
snes_color color; snes_color color;
color.red = r; color.red = r;
@@ -64,8 +69,6 @@ class SnesColor {
rom_color_ = color; rom_color_ = color;
} }
ImVec4 rgb() const { return rgb_; }
void set_rgb(const ImVec4 val) { void set_rgb(const ImVec4 val) {
rgb_.x = val.x / kColorByteMax; rgb_.x = val.x / kColorByteMax;
rgb_.y = val.y / kColorByteMax; rgb_.y = val.y / kColorByteMax;
@@ -78,20 +81,20 @@ class SnesColor {
snes_ = ConvertRgbToSnes(color); snes_ = ConvertRgbToSnes(color);
modified = true; modified = true;
} }
constexpr void set_snes(uint16_t val) {
void set_snes(uint16_t val) {
snes_ = val; snes_ = val;
snes_color col = ConvertSnesToRgb(val); snes_color col = ConvertSnesToRgb(val);
rgb_ = ImVec4(col.red, col.green, col.blue, 0.f); rgb_ = ImVec4(col.red, col.green, col.blue, 0.f);
modified = true; modified = true;
} }
snes_color rom_color() const { return rom_color_; } constexpr ImVec4 rgb() const { return rgb_; }
uint16_t snes() const { return snes_; } constexpr snes_color rom_color() const { return rom_color_; }
bool is_modified() const { return modified; } constexpr uint16_t snes() const { return snes_; }
bool is_transparent() const { return transparent; } constexpr bool is_modified() const { return modified; }
void set_transparent(bool t) { transparent = t; } constexpr bool is_transparent() const { return transparent; }
void set_modified(bool m) { modified = m; } constexpr void set_transparent(bool t) { transparent = t; }
constexpr void set_modified(bool m) { modified = m; }
private: private:
ImVec4 rgb_; ImVec4 rgb_;