NcEngine
Color.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "ncmath/Vector.h"
8
9namespace nc
10{
12struct Color : public Vector4
13{
14 using Vector4::Vector4;
15
16 constexpr explicit Color(const Vector4& in)
17 : Vector4{in}
18 {
19 }
20
21 constexpr explicit Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
22 : Vector4{(float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, (float)a / 255.0f}
23 {
24 }
25
26 static constexpr auto Red() -> Color { return Color{1.0f, 0.0, 0.0f, 1.0f}; }
27 static constexpr auto Green() -> Color { return Color{0.0f, 1.0, 0.0f, 1.0f}; }
28 static constexpr auto Blue() -> Color { return Color{0.0f, 0.0, 1.0f, 1.0f}; }
29 static constexpr auto White() -> Color { return Color{1.0f, 1.0, 1.0f, 1.0f}; }
30 static constexpr auto Black() -> Color { return Color{0.0f, 0.0, 0.0f, 1.0f}; }
31 static constexpr auto Transparent() -> Color { return Color{0.0f, 0.0, 0.0f, 0.0f}; }
32
33 constexpr auto R() -> float& { return x; }
34 constexpr auto G() -> float& { return y; }
35 constexpr auto B() -> float& { return z; }
36 constexpr auto A() -> float& { return w; }
37 constexpr auto R() const -> const float& { return x; }
38 constexpr auto G() const -> const float& { return y; }
39 constexpr auto B() const -> const float& { return z; }
40 constexpr auto A() const -> const float& { return w; }
41};
42
45{
46 Color start;
47 Color end;
48
49 constexpr Gradient() noexcept
50 : Gradient{Color::White()}
51 {
52 }
53
54 constexpr Gradient(const Color& solidColor) noexcept
55 : start{solidColor}, end{solidColor}
56 {
57 }
58
59 constexpr Gradient(const Color& from, const Color& to) noexcept
60 : start{from}, end{to}
61 {
62 }
63
65 constexpr auto Lerp(float t) const noexcept -> Color
66 {
67 return Color{
68 nc::Lerp(start.R(), end.R(), t),
69 nc::Lerp(start.G(), end.G(), t),
70 nc::Lerp(start.B(), end.B(), t),
71 nc::Lerp(start.A(), end.A(), t)
72 };
73 }
74
76 constexpr auto Lerp(float tColor, float tAlpha) const noexcept -> Color
77 {
78 return Color{
79 nc::Lerp(start.R(), end.R(), tColor),
80 nc::Lerp(start.G(), end.G(), tColor),
81 nc::Lerp(start.B(), end.B(), tColor),
82 nc::Lerp(start.A(), end.A(), tAlpha)
83 };
84 }
85
86 friend constexpr auto operator==(const Gradient& lhs, const Gradient& rhs) -> bool
87 {
88 return lhs.start == rhs.start && lhs.end == rhs.end;
89 }
90};
91} // namespace nc
Four component color.
Definition: Color.h:13
Gradient between two Colors.
Definition: Color.h:45
constexpr auto Lerp(float t) const noexcept -> Color
Select a Color between start and end.
Definition: Color.h:65
constexpr auto Lerp(float tColor, float tAlpha) const noexcept -> Color
Select a Color between start and end with distinct color and alpha factors.
Definition: Color.h:76
A four component vector.
Definition: Vector.h:48