Faro Engine 0.0.0.b519570 (main)
Loading...
Searching...
No Matches
Color.hpp
Go to the documentation of this file.
1#pragma once
2#include "../Containers/String.hpp"
3
4namespace Faro
5{
6 template <class T, int TMax>
7 struct IColor
8 {
9 union
10 {
11 T d[4];
12 #pragma warning(suppress: 4201)
13 struct { T r, g, b, a; };
14 };
15
16 IColor(T r, T g, T b, T a = TMax) : r(r), g(g), b(b), a(a) { }
17 IColor(float color[4]) : r(color[0]), g(color[1]), b(color[2]), a(color[3]) { }
18 IColor() : r(0), g(0), b(0), a(0) { }
19
21 {
22 return Color(r + other.r, g + other.g, b + other.b, a + other.a);
23 }
24
26 {
27 return Color(r + other.r, g + other.g, b + other.b, a + other.a);
28 }
29
31 {
32 return Color(r - other.r, g - other.g, b - other.b, a - other.a);
33 }
34
36 {
37 return Color(r - other.r, g - other.g, b - other.b, a - other.a);
38 }
39
40 IColor Lerp(IColor& other, float time) const
41 {
42 return Color((1 - time) * r + other.r * time, (1 - time) * g + other.g * time, (1 - time) * b + other.b * time, (1 - time) * a + other.a * time);
43 }
44
45 // Predefined colors
46 static IColor Red;
47 static IColor Green;
48 static IColor Blue;
49 static IColor White;
50 static IColor Black;
51 static IColor Clear;
52
53#ifdef _MSC_VER //TODO use engine preprocessor defines
54#define SSCANF sscanf_s
55#else
56#define SSCANF sscanf
57#endif
58 static IColor FromHex(String hex)
59 {
60 if (hex[0] == '#') hex.Erase(0);
61 int r = 255, g = 255, b = 255, a = 255;
62
63 if (hex.Length() == 6)
64 {
65 SSCANF(*hex, "%02x%02x%02x", &r, &g, &b);
66 }
67 else if (hex.Length() == 8)
68 {
69 SSCANF(*hex, "%02x%02x%02x%02x", &r, &g, &b, &a);
70 }
71
72 return IColor((r / 255.0f) * static_cast<float>(TMax), (g / 255.0f) * static_cast<float>(TMax), (b / 255.0f) * static_cast<float>(TMax), (a / 255.0f) * static_cast<float>(TMax));
73 }
74 };
75
78
79 template <class T, int TMax>
81
82 template <class T, int TMax>
84
85 template <class T, int TMax>
87
88 template <class T, int TMax>
90
91 template <class T, int TMax>
93
94 template <class T, int TMax>
96}
#define SSCANF
Definition Color.hpp:56
Definition String.hpp:12
uint32 Length()
Definition String.hpp:25
void Erase(uint32 index, uint32 amount=1)
Definition String.hpp:31
Definition Array.hpp:8
IColor< float, 1 > FloatColor
Definition Color.hpp:76
IColor< uint8, 255 > UInt8Color
Definition Color.hpp:77
Definition Color.hpp:8
IColor operator-(IColor &other)
Definition Color.hpp:30
T r
Definition Color.hpp:13
IColor(T r, T g, T b, T a=TMax)
Definition Color.hpp:16
static IColor White
Definition Color.hpp:49
IColor Lerp(IColor &other, float time) const
Definition Color.hpp:40
static IColor Black
Definition Color.hpp:50
static IColor Green
Definition Color.hpp:47
T g
Definition Color.hpp:13
IColor operator+(IColor &other)
Definition Color.hpp:20
static IColor Clear
Definition Color.hpp:51
T d[4]
Definition Color.hpp:11
IColor(float color[4])
Definition Color.hpp:17
T b
Definition Color.hpp:13
T a
Definition Color.hpp:13
IColor()
Definition Color.hpp:18
static IColor FromHex(String hex)
Definition Color.hpp:58
static IColor Red
Definition Color.hpp:46
static IColor Blue
Definition Color.hpp:48