Refactor color conversion functions for consistent naming and improved readability

This commit is contained in:
scawful
2024-11-18 14:12:46 -05:00
parent d664762344
commit ec85a206b1
6 changed files with 37 additions and 37 deletions

View File

@@ -433,7 +433,7 @@ absl::Status PaletteEditor::HandleColorPopup(gfx::SnesPalette& palette, int i,
// SNES Format // SNES Format
CustomFormatString(buf, IM_ARRAYSIZE(buf), "$%04X", CustomFormatString(buf, IM_ARRAYSIZE(buf), "$%04X",
ConvertRGBtoSNES(ImVec4(col[0], col[1], col[2], 1.0f))); ConvertRgbToSnes(ImVec4(col[0], col[1], col[2], 1.0f)));
if (Selectable(buf)) SetClipboardText(buf); if (Selectable(buf)) SetClipboardText(buf);
EndPopup(); EndPopup();

View File

@@ -1,11 +1,11 @@
#include "app/gfx/snes_color.h" #include "app/gfx/snes_color.h"
#include "imgui/imgui.h"
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include "imgui/imgui.h"
namespace yaze { namespace yaze {
namespace app { namespace app {
namespace gfx { namespace gfx {
@@ -17,7 +17,7 @@ constexpr uint16_t SNES_BLUE_MASK = 32;
constexpr uint16_t SNES_GREEN_SHIFT = 32; constexpr uint16_t SNES_GREEN_SHIFT = 32;
constexpr uint16_t SNES_BLUE_SHIFT = 1024; constexpr uint16_t SNES_BLUE_SHIFT = 1024;
snes_color ConvertSNEStoRGB(uint16_t color_snes) { snes_color ConvertSnesToRgb(uint16_t color_snes) {
snes_color result; snes_color result;
result.red = (color_snes % SNES_RED_MASK) * 8; result.red = (color_snes % SNES_RED_MASK) * 8;
@@ -31,19 +31,19 @@ snes_color ConvertSNEStoRGB(uint16_t color_snes) {
return result; return result;
} }
uint16_t ConvertRGBtoSNES(const snes_color& color) { uint16_t ConvertRgbToSnes(const snes_color& color) {
uint16_t red = color.red / 8; uint16_t red = color.red / 8;
uint16_t green = color.green / 8; uint16_t green = color.green / 8;
uint16_t blue = color.blue / 8; uint16_t blue = color.blue / 8;
return (blue * SNES_BLUE_SHIFT) + (green * SNES_GREEN_SHIFT) + red; return (blue * SNES_BLUE_SHIFT) + (green * SNES_GREEN_SHIFT) + red;
} }
uint16_t ConvertRGBtoSNES(const ImVec4& color) { uint16_t ConvertRgbToSnes(const ImVec4& color) {
snes_color new_color; snes_color new_color;
new_color.red = color.x * kColorByteMax; new_color.red = color.x * kColorByteMax;
new_color.green = color.y * kColorByteMax; new_color.green = color.y * kColorByteMax;
new_color.blue = color.z * kColorByteMax; new_color.blue = color.z * kColorByteMax;
return ConvertRGBtoSNES(new_color); return ConvertRgbToSnes(new_color);
} }
SnesColor ReadColorFromRom(int offset, const uint8_t* rom) { SnesColor ReadColorFromRom(int offset, const uint8_t* rom) {
@@ -62,7 +62,7 @@ std::vector<snes_color> Extract(const char* data, unsigned int offset,
for (unsigned int i = 0; i < palette_size * 2; i += 2) { for (unsigned int i = 0; i < palette_size * 2; i += 2) {
uint16_t snes_color = (static_cast<uint8_t>(data[offset + i + 1]) << 8) | uint16_t snes_color = (static_cast<uint8_t>(data[offset + i + 1]) << 8) |
static_cast<uint8_t>(data[offset + i]); static_cast<uint8_t>(data[offset + i]);
palette[i / 2] = ConvertSNEStoRGB(snes_color); palette[i / 2] = ConvertSnesToRgb(snes_color);
} }
return palette; return palette;
} }
@@ -70,7 +70,7 @@ std::vector<snes_color> Extract(const char* data, unsigned int offset,
std::vector<char> Convert(const std::vector<snes_color>& palette) { std::vector<char> Convert(const std::vector<snes_color>& palette) {
std::vector<char> data(palette.size() * 2); std::vector<char> data(palette.size() * 2);
for (unsigned int i = 0; i < palette.size(); i++) { for (unsigned int i = 0; i < palette.size(); i++) {
uint16_t snes_data = ConvertRGBtoSNES(palette[i]); uint16_t snes_data = ConvertRgbToSnes(palette[i]);
data[i * 2] = snes_data & 0xFF; data[i * 2] = snes_data & 0xFF;
data[i * 2 + 1] = snes_data >> 8; data[i * 2 + 1] = snes_data >> 8;
} }

View File

@@ -14,9 +14,9 @@ namespace gfx {
constexpr int NumberOfColors = 3143; constexpr int NumberOfColors = 3143;
snes_color ConvertSNEStoRGB(uint16_t snes_color); snes_color ConvertSnesToRgb(uint16_t snes_color);
uint16_t ConvertRGBtoSNES(const snes_color& color); uint16_t ConvertRgbToSnes(const snes_color& color);
uint16_t ConvertRGBtoSNES(const ImVec4& color); uint16_t ConvertRgbToSnes(const ImVec4& color);
std::vector<snes_color> Extract(const char* data, unsigned int offset, std::vector<snes_color> Extract(const char* data, unsigned int offset,
unsigned int palette_size); unsigned int palette_size);
@@ -44,15 +44,15 @@ class SnesColor {
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) { 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) { SnesColor(uint8_t r, uint8_t g, uint8_t b) {
@@ -61,7 +61,7 @@ class SnesColor {
color.red = r; color.red = r;
color.green = g; color.green = g;
color.blue = b; color.blue = b;
snes_ = ConvertRGBtoSNES(color); snes_ = ConvertRgbToSnes(color);
rom_color_ = color; rom_color_ = color;
} }
@@ -76,13 +76,13 @@ class SnesColor {
color.green = val.y; color.green = val.y;
color.blue = val.z; color.blue = val.z;
rom_color_ = color; rom_color_ = color;
snes_ = ConvertRGBtoSNES(color); snes_ = ConvertRgbToSnes(color);
modified = true; modified = true;
} }
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;
} }

View File

@@ -229,7 +229,7 @@ SnesPalette::SnesPalette(char *data) {
SnesColor col; SnesColor col;
col.set_snes(static_cast<uchar>(data[i + 1]) << 8); col.set_snes(static_cast<uchar>(data[i + 1]) << 8);
col.set_snes(col.snes() | static_cast<uchar>(data[i])); col.set_snes(col.snes() | static_cast<uchar>(data[i]));
snes_color mColor = ConvertSNEStoRGB(col.snes()); snes_color mColor = ConvertSnesToRgb(col.snes());
col.set_rgb(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f)); col.set_rgb(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
colors.push_back(col); colors.push_back(col);
} }
@@ -241,7 +241,7 @@ SnesPalette::SnesPalette(const unsigned char *snes_pal) {
SnesColor col; SnesColor col;
col.set_snes(snes_pal[i + 1] << (uint16_t)8); col.set_snes(snes_pal[i + 1] << (uint16_t)8);
col.set_snes(col.snes() | snes_pal[i]); col.set_snes(col.snes() | snes_pal[i]);
snes_color mColor = ConvertSNEStoRGB(col.snes()); snes_color mColor = ConvertSnesToRgb(col.snes());
col.set_rgb(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f)); col.set_rgb(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
colors.push_back(col); colors.push_back(col);
} }
@@ -258,7 +258,7 @@ SnesPalette::SnesPalette(const std::vector<ImVec4> &cols) {
SnesPalette::SnesPalette(const std::vector<snes_color> &cols) { SnesPalette::SnesPalette(const std::vector<snes_color> &cols) {
for (const auto &each : cols) { for (const auto &each : cols) {
SnesColor scol; SnesColor scol;
scol.set_snes(ConvertRGBtoSNES(each)); scol.set_snes(ConvertRgbToSnes(each));
colors.push_back(scol); colors.push_back(scol);
} }
} }
@@ -279,7 +279,7 @@ SnesPalette ReadPaletteFromRom(int offset, int num_colors, const uint8_t *rom) {
new_color.red = (color & 0x1F) * 8; new_color.red = (color & 0x1F) * 8;
new_color.green = ((color >> 5) & 0x1F) * 8; new_color.green = ((color >> 5) & 0x1F) * 8;
new_color.blue = ((color >> 10) & 0x1F) * 8; new_color.blue = ((color >> 10) & 0x1F) * 8;
colors[color_offset].set_snes(ConvertRGBtoSNES(new_color)); colors[color_offset].set_snes(ConvertRgbToSnes(new_color));
if (color_offset == 0) { if (color_offset == 0) {
colors[color_offset].set_transparent(true); colors[color_offset].set_transparent(true);
} }

View File

@@ -48,7 +48,7 @@ IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor* color,
ImGui::ColorEdit4(label.data(), (float*)&displayColor.x, flags); ImGui::ColorEdit4(label.data(), (float*)&displayColor.x, flags);
color->set_rgb(displayColor); color->set_rgb(displayColor);
color->set_snes(gfx::ConvertRGBtoSNES(displayColor)); color->set_snes(gfx::ConvertRgbToSnes(displayColor));
return pressed; return pressed;
} }

View File

@@ -10,8 +10,8 @@ namespace test {
namespace gfx { namespace gfx {
using ::testing::ElementsAreArray; using ::testing::ElementsAreArray;
using yaze::app::gfx::ConvertRGBtoSNES; using yaze::app::gfx::ConvertRgbToSnes;
using yaze::app::gfx::ConvertSNEStoRGB; using yaze::app::gfx::ConvertSnestoRGB;
using yaze::app::gfx::Extract; using yaze::app::gfx::Extract;
using yaze::app::gfx::SnesPalette; using yaze::app::gfx::SnesPalette;
@@ -25,45 +25,45 @@ unsigned int test_convert(snes_color col) {
} }
} // namespace } // namespace
TEST(SNESPaletteTest, AddColor) { TEST(SnesPaletteTest, AddColor) {
yaze::app::gfx::SnesPalette palette; yaze::app::gfx::SnesPalette palette;
yaze::app::gfx::SnesColor color; yaze::app::gfx::SnesColor color;
palette.AddColor(color); palette.AddColor(color);
ASSERT_EQ(palette.size(), 1); ASSERT_EQ(palette.size(), 1);
} }
TEST(SNESColorTest, ConvertRGBtoSNES) { TEST(SnesColorTest, ConvertRgbToSnes) {
snes_color color = {132, 132, 132}; snes_color color = {132, 132, 132};
uint16_t snes = ConvertRGBtoSNES(color); uint16_t snes = ConvertRgbToSnes(color);
ASSERT_EQ(snes, 0x4210); ASSERT_EQ(snes, 0x4210);
} }
TEST(SNESColorTest, ConvertSNEStoRGB) { TEST(SnesColorTest, ConvertSnestoRGB) {
uint16_t snes = 0x4210; uint16_t snes = 0x4210;
snes_color color = ConvertSNEStoRGB(snes); snes_color color = ConvertSnestoRGB(snes);
ASSERT_EQ(color.red, 132); ASSERT_EQ(color.red, 132);
ASSERT_EQ(color.green, 132); ASSERT_EQ(color.green, 132);
ASSERT_EQ(color.blue, 132); ASSERT_EQ(color.blue, 132);
} }
TEST(SNESColorTest, ConvertSNESToRGB_Binary) { TEST(SnesColorTest, ConvertSnesToRGB_Binary) {
uint16_t red = 0b0000000000011111; uint16_t red = 0b0000000000011111;
uint16_t blue = 0b0111110000000000; uint16_t blue = 0b0111110000000000;
uint16_t green = 0b0000001111100000; uint16_t green = 0b0000001111100000;
uint16_t purple = 0b0111110000011111; uint16_t purple = 0b0111110000011111;
snes_color testcolor; snes_color testcolor;
testcolor = ConvertSNEStoRGB(red); testcolor = ConvertSnestoRGB(red);
ASSERT_EQ(0xFF0000, test_convert(testcolor)); ASSERT_EQ(0xFF0000, test_convert(testcolor));
testcolor = ConvertSNEStoRGB(green); testcolor = ConvertSnestoRGB(green);
ASSERT_EQ(0x00FF00, test_convert(testcolor)); ASSERT_EQ(0x00FF00, test_convert(testcolor));
testcolor = ConvertSNEStoRGB(blue); testcolor = ConvertSnestoRGB(blue);
ASSERT_EQ(0x0000FF, test_convert(testcolor)); ASSERT_EQ(0x0000FF, test_convert(testcolor));
testcolor = ConvertSNEStoRGB(purple); testcolor = ConvertSnestoRGB(purple);
ASSERT_EQ(0xFF00FF, test_convert(testcolor)); ASSERT_EQ(0xFF00FF, test_convert(testcolor));
} }
TEST(SNESColorTest, Extraction) { TEST(SnesColorTest, Extraction) {
// red, blue, green, purple // red, blue, green, purple
char data[8] = {0x1F, 0x00, 0x00, 0x7C, static_cast<char>(0xE0), char data[8] = {0x1F, 0x00, 0x00, 0x7C, static_cast<char>(0xE0),
0x03, 0x1F, 0x7C}; 0x03, 0x1F, 0x7C};
@@ -75,7 +75,7 @@ TEST(SNESColorTest, Extraction) {
ASSERT_EQ(0xFF00FF, test_convert(pal[3])); ASSERT_EQ(0xFF00FF, test_convert(pal[3]));
} }
TEST(SNESColorTest, Convert) { TEST(SnesColorTest, Convert) {
// red, blue, green, purple white // red, blue, green, purple white
char data[10] = {0x1F, char data[10] = {0x1F,
0x00, 0x00,