-
Notifications
You must be signed in to change notification settings - Fork 2
/
Color.cpp
81 lines (67 loc) · 1.68 KB
/
Color.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
77
78
79
80
81
/*
* Color.cpp
* Created by Zachary Ferguson
* Source file for the definitions of the Color class.
*/
#include "Color.h"
/* Constructor that takes a red, green, and blue value. */
Color::Color(float red, float green, float blue)
{
/* Sets the color to the one given. */
this->setColor(red, green, blue);
}
/* Deconstructor */
Color::~Color(){}
/* Set the colors or a specific color. */
void Color::setColor(float red, float green, float blue)
{
/* Check that the color values are valid */
this->setRed(red);
this->setGreen(green);
this->setBlue(blue);
}
void Color::setRed(float red)
{
/* Check that the color value given is valid */
assert(red >= 0.0 && red <= 1.0);
this->red = red;
}
void Color::setGreen(float green)
{
/* Check that the color value given is valid */
assert(green >= 0.0 && green <= 1.0);
this->green = green;
}
void Color::setBlue(float blue)
{
/* Check that the color value given is valid */
assert(blue >= 0.0 && blue <= 1.0);
this->blue = blue;
}
/* Get all the colors as a vector or just a single color as a float. */
const std::vector<float> Color::getColor() const
{
return{ this->getRed(), this->getGreen(), this->getBlue() };
}
const float Color::getRed() const
{
return this->red;
}
const float Color::getGreen() const
{
return this->green;
}
const float Color::getBlue() const
{
return this->blue;
}
/* Convert this color to a string representation of the format "r g b". */
std::string Color::toString() const
{
/* Makes the string representation of the vector. */
std::stringstream strStream;
/* set the precision to 4 */
strStream.precision(4);
strStream << this->red << " " << this->green << " " << this->blue;
return strStream.str();
}