1 | /**********************************************************************
|
---|
2 | *<
|
---|
3 | FILE: color.h
|
---|
4 |
|
---|
5 | DESCRIPTION:
|
---|
6 |
|
---|
7 | CREATED BY: Dan Silva
|
---|
8 |
|
---|
9 | HISTORY:
|
---|
10 |
|
---|
11 | *> Copyright (c) 1994, All Rights Reserved.
|
---|
12 | **********************************************************************/
|
---|
13 |
|
---|
14 | #ifndef _COLOR_H
|
---|
15 |
|
---|
16 | #define _COLOR_H
|
---|
17 |
|
---|
18 | #include "point3.h"
|
---|
19 |
|
---|
20 | #define FLto255(x) ((int)((x)*255.0f+.5))
|
---|
21 |
|
---|
22 | class Color;
|
---|
23 |
|
---|
24 | struct RealPixel {
|
---|
25 | unsigned char r,g,b; // mantissas
|
---|
26 | unsigned char e; // exponent
|
---|
27 | DllExport operator Color();
|
---|
28 | };
|
---|
29 |
|
---|
30 | DllExport RealPixel MakeRealPixel(float r, float g, float b);
|
---|
31 | DllExport void ExpandRealPixel(const RealPixel &rp, float &r, float &g, float &b);
|
---|
32 |
|
---|
33 |
|
---|
34 | class Color {
|
---|
35 | public:
|
---|
36 | float r,g,b;
|
---|
37 |
|
---|
38 | // Constructors
|
---|
39 | Color() {}
|
---|
40 | Color(float R, float G, float B) { r = R; g = G; b = B; }
|
---|
41 | Color(double R, double G, double B) { r = (float)R; g = (float)G; b = (float)B; }
|
---|
42 | Color(int R, int G, int B) { r = (float)R; g = (float)G; b = (float)B; }
|
---|
43 | Color(const Color& a) { r = a.r; g = a.g; b = a.b; }
|
---|
44 | DllExport Color(DWORD rgb); // from Windows RGB value
|
---|
45 | Color(Point3 p) { r = p.x; g = p.y; b = p.z; }
|
---|
46 | Color(float af[3]) { r = af[0]; g = af[1]; b = af[2]; }
|
---|
47 | Color(RealPixel rp) { ExpandRealPixel(rp,r,g,b); }
|
---|
48 |
|
---|
49 | void Black() { r = g = b = 0.0f; }
|
---|
50 | void White() { r = g = b = 1.0f; }
|
---|
51 |
|
---|
52 | DllExport void ClampMax(); // makes components >= 0.0
|
---|
53 | DllExport void ClampMin(); // makes components <= 1.0
|
---|
54 | DllExport void ClampMinMax(); // makes components in [0,1]
|
---|
55 |
|
---|
56 | // Access operators
|
---|
57 | float& operator[](int i) { return (&r)[i]; }
|
---|
58 | const float& operator[](int i) const { return (&r)[i]; }
|
---|
59 |
|
---|
60 | // Conversion function
|
---|
61 | operator float*() { return(&r); }
|
---|
62 |
|
---|
63 | // Convert to Windows RGB
|
---|
64 | operator DWORD() { return RGB(FLto255(r),FLto255(g), FLto255(b)); }
|
---|
65 |
|
---|
66 | // Convert to Point3
|
---|
67 | operator Point3() { return Point3(r,g,b); }
|
---|
68 |
|
---|
69 | // Convert to RealPixel
|
---|
70 | DllExport operator RealPixel() { return MakeRealPixel(r,g,b); }
|
---|
71 |
|
---|
72 | // Unary operators
|
---|
73 | Color operator-() const { return(Color(-r,-g,-b)); }
|
---|
74 | Color operator+() const { return *this; }
|
---|
75 |
|
---|
76 | // Assignment operators
|
---|
77 | inline Color& operator-=(const Color&);
|
---|
78 | inline Color& operator+=(const Color&);
|
---|
79 | inline Color& operator*=(float);
|
---|
80 | inline Color& operator/=(float);
|
---|
81 | inline Color& operator*=(const Color&); // element-by-element multiplg.
|
---|
82 |
|
---|
83 | // Test for equality
|
---|
84 | int operator==(const Color& p) const { return ((p.r==r)&&(p.g==g)&&(p.b==b)); }
|
---|
85 | int operator!=(const Color& p) const { return ((p.r!=r)||(p.g!=g)||(p.b!=b)); }
|
---|
86 |
|
---|
87 | // Binary operators
|
---|
88 | inline Color operator-(const Color&) const;
|
---|
89 | inline Color operator+(const Color&) const;
|
---|
90 | inline Color operator/(const Color&) const;
|
---|
91 | inline Color operator*(const Color&) const;
|
---|
92 | inline Color operator^(const Color&) const; // CROSS PRODUCT
|
---|
93 | };
|
---|
94 |
|
---|
95 | int DllExport MaxComponent(const Color&); // index of the component with the maximum abs value
|
---|
96 | int DllExport MinComponent(const Color&); // index of the component with the minimum abs value
|
---|
97 |
|
---|
98 | float DllExport MaxVal(const Color&); // value of the component with the maximum abs value
|
---|
99 | float DllExport MinVal(const Color&); // value of the component with the minimum abs value
|
---|
100 |
|
---|
101 | // Inlines:
|
---|
102 |
|
---|
103 | inline float Length(const Color& v) {
|
---|
104 | return (float)sqrt(v.r*v.r+v.g*v.g+v.b*v.b);
|
---|
105 | }
|
---|
106 |
|
---|
107 | inline Color& Color::operator-=(const Color& a) {
|
---|
108 | r -= a.r; g -= a.g; b -= a.b;
|
---|
109 | return *this;
|
---|
110 | }
|
---|
111 |
|
---|
112 | inline Color& Color::operator+=(const Color& a) {
|
---|
113 | r += a.r; g += a.g; b += a.b;
|
---|
114 | return *this;
|
---|
115 | }
|
---|
116 |
|
---|
117 | inline Color& Color::operator*=(float f) {
|
---|
118 | r *= f; g *= f; b *= f;
|
---|
119 | return *this;
|
---|
120 | }
|
---|
121 |
|
---|
122 | inline Color& Color::operator/=(float f) {
|
---|
123 | r /= f; g /= f; b /= f;
|
---|
124 | return *this;
|
---|
125 | }
|
---|
126 |
|
---|
127 | inline Color& Color::operator*=(const Color& a) {
|
---|
128 | r *= a.r; g *= a.g; b *= a.b;
|
---|
129 | return *this;
|
---|
130 | }
|
---|
131 |
|
---|
132 | inline Color Color::operator-(const Color& c) const {
|
---|
133 | return(Color(r-c.r,g-c.g,b-c.b));
|
---|
134 | }
|
---|
135 |
|
---|
136 | inline Color Color::operator+(const Color& c) const {
|
---|
137 | return(Color(r+c.r,g+c.g,b+c.b));
|
---|
138 | }
|
---|
139 |
|
---|
140 | inline Color Color::operator/(const Color& c) const {
|
---|
141 | return Color(r/c.r,g/c.g,b/c.b);
|
---|
142 | }
|
---|
143 |
|
---|
144 | inline Color Color::operator*(const Color& c) const {
|
---|
145 | return Color(r*c.r, g*c.g, b*c.b);
|
---|
146 | }
|
---|
147 |
|
---|
148 | inline Color operator*(float f, const Color& a) {
|
---|
149 | return(Color(a.r*f, a.g*f, a.b*f));
|
---|
150 | }
|
---|
151 |
|
---|
152 | inline Color operator*(const Color& a, float f) {
|
---|
153 | return(Color(a.r*f, a.g*f, a.b*f));
|
---|
154 | }
|
---|
155 |
|
---|
156 | inline Color operator/(const Color& a, float f) {
|
---|
157 | return(Color(a.r/f, a.g/f, a.b/f));
|
---|
158 | }
|
---|
159 |
|
---|
160 | inline Color operator+(const Color& a, float f) {
|
---|
161 | return(Color(a.r+f, a.g+f, a.b+f));
|
---|
162 | }
|
---|
163 |
|
---|
164 | inline Color operator+(float f, const Color& a) {
|
---|
165 | return(Color(a.r+f, a.g+f, a.b+f));
|
---|
166 | }
|
---|
167 |
|
---|
168 | inline Color operator-(const Color& a, float f) {
|
---|
169 | return(Color(a.r-f, a.g-f, a.b-f));
|
---|
170 | }
|
---|
171 |
|
---|
172 | inline Color operator-(float f, const Color& a) {
|
---|
173 | return(Color(f-a.r, f-a.g, f-a.b));
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | #endif
|
---|
178 |
|
---|