diff --git a/Porytiles-2.x/include/porytiles/Color/Bgr15.h b/Porytiles-2.x/include/porytiles/Color/Bgr15.h index 263fa35..57c9084 100644 --- a/Porytiles-2.x/include/porytiles/Color/Bgr15.h +++ b/Porytiles-2.x/include/porytiles/Color/Bgr15.h @@ -3,8 +3,9 @@ #include #include +#include -#include "porytiles/Color/Rgba32.h" +#include namespace porytiles::color { @@ -12,9 +13,13 @@ namespace porytiles::color { * @brief Value object representing a color in 15-bit BGR format. * * @details - * TODO 2.x : fill in explanation about BGR format, 5 bits per color, top bit unused, etc. + * The 15-bit BGR format represents a color with three 5-bit channels: one channel for blue, green, + * 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, so Bgr15's @link Bgr15::computeAlphaComponent + * computeAlphaComponent @endlink implementation simply returns @link ALPHA_OPAQUE @endlink. */ -class Bgr15 : public Color { +class Bgr15 { std::uint16_t bgr; public: @@ -62,7 +67,7 @@ class Bgr15 : public Color { * * @return The 8-bit red component derived from the 15-bit BGR value. */ - [[nodiscard]] std::uint8_t computeRedComponent() const override; + [[nodiscard]] std::uint8_t computeRedComponent() const; /** * @brief Computes the green component from the 15-bit BGR color value. @@ -73,7 +78,7 @@ class Bgr15 : public Color { * * @return The 8-bit green component derived from the 15-bit BGR value. */ - [[nodiscard]] std::uint8_t computeGreenComponent() const override; + [[nodiscard]] std::uint8_t computeGreenComponent() const; /** * @brief Computes the blue component from the 15-bit BGR color value. @@ -84,7 +89,7 @@ class Bgr15 : public Color { * * @return The 8-bit blue component derived from the 15-bit BGR value. */ - [[nodiscard]] std::uint8_t computeBlueComponent() const override; + [[nodiscard]] std::uint8_t computeBlueComponent() const; /** * @brief Computes the alpha component, which will always be opaque. @@ -95,17 +100,58 @@ class Bgr15 : public Color { * * @return The opaque alpha component. */ - [[nodiscard]] std::uint8_t computeAlphaComponent() const override + [[nodiscard]] std::uint8_t computeAlphaComponent() const { - return Rgba32::ALPHA_OPAQUE; + return ALPHA_OPAQUE; } + /** + * @brief Converts the Color to a JASC-PAL formatted string. + * + * @details + * This method converts the red, green, and blue components of the Color object into a + * space-separated string format commonly used in JASC-PAL color palette files. JASC-PAL files + * typically do not include an alpha channel value, so that value is omitted from the string + * returned here. + * + * @return A string representation of the color in JASC-PAL format. + */ + [[nodiscard]] std::string toJascString() const + { + return std::to_string(computeRedComponent()) + " " + + std::to_string(computeGreenComponent()) + " " + + std::to_string(computeBlueComponent()); + } + + /** + * @brief Equality operator for Bgr15 objects. + * + * @details + * Compares two Bgr15 objects to determine whether their 15-bit BGR values are equal. The + * comparison uses the raw value but shifts off the top bit, since this bit is unused in BGR15 + * format. Garbage bit values here should not affect logical equality. + * + * @param lhs The left-hand side Bgr15 object to compare. + * @param rhs The right-hand side Bgr15 object to compare. + * @return True if the 15-bit BGR values of both Bgr15 objects are equal, false otherwise. + */ friend bool operator==(const Bgr15 &lhs, const Bgr15 &rhs) { - // TODO 2.x : make this ignore the top bit of the raw value, since that bit is unused - return lhs.getRawValue() == rhs.getRawValue(); + return lhs.getRawValue() << 1 == rhs.getRawValue() << 1; } + /** + * @brief Inequality operator for Bgr15 objects. + * + * @details + * Compares two Bgr15 objects to determine whether their 15-bit BGR values are not equal. + * Currently, it compares the raw 16-bit values, but it should be updated to ignore the top + * unused bit. + * + * @param lhs The left-hand side Bgr15 object to compare. + * @param rhs The right-hand side Bgr15 object to compare. + * @return True if the 15-bit BGR values of both Bgr15 objects are not equal, false otherwise. + */ friend bool operator!=(const Bgr15 &lhs, const Bgr15 &rhs) { return !(lhs == rhs); diff --git a/Porytiles-2.x/include/porytiles/Color/Color.h b/Porytiles-2.x/include/porytiles/Color/Color.h index cfd54c3..342efdf 100644 --- a/Porytiles-2.x/include/porytiles/Color/Color.h +++ b/Porytiles-2.x/include/porytiles/Color/Color.h @@ -2,7 +2,7 @@ #define PORYTILES_COLOR_COLOR_H #include -#include +#include namespace porytiles::color { @@ -18,6 +18,16 @@ namespace porytiles::color { */ class Color { public: + /** + * @brief Completely transparent alpha channel value. + */ + static constexpr std::uint8_t ALPHA_TRANSPARENT = 0; + + /** + * @brief Completely opaque alpha channel value. + */ + static constexpr std::uint8_t ALPHA_OPAQUE = 0xff; + virtual ~Color() = default; /** @@ -48,21 +58,6 @@ class Color { */ [[nodiscard]] virtual std::uint8_t computeAlphaComponent() const = 0; - /** - * @brief Converts the Color to a JASC-PAL formatted string. - * - * @details - * This method converts the red, green, and blue components of the Color object into a - * space-separated string format commonly used in JASC-PAL color palette files. - * - * @return A string representation of the color in JASC-PAL format. - */ - [[nodiscard]] virtual std::string toJascString() const - { - return std::to_string(computeRedComponent()) + " " + - std::to_string(computeGreenComponent()) + " " + - std::to_string(computeBlueComponent()); - } /** * @brief Compares two Color objects for equality. diff --git a/Porytiles-2.x/include/porytiles/Color/ColorConverter.h b/Porytiles-2.x/include/porytiles/Color/ColorConverter.h new file mode 100644 index 0000000..0a34354 --- /dev/null +++ b/Porytiles-2.x/include/porytiles/Color/ColorConverter.h @@ -0,0 +1,21 @@ +#ifndef PORYTILES_COLOR_COLOR_CONVERTER_H +#define PORYTILES_COLOR_COLOR_CONVERTER_H + +#include + +namespace porytiles::color { + +/** + * @brief Service interface for a converter service that transforms one Color implementation into a + * different Color implementation. + */ +template +class ColorConverter { + public: + virtual ~ColorConverter() = default; + [[nodiscard]] virtual U convert(const T&) const = 0; +}; + +} // namespace porytiles::color + +#endif // PORYTILES_COLOR_COLOR_CONVERTER_H \ No newline at end of file diff --git a/Porytiles-2.x/include/porytiles/Color/ColorConverters.h b/Porytiles-2.x/include/porytiles/Color/ColorConverters.h deleted file mode 100644 index 1dec011..0000000 --- a/Porytiles-2.x/include/porytiles/Color/ColorConverters.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef PORYTILES_COLOR_COLOR_CONVERTERS_H -#define PORYTILES_COLOR_COLOR_CONVERTERS_H - -#include "porytiles/Color/Bgr15.h" -#include "porytiles/Color/Rgba32.h" - -namespace porytiles::color { - -/** - * @brief Converts an Rgba32 color to a 15-bit Bgr15 color. - * - * @param rgba The Rgba32 color object to convert. - * @return The converted Bgr15 color. - */ -Bgr15 rgbaToBgr(const Rgba32 &rgba); - -/** - * @brief Converts a Bgr15 color to an Rgba32 color. - * - * @param bgr The Bgr15 color object to convert. - * @return The converted Rgba32 color. - */ -Rgba32 bgrToRgba(const Bgr15 &bgr); - -} // namespace porytiles::color - -#endif // PORYTILES_COLOR_COLOR_CONVERTERS_H diff --git a/Porytiles-2.x/include/porytiles/Color/NamespaceDoc.h b/Porytiles-2.x/include/porytiles/Color/NamespaceDoc.h deleted file mode 100644 index 8834c3f..0000000 --- a/Porytiles-2.x/include/porytiles/Color/NamespaceDoc.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef PORYTILES_COLOR_NAMESPACE_H -#define PORYTILES_COLOR_NAMESPACE_H - -/** - * @brief Color related code. - */ -namespace porytiles::color { -} - -#endif //PORYTILES_COLOR_NAMESPACE_H diff --git a/Porytiles-2.x/include/porytiles/Color/Palette.h b/Porytiles-2.x/include/porytiles/Color/Palette.h new file mode 100644 index 0000000..9ab97bf --- /dev/null +++ b/Porytiles-2.x/include/porytiles/Color/Palette.h @@ -0,0 +1,48 @@ +#ifndef PORYTILES_COLOR_PALETTE_H +#define PORYTILES_COLOR_PALETTE_H + +#include + +#include +#include + +namespace porytiles::color { + +template +class Palette { + std::vector colors; + + public: + explicit Palette(std::size_t capacity) { + colors.reserve(capacity); + } + + [[nodiscard]] std::size_t getCapacity() const { + return colors.capacity(); + } + + [[nodiscard]] std::size_t getSize() const { + return colors.size(); + } + + const T& at(std::size_t index) const { + return colors.at(index); + } + + void pushColor(const T& color) { + if (getSize() >= getCapacity()) { + throw std::out_of_range("porytiles::color::Palette::pushColor"); + } + colors.push_back(color); + } + + template + void foo(const ColorConverter& converter, U color) { + auto myColor = converter.convert(color); + std::cout << ":" << myColor.toJascString() << std::endl; + } +}; + +} // namespace porytiles::color + +#endif // PORYTILES_COLOR_PALETTE_H \ No newline at end of file diff --git a/Porytiles-2.x/include/porytiles/Color/RGBLike.h b/Porytiles-2.x/include/porytiles/Color/RGBLike.h new file mode 100644 index 0000000..1a37c6d --- /dev/null +++ b/Porytiles-2.x/include/porytiles/Color/RGBLike.h @@ -0,0 +1,51 @@ +#ifndef PORYTILES_COLOR_RGBLIKE_H +#define PORYTILES_COLOR_RGBLIKE_H + +#include +#include +#include + +namespace porytiles::color { + +/** + * @brief An RGBLike should match any type which can behave like an RGBA format color. + * + * @details + * The Porytiles library is color implementation agnostic. That is, it only needs a color to conform + * to this simple concept. The underlying color implementation is irrelevant, as most color + * representations can be converted into red, green, and blue channels via a simple formula. + * Porytiles provides a few RGBLike compliant implementations that may be useful, but users of the + * Porytiles library may also implement their own if they have a specific use-case. + */ +template +concept RGBLike = requires(T t) { + { + t.computeRedComponent() + } -> std::same_as; + { + t.computeGreenComponent() + } -> std::same_as; + { + t.computeBlueComponent() + } -> std::same_as; + { + t.computeAlphaComponent() + } -> std::same_as; + { + t.toJascString() + } -> std::same_as; +}; + +/** + * @brief Completely transparent alpha channel value. + */ +constexpr std::uint8_t ALPHA_TRANSPARENT = 0; + +/** + * @brief Completely opaque alpha channel value. + */ +constexpr std::uint8_t ALPHA_OPAQUE = 0xff; + +} // namespace porytiles::color + +#endif // PORYTILES_COLOR_RGBLIKE_H \ No newline at end of file diff --git a/Porytiles-2.x/include/porytiles/Color/Rgba32.h b/Porytiles-2.x/include/porytiles/Color/Rgba32.h index 649b5b8..ea88b16 100644 --- a/Porytiles-2.x/include/porytiles/Color/Rgba32.h +++ b/Porytiles-2.x/include/porytiles/Color/Rgba32.h @@ -4,7 +4,7 @@ #include #include -#include "porytiles/Color/Color.h" +#include namespace porytiles::color { @@ -12,18 +12,19 @@ namespace porytiles::color { * @brief Value object representing a color in 32-bit RGBA format. * * @details - * TODO 2.x : fill in explanation about RGBA format, 8 bits per color, alpha channel, etc + * The 32-bit RGBA format represents an RGBLike with four 8-bit channels: one channel for red, + * blue, and green respectively. The last 8-bit channel, the alpha channel, represents color + * transparency. For the purposes of Porytiles, the only relevant alpha channel values are 0 + * (completely transparent) and 255 (completely opaque). You can read more about the RGBA color + * format here: RGBA Color Model */ -class Rgba32 : public Color { +class Rgba32 { std::uint8_t red; std::uint8_t green; std::uint8_t blue; std::uint8_t alpha; public: - static constexpr std::uint8_t ALPHA_TRANSPARENT = 0; - static constexpr std::uint8_t ALPHA_OPAQUE = 0xff; - /** * @brief Default constructor for Rgba32. Initializes the color and alpha components to zero. */ @@ -46,7 +47,7 @@ class Rgba32 : public Color { /** * @brief Constructs an Rgba32 object with specified RGB values and an implicit alpha value of - * 255 (fully opaque). + * 255 (completely opaque). * * @param red The red component of the color. * @param green The green component of the color. @@ -63,7 +64,7 @@ class Rgba32 : public Color { * * @return The internal 8-bit red component. */ - [[nodiscard]] std::uint8_t computeRedComponent() const override + [[nodiscard]] std::uint8_t computeRedComponent() const { return red; } @@ -73,7 +74,7 @@ class Rgba32 : public Color { * * @return The internal 8-bit green component. */ - [[nodiscard]] std::uint8_t computeGreenComponent() const override + [[nodiscard]] std::uint8_t computeGreenComponent() const { return green; } @@ -83,7 +84,7 @@ class Rgba32 : public Color { * * @return The internal 8-bit blue component. */ - [[nodiscard]] std::uint8_t computeBlueComponent() const override + [[nodiscard]] std::uint8_t computeBlueComponent() const { return blue; } @@ -93,23 +94,108 @@ class Rgba32 : public Color { * * @return The internal 8-bit alpha component. */ - [[nodiscard]] std::uint8_t computeAlphaComponent() const override + [[nodiscard]] std::uint8_t computeAlphaComponent() const { return alpha; } + /** + * @brief Converts the Color to a JASC-PAL formatted string. + * + * @details + * This method converts the red, green, and blue components of the Color object into a + * space-separated string format commonly used in JASC-PAL color palette files. JASC-PAL files + * typically do not include an alpha channel value, so that value is omitted from the string + * returned here. + * + * @return A string representation of the color in JASC-PAL format. + */ + [[nodiscard]] std::string toJascString() const + { + return std::to_string(computeRedComponent()) + " " + + std::to_string(computeGreenComponent()) + " " + + std::to_string(computeBlueComponent()); + } + + /** + * @brief Equality operator for Rgba32 objects. + * + * @details + * Rgba32 equality is computed component-wise. If each component is equal (including alpha + * channel), then the two Rgba32 colors are equal. + * + * @param lhs The left-hand side Rgba32 object. + * @param rhs The right-hand side Rgba32 object. + * @return True if the RGBA components of the two Rgba32 objects are equal, false otherwise. + */ friend bool operator==(const Rgba32 &lhs, const Rgba32 &rhs) { return lhs.red == rhs.red && lhs.green == rhs.green && lhs.blue == rhs.blue && lhs.alpha == rhs.alpha; } + /** + * @brief Inequality operator for Rgba32 objects. + * + * @details + * Rgba32 inequality is computed component-wise. If any component is not equal (including alpha + * channel), then the two Rgba32 colors are not equal. + * + * @param lhs The left-hand side Rgba32 object. + * @param rhs The right-hand side Rgba32 object. + * @return True if the RGBA components of the two Rgba32 objects are not equal, false otherwise. + */ friend bool operator!=(const Rgba32 &lhs, const Rgba32 &rhs) { return !(lhs == rhs); } }; +/** + * @brief Completely transparent preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_TRANSPARENT{0, 0, 0, ALPHA_TRANSPARENT}; + +/** + * @brief Black preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_BLACK{0, 0, 0}; + +/** + * @brief Red preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_RED{255, 0, 0}; + +/** + * @brief Green preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_GREEN{0, 255, 0}; + +/** + * @brief Blue preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_BLUE{0, 0, 255}; + +/** + * @brief Yellow preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_YELLOW{255, 255, 0}; + +/** + * @brief Cyan preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_CYAN{0, 255, 255}; + +/** + * @brief Magenta preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_MAGENTA{255, 0, 255}; + +/** + * @brief White preinitialized Rgba32. + */ +static constexpr Rgba32 RGBA_WHITE{255, 255, 255}; + } // namespace porytiles::color #endif // PORYTILES_COLOR_RGBA32_H diff --git a/Porytiles-2.x/include/porytiles/Color/RgbaToBgr.h b/Porytiles-2.x/include/porytiles/Color/RgbaToBgr.h new file mode 100644 index 0000000..97534c4 --- /dev/null +++ b/Porytiles-2.x/include/porytiles/Color/RgbaToBgr.h @@ -0,0 +1,30 @@ +#ifndef PORYTILES_COLOR_RGBA_TO_BGR_H +#define PORYTILES_COLOR_RGBA_TO_BGR_H + +#include +#include +#include + +namespace porytiles::color { + +/** + * @brief A ColorConverter implementation that converts an Rgba32 into a Bgr15. + */ +class RgbaToBgr final : public ColorConverter { + public: + /** + * @brief Converts an Rgba32 color to a Bgr15 color format. + * + * @details + * This method takes an Rgba32 color object, extracts its red, green, and + * blue components, and converts it to a Bgr15 color object. + * + * @param rgba A constant reference to an Rgba32 color object. + * @return An equivalent Bgr15 color object. + */ + [[nodiscard]] Bgr15 convert(const Rgba32 &rgba) const override; +}; + +} // namespace porytiles::color + +#endif // PORYTILES_COLOR_RGBA_TO_BGR_H \ No newline at end of file diff --git a/Porytiles-2.x/include/porytiles/Color/_namespace.h b/Porytiles-2.x/include/porytiles/Color/_namespace.h new file mode 100644 index 0000000..045ec43 --- /dev/null +++ b/Porytiles-2.x/include/porytiles/Color/_namespace.h @@ -0,0 +1,23 @@ +#ifndef PORYTILES_COLOR_NAMESPACE_H +#define PORYTILES_COLOR_NAMESPACE_H + +/** + * @brief Porytiles color types and color-related types. + * + * @details + * The core Porytiles color representation can be found in the Color interface. Porytiles also + * provides some basic Color implementations: Rgba32 and Bgr15. Library users may provide their own + * for custom use-cases. + * + * Colors can be stored in an aggregate object called a Palette. The Palette class provides a + * minimum possible interface for the palette concept. The Porytiles compiler is designed to work + * with any palette (and thus any color representation) that conforms to this interface. + * + * Each Color implementation must provide its own Palette implementation, since different color + * formats might have special implementation considerations. The Porytiles library includes palette + * implementations for Rgba32 and Bgr15, namely Rgba32Palette and Bgr15Palette. + */ +namespace porytiles::color { +} + +#endif // PORYTILES_COLOR_NAMESPACE_H diff --git a/Porytiles-2.x/include/porytiles/Tile/NamespaceDoc.h b/Porytiles-2.x/include/porytiles/Tile/_namespace.h similarity index 78% rename from Porytiles-2.x/include/porytiles/Tile/NamespaceDoc.h rename to Porytiles-2.x/include/porytiles/Tile/_namespace.h index dacd706..3f5ce6f 100644 --- a/Porytiles-2.x/include/porytiles/Tile/NamespaceDoc.h +++ b/Porytiles-2.x/include/porytiles/Tile/_namespace.h @@ -7,4 +7,4 @@ namespace porytiles::tile { } -#endif //PORYTILES_TILE_NAMESPACE_H +#endif // PORYTILES_TILE_NAMESPACE_H diff --git a/Porytiles-2.x/lib/Color/ColorConverters.cpp b/Porytiles-2.x/lib/Color/ColorConverters.cpp deleted file mode 100644 index 2a70e3e..0000000 --- a/Porytiles-2.x/lib/Color/ColorConverters.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "porytiles/Color/ColorConverters.h" - -#include "porytiles/Color/Bgr15.h" -#include "porytiles/Color/Rgba32.h" - -namespace porytiles::color { -Bgr15 rgbaToBgr(const Rgba32 &rgba) -{ - /* - * Convert each color channel from 8-bit to 5-bit via a right shift of 3, then shift back into - * the correct position. - */ - return Bgr15{static_cast(rgba.computeBlueComponent() >> 3 << 10 | - rgba.computeGreenComponent() >> 3 << 5 | - rgba.computeRedComponent() >> 3)}; -} - -Rgba32 bgrToRgba(const Bgr15 &bgr) -{ - return Rgba32{bgr.computeRedComponent(), bgr.computeGreenComponent(), bgr.computeBlueComponent()}; -} -} \ No newline at end of file diff --git a/Porytiles-2.x/lib/Color/Rgba32.cpp b/Porytiles-2.x/lib/Color/Rgba32.cpp index e1bbc7e..5ac9279 100644 --- a/Porytiles-2.x/lib/Color/Rgba32.cpp +++ b/Porytiles-2.x/lib/Color/Rgba32.cpp @@ -1,5 +1,3 @@ #include "porytiles/Color/Rgba32.h" -#include - using namespace porytiles::color; diff --git a/Porytiles-2.x/lib/Color/RgbaToBgr.cpp b/Porytiles-2.x/lib/Color/RgbaToBgr.cpp new file mode 100644 index 0000000..abef254 --- /dev/null +++ b/Porytiles-2.x/lib/Color/RgbaToBgr.cpp @@ -0,0 +1,14 @@ +#include "porytiles/Color/RgbaToBgr.h" + +#include +#include + +using namespace porytiles::color; + +Bgr15 RgbaToBgr::convert(const Rgba32 &rgba) const +{ + const auto &rgb = dynamic_cast(rgba); + const Bgr15 bgr{rgb.computeRedComponent(), rgb.computeGreenComponent(), + rgb.computeBlueComponent()}; + return bgr; +} \ No newline at end of file diff --git a/Porytiles-2.x/tests/Color/Bgr15Test.cpp b/Porytiles-2.x/tests/Color/Bgr15Test.cpp index bc6e5c8..185db29 100644 --- a/Porytiles-2.x/tests/Color/Bgr15Test.cpp +++ b/Porytiles-2.x/tests/Color/Bgr15Test.cpp @@ -1,5 +1,6 @@ #include +#include "porytiles/Color/RGBLike.h" #include "porytiles/Color/Bgr15.h" // Just a fun little example of template metaprogramming @@ -16,7 +17,7 @@ TEST(Bgr15Test, TestDefaultCtor) { using namespace porytiles::color; - const Bgr15 bgr{}; + constexpr Bgr15 bgr{}; EXPECT_EQ(bgr.getRawValue(), 0); } @@ -27,3 +28,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(), ALPHA_OPAQUE); +} diff --git a/Porytiles-2.x/tests/Color/ColorConvertersTest.cpp b/Porytiles-2.x/tests/Color/ColorConvertersTest.cpp deleted file mode 100644 index e55da35..0000000 --- a/Porytiles-2.x/tests/Color/ColorConvertersTest.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include - -#include - -#include "porytiles/Color/Bgr15.h" -#include "porytiles/Color/ColorConverters.h" -#include "porytiles/Color/Rgba32.h" - -TEST(ColorConvertersTest, RgbaToBgr) -{ - using namespace porytiles::color; - - const Rgba32 redRgba1{255, 0, 0}; - const Bgr15 redBgr1{31}; - const Bgr15 redBgr2{255, 0, 0}; - - EXPECT_EQ(rgbaToBgr(redRgba1), redBgr1); - EXPECT_EQ(rgbaToBgr(redRgba1), redBgr2); - - const Rgba32 yellowRgba1{255, 255, 0}; - const Bgr15 yellowBgr1{1023}; - const Bgr15 yellowBgr2{255, 255, 0}; - EXPECT_EQ(rgbaToBgr(yellowRgba1), yellowBgr1); - EXPECT_EQ(rgbaToBgr(yellowRgba1), yellowBgr2); -} - -TEST(ColorConvertersTest, BgrToRgba) -{ - using namespace porytiles::color; - - const Bgr15 blueBgr1{0, 0, 255}; - const Rgba32 blueRgba1{0, 0, 248}; - - EXPECT_EQ(bgrToRgba(blueBgr1), blueRgba1); -} diff --git a/Porytiles-2.x/tests/Color/PaletteTest.cpp b/Porytiles-2.x/tests/Color/PaletteTest.cpp new file mode 100644 index 0000000..199219a --- /dev/null +++ b/Porytiles-2.x/tests/Color/PaletteTest.cpp @@ -0,0 +1,15 @@ +#include + +#include +#include +#include +#include + +TEST(PaletteTest, Foo) +{ + using namespace porytiles::color; + + Palette palette{10}; + RgbaToBgr rgbaToBgr{}; + palette.foo(rgbaToBgr, RGBA_RED); +} diff --git a/Porytiles-2.x/tests/Color/Rgba32Test.cpp b/Porytiles-2.x/tests/Color/Rgba32Test.cpp index 9afe030..44f496b 100644 --- a/Porytiles-2.x/tests/Color/Rgba32Test.cpp +++ b/Porytiles-2.x/tests/Color/Rgba32Test.cpp @@ -1,20 +1,21 @@ #include -#include "porytiles/Color/Rgba32.h" +#include +#include TEST(Rgba32Test, TestJascString) { using namespace porytiles::color; - const Rgba32 transparentRgba{}; + constexpr Rgba32 transparentRgba{}; EXPECT_EQ(transparentRgba.toJascString(), "0 0 0"); - const Rgba32 red{255, 0, 0}; + constexpr Rgba32 red{255, 0, 0}; EXPECT_EQ(red.toJascString(), "255 0 0"); - const Rgba32 green{0, 255, 0, Rgba32::ALPHA_OPAQUE}; + constexpr Rgba32 green{0, 255, 0, ALPHA_OPAQUE}; EXPECT_EQ(green.toJascString(), "0 255 0"); - const Rgba32 magenta{255, 0, 255, 0}; + constexpr Rgba32 magenta{255, 0, 255, 0}; EXPECT_EQ(magenta.toJascString(), "255 0 255"); } diff --git a/Porytiles-2.x/tests/Color/RgbaToBgrTest.cpp b/Porytiles-2.x/tests/Color/RgbaToBgrTest.cpp new file mode 100644 index 0000000..873e311 --- /dev/null +++ b/Porytiles-2.x/tests/Color/RgbaToBgrTest.cpp @@ -0,0 +1,22 @@ +#include + +#include +#include +#include + +TEST(RgbaToBgrTest, RgbaToBgr) +{ + using namespace porytiles::color; + + const RgbaToBgr rgbaToBgr{}; + + constexpr Rgba32 redRgba1{255, 0, 0}; + constexpr Bgr15 redBgr1{31}; + const auto convertedBgr1 = rgbaToBgr.convert(redRgba1); + EXPECT_EQ(convertedBgr1, redBgr1); + + constexpr Rgba32 redRgba2{250, 0, 0}; + const auto convertedBgr2 = rgbaToBgr.convert(redRgba2); + // should still match redBgr1 due to precision loss between formats + EXPECT_EQ(convertedBgr2, redBgr1); +} diff --git a/Porytiles-2.x/tools/driver/main.cpp b/Porytiles-2.x/tools/driver/main.cpp index 3b461a9..31240eb 100644 --- a/Porytiles-2.x/tools/driver/main.cpp +++ b/Porytiles-2.x/tools/driver/main.cpp @@ -3,13 +3,12 @@ #include "porytiles/Color/Bgr15.h" #include "porytiles/Color/Rgba32.h" -#include "porytiles/Color/ColorConverters.h" int main() { using namespace porytiles::color; - const Bgr15 bgr{}; - const Rgba32 rgb{}; + constexpr Bgr15 bgr{}; + constexpr Rgba32 rgb{}; std::cout << "Hello, Porytiles!" << std::endl; std::cout << "My BGR raw value: " << std::to_string(bgr.getRawValue()) << std::endl; std::cout << "My RGB jasc string: " << rgb.toJascString() << std::endl;