1 | /**********************************************************************
|
---|
2 | *<
|
---|
3 | FILE: acolor.h
|
---|
4 |
|
---|
5 | DESCRIPTION: floating point color + alpha
|
---|
6 |
|
---|
7 | CREATED BY: Dan Silva
|
---|
8 |
|
---|
9 | HISTORY:
|
---|
10 |
|
---|
11 | *> Copyright (c) 1994, All Rights Reserved.
|
---|
12 | **********************************************************************/
|
---|
13 |
|
---|
14 | #ifndef _ACOLOR_H
|
---|
15 |
|
---|
16 | #define _ACOLOR_H
|
---|
17 |
|
---|
18 | #include "point3.h"
|
---|
19 | #include "color.h"
|
---|
20 |
|
---|
21 | class AColor {
|
---|
22 | public:
|
---|
23 | float r,g,b,a;
|
---|
24 |
|
---|
25 | // Constructors
|
---|
26 | AColor() {}
|
---|
27 | AColor(float R, float G, float B, float A=1.0f) { r = R; g = G; b = B; a = A; }
|
---|
28 | AColor(double R, double G, double B, double A=1.0) {
|
---|
29 | r = (float)R; g = (float)G; b = (float)B; a = (float)A; }
|
---|
30 | AColor(int R, int G, int B, int A=0) {
|
---|
31 | r = (float)R; g = (float)G; b = (float)B; a = (float)A; }
|
---|
32 | AColor(const AColor& c) { r = c.r; g = c.g; b = c.b; a = c.a; }
|
---|
33 | AColor(const Color& c, float alph=1.0f) { r = c.r; g = c.g; b = c.b; a = alph; }
|
---|
34 | AColor(DWORD rgb, float alph=1.0f); // from Windows RGB value
|
---|
35 | AColor(float af[4]) { r = af[0]; g = af[1]; b = af[2];a = af[3]; }
|
---|
36 |
|
---|
37 | void Black() { r = g = b = 0.0f; a = 1.0f; }
|
---|
38 | void White() { r = g = b = 1.0f; a = 1.0f; }
|
---|
39 |
|
---|
40 | // Access operators
|
---|
41 | float& operator[](int i) { return (&r)[i]; }
|
---|
42 | const float& operator[](int i) const { return (&r)[i]; }
|
---|
43 |
|
---|
44 | // Conversion functions
|
---|
45 | operator float*() { return(&r); }
|
---|
46 | operator Color() { return Color(r,g,b); }
|
---|
47 |
|
---|
48 | // Convert to Windows RGB
|
---|
49 | operator DWORD() { return RGB(FLto255(r),FLto255(g), FLto255(b)); }
|
---|
50 |
|
---|
51 | // Convert to Point3
|
---|
52 | operator Point3() { return Point3(r,g,b); }
|
---|
53 |
|
---|
54 | // Unary operators
|
---|
55 | AColor operator-() const { return (AColor(-r,-g,-b, -a)); }
|
---|
56 | AColor operator+() const { return *this; }
|
---|
57 |
|
---|
58 | // Assignment operators
|
---|
59 | inline AColor& operator-=(const AColor&);
|
---|
60 | inline AColor& operator+=(const AColor&);
|
---|
61 | inline AColor& operator*=(float);
|
---|
62 | inline AColor& operator/=(float);
|
---|
63 | inline AColor& operator*=(const AColor&); // element-by-element multiplg.
|
---|
64 |
|
---|
65 | // Test for equality
|
---|
66 | int operator==(const AColor& p) const { return ((p.r==r)&&(p.g==g)&&(p.b==b)&&(p.a==a)); }
|
---|
67 | int operator!=(const AColor& p) const { return ((p.r!=r)||(p.g!=g)||(p.b!=b)||(p.a!=a)); }
|
---|
68 |
|
---|
69 | // Binary operators
|
---|
70 | inline AColor operator-(const AColor&) const;
|
---|
71 | inline AColor operator+(const AColor&) const;
|
---|
72 | inline AColor operator/(const AColor&) const;
|
---|
73 | inline AColor operator*(const AColor&) const;
|
---|
74 | inline AColor operator^(const AColor&) const; // CROSS PRODUCT
|
---|
75 | };
|
---|
76 |
|
---|
77 | int DllExport MaxComponent(const AColor&); // the component with the maximum abs value
|
---|
78 | int DllExport MinComponent(const AColor&); // the component with the minimum abs value
|
---|
79 |
|
---|
80 | // Inlines:
|
---|
81 |
|
---|
82 | inline AColor& AColor::operator-=(const AColor& c) {
|
---|
83 | r -= c.r; g -= c.g; b -= c.b; a -= c.a;
|
---|
84 | return *this;
|
---|
85 | }
|
---|
86 |
|
---|
87 | inline AColor& AColor::operator+=(const AColor& c) {
|
---|
88 | r += c.r; g += c.g; b += c.b; a += c.a;
|
---|
89 | return *this;
|
---|
90 | }
|
---|
91 |
|
---|
92 | inline AColor& AColor::operator*=(float f) {
|
---|
93 | r *= f; g *= f; b *= f; a *= f;
|
---|
94 | return *this;
|
---|
95 | }
|
---|
96 |
|
---|
97 | inline AColor& AColor::operator/=(float f) {
|
---|
98 | r /= f; g /= f; b /= f; a /= f;
|
---|
99 | return *this;
|
---|
100 | }
|
---|
101 |
|
---|
102 | inline AColor& AColor::operator*=(const AColor& c) {
|
---|
103 | r *= c.r; g *= c.g; b *= c.b; a *= c.a;
|
---|
104 | return *this;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | inline AColor AColor::operator-(const AColor& c) const {
|
---|
109 | return(AColor(r-c.r,g-c.g,b-c.b,a-c.a));
|
---|
110 | }
|
---|
111 |
|
---|
112 | inline AColor AColor::operator+(const AColor& c) const {
|
---|
113 | return(AColor(r+c.r,g+c.g,b+c.b,a+c.a));
|
---|
114 | }
|
---|
115 |
|
---|
116 | inline AColor AColor::operator/(const AColor& c) const {
|
---|
117 | return AColor(r/c.r,g/c.g,b/c.b,a/c.a);
|
---|
118 | }
|
---|
119 |
|
---|
120 | inline AColor AColor::operator*(const AColor& c) const {
|
---|
121 | return AColor(r*c.r, g*c.g, b*c.b, a*c.a);
|
---|
122 | }
|
---|
123 |
|
---|
124 | inline AColor operator*(float f, const AColor& a) {
|
---|
125 | return(AColor(a.r*f, a.g*f, a.b*f, a.a*f));
|
---|
126 | }
|
---|
127 |
|
---|
128 | inline AColor operator*(const AColor& a, float f) {
|
---|
129 | return(AColor(a.r*f, a.g*f, a.b*f, a.a*f));
|
---|
130 | }
|
---|
131 |
|
---|
132 | // Composite fg over bg, assuming associated alpha,
|
---|
133 | // i.e. pre-multiplied alpha for both fg and bg
|
---|
134 | inline AColor CompOver(const AColor &fg, const AColor& bg) {
|
---|
135 | return fg + (1.0f-fg.a)*bg;
|
---|
136 | }
|
---|
137 |
|
---|
138 | typedef AColor RGBA;
|
---|
139 |
|
---|
140 | #endif
|
---|
141 |
|
---|