-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathColor.h
107 lines (85 loc) · 2.87 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
////////////////////////////////////////////////////////////////////////////////////
// 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 "Vector.h"
#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
template<typename T>
friend Vector<T,3> color2vector3(Color const &c) {
return Vector<T,3>((T) c.r(), (T) c.g(), (T) c.b());
}
template<typename T>
friend Vector<T,4> color2vector4(Color const &c) {
return Vector<T,4>((T) c.r(), (T) c.g(), (T) c.b(), (T) c.a());
}
template<typename T>
friend Color vector2color3(Vector<T,3> const &v) {
return Color(float(v(0)), float(v(1)), float(v(2)), 1.0f);
}
template<typename T>
friend Color vector2color4(Vector<T,4> const &v) {
return Color(float(v(0)), float(v(1)), float(v(2)), float(v(3)));
}
///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;
};
template<typename T>
Vector<T,3> color2vector3(Color const &c);
template<typename T>
Vector<T,4> color2vector4(Color const &c);
template<typename T>
Color vector2color3(Vector<T,3> const &v);
template<typename T>
Color vector2color4(Vector<T,4> const &v);
#endif