Better color typing

This commit is contained in:
Ian Gulliver
2017-08-13 08:44:49 -07:00
parent 0ccae577eb
commit 00a1577c53
8 changed files with 87 additions and 85 deletions

44
image.h
View File

@@ -9,57 +9,57 @@
#include "color.h"
#include "coord.h"
template <int32_t X, int32_t Y, int32_t C>
class Image : public Array<Array<Color<C>, X>, Y> {
template <int32_t X, int32_t Y, class C>
class Image : public Array<Array<C, X>, Y> {
public:
constexpr const Color<C>& GetPixel(const Coord<2>& coord) const;
constexpr const C& GetPixel(const Coord<2>& coord) const;
void SetPixel(const Coord<2>& coord, const Color<C>& color);
void DrawXLine(const Coord<2>& start, const Color<C>& color, int32_t length);
void DrawYLine(const Coord<2>& start, const Color<C>& color, int32_t length);
void DrawRectangle(const Coord<2>& start, const Color<C>& color, int32_t x_length, int32_t y_length);
void DrawSquare(const Coord<2>& start, const Color<C>& color, int32_t length);
void SetPixel(const Coord<2>& coord, const C& color);
void DrawXLine(const Coord<2>& start, const C& color, int32_t length);
void DrawYLine(const Coord<2>& start, const C& color, int32_t length);
void DrawRectangle(const Coord<2>& start, const C& color, int32_t x_length, int32_t y_length);
void DrawSquare(const Coord<2>& start, const C& color, int32_t length);
std::string ToPng();
};
template <int32_t X, int32_t Y, int32_t C>
constexpr const Color<C>& Image<X, Y, C>::GetPixel(const Coord<2>& coord) const {
template <int32_t X, int32_t Y, class C>
constexpr const C& Image<X, Y, C>::GetPixel(const Coord<2>& coord) const {
return this->at(coord.at(1)).at(coord.at(0));
}
template <int32_t X, int32_t Y, int32_t C>
void Image<X, Y, C>::SetPixel(const Coord<2>& coord, const Color<C>& color) {
template <int32_t X, int32_t Y, class C>
void Image<X, Y, C>::SetPixel(const Coord<2>& coord, const C& color) {
if (coord.at(0) >= X || coord.at(1) >= Y) {
return;
}
this->at(coord.at(1)).at(coord.at(0)) = color;
}
template <int32_t X, int32_t Y, int32_t C>
void Image<X, Y, C>::DrawXLine(const Coord<2>& coord, const Color<C>& color, int32_t length) {
template <int32_t X, int32_t Y, class C>
void Image<X, Y, C>::DrawXLine(const Coord<2>& coord, const C& color, int32_t length) {
for (int32_t x = coord.at(0); x <= coord.at(0) + length; ++x) {
SetPixel({{{{x, coord.at(1)}}}}, color);
}
}
template <int32_t X, int32_t Y, int32_t C>
void Image<X, Y, C>::DrawYLine(const Coord<2>& coord, const Color<C>& color, int32_t length) {
template <int32_t X, int32_t Y, class C>
void Image<X, Y, C>::DrawYLine(const Coord<2>& coord, const C& color, int32_t length) {
for (int32_t y = coord.at(1); y <= coord.at(1) + length; ++y) {
SetPixel({{{{coord.at(0), y}}}}, color);
}
}
template <int32_t X, int32_t Y, int32_t C>
void Image<X, Y, C>::DrawRectangle(const Coord<2>& start, const Color<C>& color, int32_t x_length, int32_t y_length) {
template <int32_t X, int32_t Y, class C>
void Image<X, Y, C>::DrawRectangle(const Coord<2>& start, const C& color, int32_t x_length, int32_t y_length) {
DrawXLine(start, color, x_length);
DrawXLine({{{{start.at(0), start.at(1) + y_length}}}}, color, x_length);
DrawYLine(start, color, y_length);
DrawYLine({{{{start.at(0) + x_length, start.at(1)}}}}, color, y_length);
}
template <int32_t X, int32_t Y, int32_t C>
void Image<X, Y, C>::DrawSquare(const Coord<2>& start, const Color<C>& color, int32_t length) {
template <int32_t X, int32_t Y, class C>
void Image<X, Y, C>::DrawSquare(const Coord<2>& start, const C& color, int32_t length) {
DrawRectangle(start, color, length, length);
}
@@ -68,9 +68,9 @@ static inline void WriteCallback(png_structp png_ptr, png_bytep data, png_size_t
dest->append(reinterpret_cast<char*>(data), length);
}
template <int32_t X, int32_t Y, int32_t C>
template <int32_t X, int32_t Y, class C>
std::string Image<X, Y, C>::ToPng() {
static_assert(C == 3); // PNG only supports RGB
// TODO: specialize this to RgbColor
std::string ret;