Skip to content

Commit

Permalink
Added ColorConverter interface and RgbaToBgr impl
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Nov 4, 2024
1 parent 3d5898d commit 5c3dc92
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 89 deletions.
5 changes: 3 additions & 2 deletions Porytiles-2.x/include/porytiles/Color/Bgr15.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace porytiles::color {
* and red respectively. Internally, a Bgr15 is representing using a 16-bit integer, with the top
* bit left unused. Bgr15 is the preferred color format for the Game Boy Advance's palette RAM. The
* BGR format has no concept of transparency. Since all Colors must provide an implementation of
* @link Color::computeAlphaComponent computeAlphaComponent @endlink, Bgr15 simply returns an opaque value.
* @link Color::computeAlphaComponent computeAlphaComponent @endlink, Bgr15 simply returns an opaque
* value.
*/
class Bgr15 : public Color {
std::uint16_t bgr;
Expand Down Expand Up @@ -101,7 +102,7 @@ class Bgr15 : public Color {
*/
[[nodiscard]] std::uint8_t computeAlphaComponent() const override
{
return Rgba32::ALPHA_OPAQUE;
return ALPHA_OPAQUE;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Porytiles-2.x/include/porytiles/Color/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define PORYTILES_COLOR_COLOR_H

#include <cstdint>
#include <iostream>
#include <string>

namespace porytiles::color {

Expand Down
20 changes: 20 additions & 0 deletions Porytiles-2.x/include/porytiles/Color/ColorConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PORYTILES_COLOR_COLOR_CONVERTER_H
#define PORYTILES_COLOR_COLOR_CONVERTER_H

#include <porytiles/Color/Color.h>

namespace porytiles::color {

/**
* @brief Service interface for a converter service that transforms one Color implementation into a
* different Color implementation.
*/
class ColorConverter {
public:
virtual ~ColorConverter() = default;
[[nodiscard]] virtual std::unique_ptr<Color> convert(const Color &input) const = 0;
};

} // namespace porytiles::color

#endif // PORYTILES_COLOR_COLOR_CONVERTER_H
27 changes: 0 additions & 27 deletions Porytiles-2.x/include/porytiles/Color/ColorConverters.h

This file was deleted.

2 changes: 1 addition & 1 deletion Porytiles-2.x/include/porytiles/Color/Palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Palette {
[[nodiscard]] virtual std::size_t getCapacity() const = 0;
[[nodiscard]] virtual std::size_t getSize() const = 0;
virtual void pushColor(const Color &color) = 0;
[[nodiscard]] virtual const Color& colorAt(std::size_t index) const = 0;
[[nodiscard]] virtual const Color &colorAt(std::size_t index) const = 0;
};

} // namespace porytiles::color
Expand Down
2 changes: 1 addition & 1 deletion Porytiles-2.x/include/porytiles/Color/Rgba32Palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Rgba32Palette : public Palette {
[[nodiscard]] std::size_t getCapacity() const override;
[[nodiscard]] std::size_t getSize() const override;
void pushColor(const Color &color) override;
[[nodiscard]] const Color& colorAt(std::size_t index) const override;
[[nodiscard]] const Color &colorAt(std::size_t index) const override;
};

} // namespace porytiles::color
Expand Down
20 changes: 20 additions & 0 deletions Porytiles-2.x/include/porytiles/Color/RgbaToBgr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PORYTILES_COLOR_RGBA_TO_BGR_H
#define PORYTILES_COLOR_RGBA_TO_BGR_H

#include <porytiles/Color/Color.h>
#include <porytiles/Color/ColorConverter.h>

namespace porytiles::color {

/**
* @brief Service interface for a converter service that transforms one Color implementation into a
* different Color implementation.
*/
class RgbaToBgr : public ColorConverter {
public:
[[nodiscard]] std::unique_ptr<Color> convert(const Color &input) const override;
};

} // namespace porytiles::color

#endif // PORYTILES_COLOR_RGBA_TO_BGR_H
22 changes: 0 additions & 22 deletions Porytiles-2.x/lib/Color/ColorConverters.cpp

This file was deleted.

14 changes: 14 additions & 0 deletions Porytiles-2.x/lib/Color/RgbaToBgr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "porytiles/Color/RgbaToBgr.h"

#include <porytiles/Color/Bgr15.h>
#include <porytiles/Color/Rgba32.h>

using namespace porytiles::color;

std::unique_ptr<Color> RgbaToBgr::convert(const Color &input) const
{
const auto &rgb = dynamic_cast<const Rgba32 &>(input);
const Bgr15 bgr{rgb.computeRedComponent(), rgb.computeGreenComponent(),
rgb.computeBlueComponent()};
return std::make_unique<Bgr15>(bgr);
}
13 changes: 13 additions & 0 deletions Porytiles-2.x/tests/Color/Bgr15Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,16 @@ TEST(Bgr15Test, TestComponentCtor)
const Bgr15 bgr{255, 255, 255};
EXPECT_EQ(bgr.getRawValue(), TWO_FIFTEENTH_POW_MINUS_ONE);
}

TEST(Bgr15Test, TestComponentGetters)
{
using namespace porytiles::color;

const Bgr15 bgr{255, 255, 255};
// expected component value is 248 due to precision loss
constexpr std::uint8_t expectedComponentValue = 248;
EXPECT_EQ(bgr.computeRedComponent(), expectedComponentValue);
EXPECT_EQ(bgr.computeGreenComponent(), expectedComponentValue);
EXPECT_EQ(bgr.computeBlueComponent(), expectedComponentValue);
EXPECT_EQ(bgr.computeAlphaComponent(), Color::ALPHA_OPAQUE);
}
35 changes: 0 additions & 35 deletions Porytiles-2.x/tests/Color/ColorConvertersTest.cpp

This file was deleted.

22 changes: 22 additions & 0 deletions Porytiles-2.x/tests/Color/RgbaToBgrTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <gtest/gtest.h>

#include <porytiles/Color/Bgr15.h>
#include <porytiles/Color/Rgba32.h>
#include <porytiles/Color/RgbaToBgr.h>

TEST(RgbaToBgrTest, RgbaToBgr)
{
using namespace porytiles::color;

const RgbaToBgr rgbaToBgr{};

const Rgba32 redRgba1{255, 0, 0};
const Bgr15 redBgr1{31};
const auto convertedBgr1 = rgbaToBgr.convert(redRgba1);
EXPECT_EQ(dynamic_cast<const Bgr15 &>(*convertedBgr1), redBgr1);

const Rgba32 redRgba2{250, 0, 0};
const auto convertedBgr2 = rgbaToBgr.convert(redRgba2);
// should still match redBgr1 due to precision loss between formats
EXPECT_EQ(dynamic_cast<const Bgr15 &>(*convertedBgr2), redBgr1);
}

0 comments on commit 5c3dc92

Please sign in to comment.