-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.h
66 lines (49 loc) · 1.45 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
#ifndef COLOR_H
#define COLOR_H
#include <cmath>
#include <iostream>
#include "util.h"
using std::sqrt;
/* A color for a PPM file is red, green blue amounts as whole numbers 0-255 */
class color {
public:
color(double in1, double in2, double in3) : val{in1, in2, in3} {}
color() : val{0,0,0} {} //default constructor
color(double in) : val{in,in,in} {} //alt constructor
//getters
double r() const { return val[0]; }
double g() const { return val[1]; }
double b() const { return val[2]; }
double bright() const { return val[0]+val[1]+val[2]; }
void setR(double inX) { val[0] = inX; }
void setG(double inY) { val[1] = inY; }
void setB(double inZ) { val[2] = inZ; }
//overload operators
color& operator+=(const color &v) {
val[0] += v.val[0];
val[1] += v.val[1];
val[2] += v.val[2];
return *this;
}
//mult by scalar
color& operator*=(const double Sc) {
val[0] *=Sc;
val[1] *=Sc;
val[2] *=Sc;
return *this;
}
virtual bool operator==(const color& other) const {
if (typeid(*this) != typeid(other))
return false;
return (val[0] == other.val[0] && val[1] == other.val[1] && val[2] == other.val[2]);
}
inline static color random() {
return color(niceRand(), niceRand(), niceRand());
}
inline static color random(double min, double max) {
return color(nicerRand(min, max), nicerRand(min, max), nicerRand(min, max));
}
private:
double val[3];
};
#endif