-
Notifications
You must be signed in to change notification settings - Fork 0
/
Color.cpp
96 lines (79 loc) · 1.4 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <fstream>
#include "Color.h"
Color::Color()
{
}
Color::Color(const Color& c)
: r(c.r), g(c.g), b(c.b)
{
}
Color::Color(float r, float g, float b)
: r(r), g(g), b(b)
{
}
Color& Color::operator=(const Color& c)
{
r = c.r;
g = c.g;
b = c.b;
return *this;
}
Color Color::operator+(const Color& c) const
{
return Color(r + c.r, g + c.g, b + c.b);
}
Color& Color::operator+=(const Color& c)
{
r += c.r;
g += c.g;
b += c.b;
return *this;
}
Color Color::operator-(const Color& c) const
{
return Color(r - c.r, g - c.g, b - c.b);
}
Color Color::operator*(float m) const
{
return Color(r * m, g * m, b * m);
}
Color Color::operator*(const Color& c) const
{
return Color(r * c.r, g * c.g, b * c.b);
}
int Color::R255() const
{
return floatToInt255(r);
}
int Color::G255() const
{
return floatToInt255(g);
}
int Color::B255() const
{
return floatToInt255(b);
}
int Color::floatToInt255(float f) const
{
int val = (int)(f * 255);
if (val > 255) val = 255;
return val;
}
std::ostream& operator<<(std::ostream& os, const Color& c)
{
os << c.R255() << " " << c.G255() << " " << c.B255() << " ";
return os;
}
std::ifstream& operator>>(std::ifstream& ifs, Color& c)
{
char r, g, b;
ifs.get(r);
ifs.get(g);
ifs.get(b);
float rf = (unsigned char)(r) / 255.0f;
float gf = (unsigned char)(g) / 255.0f;
float bf = (unsigned char)(b) / 255.0f;
c = Color(rf, gf, bf);
return ifs;
}