1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * ---------------------------------------------------------------------- |
---|
4 | * Color.cpp: Color class |
---|
5 | * |
---|
6 | * ====================================================================== |
---|
7 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
8 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
9 | * |
---|
10 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
11 | * |
---|
12 | * See the file "license.terms" for information on usage and |
---|
13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | * ====================================================================== |
---|
15 | */ |
---|
16 | |
---|
17 | #include <stdio.h> |
---|
18 | #include <assert.h> |
---|
19 | |
---|
20 | #include "Color.h" |
---|
21 | #include "define.h" |
---|
22 | |
---|
23 | Color::Color() { |
---|
24 | R=G=B=0.0; |
---|
25 | next=0; |
---|
26 | } |
---|
27 | |
---|
28 | Color::Color(double r, double g, double b) { |
---|
29 | R=r; |
---|
30 | G=g; |
---|
31 | B=b; |
---|
32 | next=0; |
---|
33 | } |
---|
34 | |
---|
35 | Color::Color(const Color& c) |
---|
36 | : R(c.R), G(c.G), B(c.B), next(c.next) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | Color& |
---|
41 | Color::operator=(const Color& c) |
---|
42 | { |
---|
43 | R = c.R; |
---|
44 | G = c.G; |
---|
45 | B = c.B; |
---|
46 | next = c.next; |
---|
47 | return *this; |
---|
48 | } |
---|
49 | |
---|
50 | void Color::LimitColors(){ //Limits the color to be in range of 0.0 and 1.0 |
---|
51 | if (R>1.0) R=1.0; |
---|
52 | if (G>1.0) G=1.0; |
---|
53 | if (B>1.0) B=1.0; |
---|
54 | |
---|
55 | if (R<0.0) R=0.0; |
---|
56 | if (G<0.0) G=0.0; |
---|
57 | if (B<0.0) B=0.0; |
---|
58 | } |
---|
59 | |
---|
60 | Color Color::operator*(double k){ |
---|
61 | return Color(R*k, G*k, B*k); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | //This is NOT member operator. It's used so we can write (k*V), not only (V*k) (V-vector k-scalar) |
---|
66 | Color operator*(double k, Color &other){ |
---|
67 | return Color(other.R*k, other.G*k, other.B*k); |
---|
68 | } |
---|
69 | |
---|
70 | Color::~Color(){} |
---|
71 | |
---|
72 | Color Color::operator +(Color &other){ |
---|
73 | return Color(this->R+other.R,this->G+other.G,this->B+other.B); |
---|
74 | } |
---|
75 | |
---|
76 | Color Color::operator *(Color &other){ |
---|
77 | return Color(this->R*other.R,this->G*other.G,this->B*other.B); |
---|
78 | } |
---|
79 | |
---|
80 | void Color::GetRGBA(double opacity, unsigned char *result){ |
---|
81 | LimitColors(); |
---|
82 | |
---|
83 | assert(opacity>=0 && opacity <=1); |
---|
84 | |
---|
85 | result[0] = (unsigned char) (R*255.0); |
---|
86 | result[1] = (unsigned char) (G*255.0); |
---|
87 | result[2] = (unsigned char) (B*255.0); |
---|
88 | result[3] = (unsigned char) (opacity*255.0); |
---|
89 | } |
---|
90 | |
---|
91 | void Color::SetRGBA(unsigned char *color){ |
---|
92 | R = color[0]; |
---|
93 | G = color[1]; |
---|
94 | B = color[2]; |
---|
95 | } |
---|
96 | |
---|
97 | void Color::GetRGB(unsigned char *result){ |
---|
98 | result[0] = (unsigned char) (R*255.0); |
---|
99 | result[1] = (unsigned char) (G*255.0); |
---|
100 | result[2] = (unsigned char) (B*255.0); |
---|
101 | } |
---|
102 | |
---|
103 | void Color::GetRGB(float *result){ |
---|
104 | result[0] = (float) (R); |
---|
105 | result[1] = (float) (G); |
---|
106 | result[2] = (float) (B); |
---|
107 | } |
---|
108 | |
---|