source: golgotha/src/maxtool/sdk_inc/color.h @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
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
22class Color;
23
24struct RealPixel {
25        unsigned char r,g,b;   // mantissas
26        unsigned char e;           // exponent
27        DllExport operator Color();
28        };
29
30DllExport RealPixel MakeRealPixel(float r, float g, float b);
31DllExport void ExpandRealPixel(const RealPixel &rp, float &r, float &g, float &b);
32
33
34class Color {
35public:
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
95int DllExport MaxComponent(const Color&);  // index of the component with the maximum abs value
96int DllExport MinComponent(const Color&);  // index of the component with the minimum abs value
97
98float DllExport MaxVal(const Color&);  // value of the component with the maximum abs value
99float DllExport MinVal(const Color&);  // value of the component with the minimum abs value
100
101// Inlines:
102
103inline float Length(const Color& v) {   
104        return (float)sqrt(v.r*v.r+v.g*v.g+v.b*v.b);
105        }
106
107inline Color& Color::operator-=(const Color& a) {       
108        r -= a.r;       g -= a.g;       b -= a.b;
109        return *this;
110        }
111
112inline Color& Color::operator+=(const Color& a) {
113        r += a.r;       g += a.g;       b += a.b;
114        return *this;
115        }
116
117inline Color& Color::operator*=(float f) {
118        r *= f;   g *= f;       b *= f;
119        return *this;
120        }
121
122inline Color& Color::operator/=(float f) {
123        r /= f; g /= f; b /= f;
124        return *this;
125        }
126
127inline Color& Color::operator*=(const Color& a) {
128        r *= a.r;       g *= a.g;       b *= a.b;       
129        return *this;
130        }
131
132inline Color Color::operator-(const Color& c) const {
133        return(Color(r-c.r,g-c.g,b-c.b));
134        }
135
136inline Color Color::operator+(const Color& c) const {
137        return(Color(r+c.r,g+c.g,b+c.b));
138        }
139
140inline Color Color::operator/(const Color& c) const {
141        return Color(r/c.r,g/c.g,b/c.b);
142        }
143
144inline Color Color::operator*(const Color& c) const { 
145        return Color(r*c.r, g*c.g, b*c.b);     
146        }
147
148inline Color operator*(float f, const Color& a) {
149        return(Color(a.r*f, a.g*f, a.b*f));
150        }
151
152inline Color operator*(const Color& a, float f) {
153        return(Color(a.r*f, a.g*f, a.b*f));
154        }
155
156inline Color operator/(const Color& a, float f) {
157        return(Color(a.r/f, a.g/f, a.b/f));
158        }
159
160inline Color operator+(const Color& a, float f) {
161        return(Color(a.r+f, a.g+f, a.b+f));
162        }
163
164inline Color operator+(float f, const Color& a) {
165        return(Color(a.r+f, a.g+f, a.b+f));
166        }
167
168inline Color operator-(const Color& a, float f) {
169        return(Color(a.r-f, a.g-f, a.b-f));
170        }
171
172inline 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
Note: See TracBrowser for help on using the repository browser.