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