1 /***************************************
  2  * Copyright 2011, 2012 GlobWeb contributors.
  3  *
  4  * This file is part of GlobWeb.
  5  *
  6  * GlobWeb is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published by
  8  * the Free Software Foundation, version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * GlobWeb is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
 18  ***************************************/
 19 
 20  define( function() {
 21  
 22  
 23 // Simple colors
 24 var simple_colors = {
 25    aliceblue: 'f0f8ff',
 26    antiquewhite: 'faebd7',
 27    aqua: '00ffff',
 28    aquamarine: '7fffd4',
 29    azure: 'f0ffff',
 30    beige: 'f5f5dc',
 31    bisque: 'ffe4c4',
 32    black: '000000',
 33    blanchedalmond: 'ffebcd',
 34    blue: '0000ff',
 35    blueviolet: '8a2be2',
 36    brown: 'a52a2a',
 37    burlywood: 'deb887',
 38    cadetblue: '5f9ea0',
 39    chartreuse: '7fff00',
 40    chocolate: 'd2691e',
 41    coral: 'ff7f50',
 42    cornflowerblue: '6495ed',
 43    cornsilk: 'fff8dc',
 44    crimson: 'dc143c',
 45    cyan: '00ffff',
 46    darkblue: '00008b',
 47    darkcyan: '008b8b',
 48    darkgoldenrod: 'b8860b',
 49    darkgray: 'a9a9a9',
 50    darkgreen: '006400',
 51    darkkhaki: 'bdb76b',
 52    darkmagenta: '8b008b',
 53    darkolivegreen: '556b2f',
 54    darkorange: 'ff8c00',
 55    darkorchid: '9932cc',
 56    darkred: '8b0000',
 57    darksalmon: 'e9967a',
 58    darkseagreen: '8fbc8f',
 59    darkslateblue: '483d8b',
 60    darkslategray: '2f4f4f',
 61    darkturquoise: '00ced1',
 62    darkviolet: '9400d3',
 63    deeppink: 'ff1493',
 64    deepskyblue: '00bfff',
 65    dimgray: '696969',
 66    dodgerblue: '1e90ff',
 67    feldspar: 'd19275',
 68    firebrick: 'b22222',
 69    floralwhite: 'fffaf0',
 70    forestgreen: '228b22',
 71    fuchsia: 'ff00ff',
 72    gainsboro: 'dcdcdc',
 73    ghostwhite: 'f8f8ff',
 74    gold: 'ffd700',
 75    goldenrod: 'daa520',
 76    gray: '808080',
 77    green: '008000',
 78    greenyellow: 'adff2f',
 79    honeydew: 'f0fff0',
 80    hotpink: 'ff69b4',
 81    indianred : 'cd5c5c',
 82    indigo : '4b0082',
 83    ivory: 'fffff0',
 84    khaki: 'f0e68c',
 85    lavender: 'e6e6fa',
 86    lavenderblush: 'fff0f5',
 87    lawngreen: '7cfc00',
 88    lemonchiffon: 'fffacd',
 89    lightblue: 'add8e6',
 90    lightcoral: 'f08080',
 91    lightcyan: 'e0ffff',
 92    lightgoldenrodyellow: 'fafad2',
 93    lightgrey: 'd3d3d3',
 94    lightgreen: '90ee90',
 95    lightpink: 'ffb6c1',
 96    lightsalmon: 'ffa07a',
 97    lightseagreen: '20b2aa',
 98    lightskyblue: '87cefa',
 99    lightslateblue: '8470ff',
100    lightslategray: '778899',
101    lightsteelblue: 'b0c4de',
102    lightyellow: 'ffffe0',
103    lime: '00ff00',
104    limegreen: '32cd32',
105    linen: 'faf0e6',
106    magenta: 'ff00ff',
107    maroon: '800000',
108    mediumaquamarine: '66cdaa',
109    mediumblue: '0000cd',
110    mediumorchid: 'ba55d3',
111    mediumpurple: '9370d8',
112    mediumseagreen: '3cb371',
113    mediumslateblue: '7b68ee',
114    mediumspringgreen: '00fa9a',
115    mediumturquoise: '48d1cc',
116    mediumvioletred: 'c71585',
117    midnightblue: '191970',
118    mintcream: 'f5fffa',
119    mistyrose: 'ffe4e1',
120    moccasin: 'ffe4b5',
121    navajowhite: 'ffdead',
122    navy: '000080',
123    oldlace: 'fdf5e6',
124    olive: '808000',
125    olivedrab: '6b8e23',
126    orange: 'ffa500',
127    orangered: 'ff4500',
128    orchid: 'da70d6',
129    palegoldenrod: 'eee8aa',
130    palegreen: '98fb98',
131    paleturquoise: 'afeeee',
132    palevioletred: 'd87093',
133    papayawhip: 'ffefd5',
134    peachpuff: 'ffdab9',
135    peru: 'cd853f',
136    pink: 'ffc0cb',
137    plum: 'dda0dd',
138    powderblue: 'b0e0e6',
139    purple: '800080',
140    red: 'ff0000',
141    rosybrown: 'bc8f8f',
142    royalblue: '4169e1',
143    saddlebrown: '8b4513',
144    salmon: 'fa8072',
145    sandybrown: 'f4a460',
146    seagreen: '2e8b57',
147    seashell: 'fff5ee',
148    sienna: 'a0522d',
149    silver: 'c0c0c0',
150    skyblue: '87ceeb',
151    slateblue: '6a5acd',
152    slategray: '708090',
153    snow: 'fffafa',
154    springgreen: '00ff7f',
155    steelblue: '4682b4',
156    tan: 'd2b48c',
157    teal: '008080',
158    thistle: 'd8bfd8',
159    tomato: 'ff6347',
160    turquoise: '40e0d0',
161    violet: 'ee82ee',
162    violetred: 'd02090',
163    wheat: 'f5deb3',
164    white: 'ffffff',
165    whitesmoke: 'f5f5f5',
166    yellow: 'ffff00',
167    yellowgreen: '9acd32'
168 };
169 
170 var parseHex = /^(\w{2})(\w{2})(\w{2})$/;
171 var parseRgb = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/;
172 var parseRgba = /^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3},\s*(\d{1,3}))\)$/;
173 
174 
175 /**************************************************************************************************************/
176 
177 /** @name FeatureStyle
178 	@class
179 	The style to display a Feature
180 	@param style an object containing the following style properties
181 		<ul>
182 			<li>strokeColor : the color for line, or polygon outline</li>
183 			<li>strokeWidth : the width of a line</li>
184 			<li>fillColor : the color for solid polygon or point</li>
185 			<li>fillTextureUrl : the texture for solid polygon</li>
186 			<li>iconUrl : the icon to be used for point</li>
187 			<li>pointMaxSize : maximum size (in meter) for a point</li>
188 		</ul>
189  */
190 var FeatureStyle = function(style)
191 {
192 	// Color used for lines or polygon outline
193 	this.strokeColor = [1.0, 0.0, 0.0, 1.0];
194 	// Color used to full polygon
195 	this.fillColor = [1.0, 0.0, 0.0, 1.0];
196 	this.fillTextureUrl = null;
197     this.fillTexture = null;
198     this.fillShader = null;
199 	this.strokeWidth = 1;
200 	this.iconUrl = null;
201 	this.icon = null;
202 	this.label = null;
203 	this.textColor = [1.0, 1.0, 1.0, 1.0];
204 	this.fill = false;
205 	this.pointMaxSize = 40;
206 	this.opacity = 1.;
207 	
208 	if ( style )
209 	{
210 		for ( var s in style )
211 		{
212 			this[s] = style[s];
213 		}
214 	}
215 }
216 
217 
218 /**************************************************************************************************************/
219 
220 /**
221  * Convert a color from a string to RGB
222  */
223 FeatureStyle.fromStringToColor = function(color_string)
224 {
225 	var r = 0, g = 0, b = 0, a = 255;
226 	var match;
227 	
228 	color_string = color_string.trim();
229 	color_string = color_string.toLowerCase();
230 	// strip any leading #
231 	if (color_string.charAt(0) == '#') { // remove # if any
232 	   color_string = color_string.substr(1,6);
233 	}
234 	
235 	// Convert a litteral color to rgb string
236 	if ( simple_colors.hasOwnProperty(color_string) )
237 	{
238 		color_string = simple_colors[color_string];
239 	}
240 	
241 	match = parseHex.exec(color_string);
242 	if ( match )
243 	{
244 		r = parseInt(match[1],16);
245 		g = parseInt(match[2],16);
246 		b = parseInt(match[3],16);
247 	}
248 	
249 	match = parseRgb.exec(color_string);
250 	if ( match )
251 	{
252 		r = parseInt(match[1]);
253 		g = parseInt(match[2]);
254 		b = parseInt(match[3]);
255 	}
256 	
257 	match = parseRgba.exec(color_string);
258 	if ( match )
259 	{
260 		r = parseInt(match[1]);
261 		g = parseInt(match[2]);
262 		b = parseInt(match[3]);
263 		a = parseInt(match[4]);
264 	}
265 
266 	// validate/cleanup values
267 	r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
268 	g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
269 	b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
270 	a = (a < 0) ? 0 : ((a > 255) ? 255 : a);
271 
272 	return [r / 255.0, g / 255.0, b / 255.0, a / 255.0];
273 }
274 
275 /**************************************************************************************************************/
276 
277 /** 
278  * Convert an internal color to a string based color representation
279  */
280 FeatureStyle.fromColorToString = function(color)
281 {		
282    var hashColor = '#';
283    for ( var i=0; i<3; i++ )
284    {
285       var component = parseInt( color[i] * 255.0 ).toString(16)
286       hashColor += (component < 10) ? '0'+component : component;
287    }
288 
289 	return hashColor;
290 }
291 
292 /**************************************************************************************************************/
293 
294 /** 
295  * Check if a style is equals to render poly
296  */
297 FeatureStyle.prototype.isEqualForPoly = function(style)
298 {
299 	return this.fill == style.fill;
300 }
301 
302 /**************************************************************************************************************/
303 
304 /** 
305  * Check if a style is equals to render poly
306  */
307 FeatureStyle.prototype.isEqualForLine = function(style)
308 {
309 	return this.strokeColor[0] == style.strokeColor[0]
310 		&& this.strokeColor[1] == style.strokeColor[1]
311 		&& this.strokeColor[2] == style.strokeColor[2]
312 		&& this.strokeColor[3] == style.strokeColor[3]
313 		&& this.strokeWidth == style.strokeWidth;
314 }
315 
316 /**************************************************************************************************************/
317 
318 /** 
319  * Check if a style is equals to render point
320  */
321 FeatureStyle.prototype.isEqualForPoint = function(style)
322 {
323 	return this.iconUrl == style.iconUrl
324 		&& this.icon == style.icon
325 		&& this.label == style.label;
326 }
327 
328 /**************************************************************************************************************/
329 
330 return FeatureStyle;
331 
332 });
333 
334