-
Notifications
You must be signed in to change notification settings - Fork 16
/
Color.h
121 lines (94 loc) · 3.27 KB
/
Color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
////////////////////////////////////////////////////////////////////////////////////
// Copyright © Charalambos "Charis" Poullis, [email protected] //
// This work can only be used under an exclusive license of the author. //
////////////////////////////////////////////////////////////////////////////////////
#ifndef __COLOR_H__
#define __COLOR_H__
#include <math.h>
#include <Eigen/Eigen>
using namespace Eigen;
#define COLOR_EPSILON 1e-03
class Color {
public:
///Constructors
Color();
Color(Color const &_color);
Color(float _grayscale);
Color(float _r, float _g, float _b);
Color(float _r, float _g, float _b, float _a);
///Destructor
~Color();
///Returns the color and opacity
float r() const;
float &r();
float g() const;
float &g();
float b() const;
float &b();
float a() const;
float &a();
///Get a specific element
float& operator() (int elementNumber);
float operator() (int elementNumber) const;
///Mathematical functions
friend Color operator* ( Color const &c1, Color const &c2);
friend Color operator* ( Color const &c1, float scalar);
friend Color operator* ( float scalar, Color const &c1);
friend Color operator+ ( Color const &c1, Color const &c2);
friend Color operator- ( Color const &c1, Color const &c2);
friend Color operator/ ( Color const &c1, Color const &c2);
friend Color operator/ ( Color const &c1, float scalar);
friend bool operator== ( Color const &c1, Color const &c2);
friend bool operator!= ( Color const &c1, Color const &c2);
Color &operator+= (Color const &vec);
Color &operator*= (float scalar);
Color &operator/= (float scalar);
///Conversion functions
friend Color vector2color3(Vector3i const &v) {
return Color(float(v(0)), float(v(1)), float(v(2)), 1.0f);
}
friend Color vector2color3(Vector3f const &v) {
return Color(v(0), v(1), v(2), 1.0f);
}
friend Color vector2color3(Vector3d const &v) {
return Color(float(v(0)), float(v(1)), float(v(2)), 1.0f);
}
friend Color vector2color4(Vector4i const &v) {
return Color(float(v(0)), float(v(1)), float(v(2)), float(v(3)));
}
friend Color vector2color4(Vector4f const &v) {
return Color(float(v(0)), float(v(1)), float(v(2)), float(v(3)));
}
friend Color vector2color4(Vector4d const &v) {
return Color(float(v(0)), float(v(1)), float(v(2)), float(v(3)));
}
friend Vector3f color2vector3(Color const &c) {
return Vector3f(c.r(), c.g(), c.b());
}
friend Vector4f color2vector4(Color const &c) {
return Vector4f(c.r(), c.g(), c.b(), c.a());
}
///Print functions
friend std::ostream &operator<<(std::ostream &os, Color const &c) {
os << "red: " << c.r() << " green: " << c.g() << " blue: " << c.b() << " alpha: " << c.a() << std::endl;
return os;
}
friend std::istream &operator>>(std::istream& is, Color &c) {
is >> c.data[0];
is >> c.data[1];
is >> c.data[2];
is >> c.data[3];
return is;
}
private:
Vector4f data;
};
Color vector2color3(Vector3i const &v);
Color vector2color3(Vector3f const &v);
Color vector2color3(Vector3d const &v);
Color vector2color4(Vector4i const &v);
Color vector2color4(Vector4f const &v);
Color vector2color4(Vector4d const &v);
Vector3f color2vector3(Color const &c);
Vector4f color2vector4(Color const &c);
#endif