-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorData.cpp
76 lines (55 loc) · 1.78 KB
/
ColorData.cpp
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
//
// ColorData.cpp
// Originally created by the CSci-3081W TAs.
//
#include "ColorData.h"
ColorData::ColorData() : m_red(1), m_green(1), m_blue(1), m_alpha(1) {
}
ColorData::ColorData(float r, float g, float b ) : m_red(r), m_green(g), m_blue(b), m_alpha(1) {
}
ColorData::ColorData(float r, float g, float b, float a) : m_red(r), m_green(g), m_blue(b), m_alpha(a) {
}
void ColorData::setRed(float r) {
m_red = r;
}
void ColorData::setGreen(float g) {
m_green = g;
}
void ColorData::setBlue(float b) {
m_blue = b;
}
void ColorData::setAlpha(float a) {
m_alpha = a;
}
float ColorData::getRed() const {
return m_red;
}
float ColorData::getGreen() const {
return m_green;
}
float ColorData::getBlue() const {
return m_blue;
}
float ColorData::getAlpha() const {
return m_alpha;
}
float ColorData::getLuminance() const {
return 0.2126*m_red + 0.7152*m_green + 0.0722*m_blue;
}
ColorData ColorData::clampedColor() const {
float clampedRed = ColorData::clampValue(this->getRed(), 0.f, 1.f);
float clampedGreen = ColorData::clampValue(this->getGreen(), 0.f, 1.f);
float clampedBlue = ColorData::clampValue(this->getBlue(), 0.f, 1.f);
float clampedAlpha = ColorData::clampValue(this->getAlpha(), 0.f, 1.f);
return ColorData(clampedRed, clampedGreen, clampedBlue, clampedAlpha);
}
// Apply component-wise arithmatic operations
ColorData operator* (const ColorData& a, float f) {
return ColorData(a.m_red*f, a.m_green*f, a.m_blue*f, a.m_alpha*f);
}
ColorData operator+ (const ColorData& a, const ColorData& b) {
return ColorData(a.m_red + b.m_red, a.m_green + b.m_green, a.m_blue + b.m_blue, a.m_alpha + b.m_alpha);
}
ColorData operator- (const ColorData& a, const ColorData& b) {
return ColorData(a.m_red - b.m_red, a.m_green - b.m_green, a.m_blue - b.m_blue, a.m_alpha - b.m_alpha);
}