Skip to content

Commit 45e41ab

Browse files
committed
Start implementing Color, with unspecialized sdr::Rgb (Vector<3, uint8_t>)
1 parent 7c1e7fc commit 45e41ab

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

src/apps/Tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(${PROJECT_NAME}_HEADERS
88
set(${PROJECT_NAME}_SOURCES
99
Angle.cpp
1010
Barycentric.cpp
11+
Color_tests.cpp
1112
Constexpr_tests.cpp
1213
Matrix.cpp
1314
Noexcept_tests.cpp

src/apps/Tests/Color_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "catch.hpp"
2+
3+
#include <math/Color.h>
4+
5+
#include <type_traits>
6+
7+
8+
using namespace ad;
9+
using namespace ad::math;
10+
11+
12+
SCENARIO("Color types properties")
13+
{
14+
// Trivially copyable types have great properties for low level manipulation
15+
// (e.g. file serialization)
16+
// see: https://en.cppreference.com/w/cpp/types/is_trivially_copyable#Notes
17+
REQUIRE(std::is_trivially_copyable<sdr::Rgb>::value);
18+
}

src/libs/math/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(math)
33
set(${PROJECT_NAME}_HEADERS
44
Angle.h
55
Barycentric.h
6+
Color.h
67
commons.h
78
Constants.h
89
Matrix.h

src/libs/math/math/Color.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#pragma once
2+
3+
#include "Vector.h"
4+
5+
#include <iomanip>
6+
7+
8+
namespace ad {
9+
namespace math {
10+
11+
12+
#define ACCESSOR_DIMENSION(symbol, dimension) \
13+
typename base_type::value_type & symbol() \
14+
{ return this->at(dimension-1); } \
15+
typename base_type::value_type symbol() const \
16+
{ return this->at(dimension-1); }
17+
18+
19+
#define BASE Vector<Rgb_base<T_number>, 3, T_number>
20+
template <class T_number>
21+
class Rgb_base : public BASE
22+
{
23+
/// \todo Disable most of the available functions
24+
typedef BASE base_type;
25+
using base_type::base_type;
26+
27+
public:
28+
template<class T>
29+
using derived_type = Rgb_base<T>;
30+
31+
ACCESSOR_DIMENSION(r, 1)
32+
ACCESSOR_DIMENSION(g, 2)
33+
ACCESSOR_DIMENSION(b, 3)
34+
};
35+
#undef BASE
36+
37+
38+
namespace sdr {
39+
40+
41+
using Rgb = ::ad::math::Rgb_base<std::uint8_t>;
42+
43+
44+
constexpr const Rgb gBlack{(std::uint8_t)0, (std::uint8_t)0, (std::uint8_t)0};
45+
constexpr const Rgb gWhite{(std::uint8_t)255, (std::uint8_t)255, (std::uint8_t)255};
46+
47+
constexpr const Rgb gRed {(std::uint8_t)255, (std::uint8_t)0, (std::uint8_t)0};
48+
constexpr const Rgb gGreen{(std::uint8_t)0, (std::uint8_t)255, (std::uint8_t)0};
49+
constexpr const Rgb gBlue {(std::uint8_t)0, (std::uint8_t)0, (std::uint8_t)255};
50+
51+
52+
} // namespace sdr
53+
54+
55+
// CAUTION: For ADL, aRgb parameter is seen as the "aliased type", i.e. ::ad::math::Rgb_base
56+
// in ::ad::math namespcace, not in the alias sub-namespace
57+
inline std::ostream & operator<<(std::ostream & os, const sdr::Rgb &aRgb)
58+
{
59+
return os << "{"
60+
<< std::setw(3) << static_cast<int>(aRgb.r()) << "; "
61+
<< std::setw(3) << static_cast<int>(aRgb.g()) << "; "
62+
<< std::setw(3) << static_cast<int>(aRgb.b()) << "}";
63+
}
64+
65+
66+
#undef ACCESSOR_DIMENSION
67+
68+
69+
} // namespace math
70+
} // namespace ad

0 commit comments

Comments
 (0)