1 /**
  2  * Copyright (C) 2009-2012 Klaus Reimer <k@ailis.de>
  3  * See LICENSE.txt for licensing information
  4  * 
  5  * @require threedee.js
  6  */
  7 
  8 /**
  9  * Constructs a new color.
 10  * 
 11  * @param {number} red
 12  *            The red component (0.0 - 1.0)
 13  * @param {number} green
 14  *            The green component (0.0 - 1.0)
 15  * @param {number} blue
 16  *            The blue component (0.0 - 1.0)
 17  *            
 18  * @constructor
 19  * @class A color
 20  */
 21 threedee.Color = function(red, green, blue)
 22 {
 23     if (red) this.red = red;
 24     if (green) this.green = green;
 25     if (blue) this.blue = blue;
 26     this.css = "rgb(" + this.red * 255 + "," + this.green * 255 + "," +
 27         this.blue * 255 + ")";
 28     threedee.Color.counter++;
 29 };
 30 
 31 /** 
 32  * Instance counter. 
 33  * @private 
 34  * @type {number} 
 35  */
 36 threedee.Color.counter = 0;
 37 
 38 /** 
 39  * The red component. 
 40  * @private 
 41  * @type {number} 
 42  */
 43 threedee.Color.prototype.red = 0;
 44 
 45 /** 
 46  * The red component. 
 47  * @private 
 48  * @type {number} 
 49  */
 50 threedee.Color.prototype.green = 0;
 51 
 52 /** 
 53  * The red component. 
 54  * @private 
 55  * @type {number} 
 56  */
 57 threedee.Color.prototype.blue = 0;
 58 
 59 /** 
 60  * The CSS representation of the color. 
 61  * @private 
 62  * @type {string} 
 63  */
 64 threedee.Color.prototype.css = "rgb(0,0,0)";
 65 
 66 /** 
 67  * Black color. 
 68  * @final 
 69  * @type {!threedee.Color} 
 70  */
 71 threedee.Color.BLACK = new threedee.Color(0, 0, 0);
 72 
 73 /** 
 74  * Red color.
 75  * @final 
 76  * @type {!threedee.Color} 
 77  */
 78 threedee.Color.RED = new threedee.Color(1, 0, 0);
 79 
 80 /** 
 81  * Green color. 
 82  * @final 
 83  * @type {!threedee.Color} 
 84  */
 85 threedee.Color.GREEN = new threedee.Color(0, 1, 0);
 86 
 87 /** 
 88  * Blue color. 
 89  * @final 
 90  * @type {!threedee.Color} 
 91  */
 92 threedee.Color.BLUE = new threedee.Color(0, 0, 1);
 93 
 94 /** 
 95  * Dark gray color. 
 96  * @final 
 97  * @type {!threedee.Color} 
 98  */
 99 threedee.Color.DARK_GRAY = new threedee.Color(0.25, 0.25, 0.25);
100 
101 /** 
102  * Yellow color. 
103  * @final 
104  * @type {!threedee.Color} 
105  */
106 threedee.Color.YELLOW = new threedee.Color(1, 1, 0);
107 
108 /** 
109  * White color. 
110  * @final
111  * @type {!threedee.Color} 
112  */
113 threedee.Color.WHITE = new threedee.Color(1, 1, 1);
114 
115 /**
116  * Returns and resets the current instance counter.
117  * 
118  * @return {number} 
119  *            The number of created instances since the last call.
120  */
121 threedee.Color.count = function()
122 {
123     var value = threedee.Color.counter;
124     threedee.Color.counter = 0;
125     return value;
126 };
127 
128 /**
129  * Returns the red component.
130  * 
131  * @return {number} 
132  *            The red component.
133  */
134 threedee.Color.prototype.getRed = function()
135 {
136     return this.red;
137 };
138 
139 /**
140  * Returns the green component (0.0-1.0).
141  * 
142  * @return {number} 
143  *            The green component.
144  */
145 threedee.Color.prototype.getGreen = function()
146 {
147     return this.green;
148 };
149 
150 /**
151  * Returns the blue component (0.0-1.0).
152  * 
153  * @return {number} 
154  *            The blue component.
155  */
156 threedee.Color.prototype.getBlue = function()
157 {
158     return this.blue;
159 };
160 
161 /**
162  * Returns the CSS representation of the color (0.0-1.0).
163  * 
164  * @return {string} 
165  *            The CSS representation of the color.
166  */
167 threedee.Color.prototype.toCSS = function()
168 {
169     return this.css;
170 };
171 
172 /**
173  * Returns the three color components as an array. The entries are in the
174  * range from 0.0 to 1.0.
175  *
176  * @return {!Array.<number>} 
177  *            The three color components as an array
178  */
179 threedee.Color.prototype.getComponents = function()
180 {
181     return [ this.red, this.green, this.blue ];
182 };
183 
184 /**
185  * Returns a color component. 0=red, 1=green, 2=blue.
186  *
187  * @param {number} component
188  *            The component index
189  * @return {number} The color component (0-1)
190  */
191 threedee.Color.prototype.getComponent = function(component)
192 {
193     return (component ? (component == 1 ? this.green : this.blue) : this.red);
194 };
195 
196 /**
197  * Converts the color into a JSON object with keys 'r', 'g' and 'b' and
198  * returns it.
199  * 
200  * @return {Object} The color as a JSON object
201  */
202 threedee.Color.prototype.toJSON = function()
203 {
204     return { "r": this.red, "g": this.green, "b": this.blue };
205 };
206 
207 /**
208  * Creates a new color instance with the color components read from the
209  * specified JSON object (with keys 'r', 'g' and 'b'). Returns null if
210  * data was empty
211  * 
212  * @param {Object} data
213  *            The color as JSON object
214  * @return {threedee.Color} The color object or null if data was empty
215  */
216 threedee.Color.fromJSON = function(data)
217 {
218     if (!data) return null;
219     return new threedee.Color(+data["r"], +data["g"], +data["b"]);
220 };
221