Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.gfx.color.hsv");
14
dojo.require("dojo.lang.array");
15
dojo.require("dojo.math");
16
dojo.lang.extend(dojo.gfx.color.Color, {toHsv:function () {
17
	return dojo.gfx.color.rgb2hsv(this.toRgb());
18
}});
19
dojo.gfx.color.rgb2hsv = function (r, g, b, options) {
20
	if (dojo.lang.isArray(r)) {
21
		if (g) {
22
			options = g;
23
		}
24
		b = r[2] || 0;
25
		g = r[1] || 0;
26
		r = r[0] || 0;
27
	}
28
	var opt = {inputRange:(options && options.inputRange) ? options.inputRange : 255, outputRange:(options && options.outputRange) ? options.outputRange : [255, 255, 255]};
29
	var h = null;
30
	var s = null;
31
	var v = null;
32
	switch (opt.inputRange) {
33
	  case 1:
34
		r = (r * 255);
35
		g = (g * 255);
36
		b = (b * 255);
37
		break;
38
	  case 100:
39
		r = (r / 100) * 255;
40
		g = (g / 100) * 255;
41
		b = (b / 100) * 255;
42
		break;
43
	  default:
44
		break;
45
	}
46
	var min = Math.min(r, g, b);
47
	v = Math.max(r, g, b);
48
	var delta = v - min;
49
	s = (v == 0) ? 0 : delta / v;
50
	if (s == 0) {
51
		h = 0;
52
	} else {
53
		if (r == v) {
54
			h = 60 * (g - b) / delta;
55
		} else {
56
			if (g == v) {
57
				h = 120 + 60 * (b - r) / delta;
58
			} else {
59
				if (b == v) {
60
					h = 240 + 60 * (r - g) / delta;
61
				}
62
			}
63
		}
64
		if (h <= 0) {
65
			h += 360;
66
		}
67
	}
68
	switch (opt.outputRange[0]) {
69
	  case 360:
70
		break;
71
	  case 100:
72
		h = (h / 360) * 100;
73
		break;
74
	  case 1:
75
		h = (h / 360);
76
		break;
77
	  default:
78
		h = (h / 360) * 255;
79
		break;
80
	}
81
	switch (opt.outputRange[1]) {
82
	  case 100:
83
		s = s * 100;
84
	  case 1:
85
		break;
86
	  default:
87
		s = s * 255;
88
		break;
89
	}
90
	switch (opt.outputRange[2]) {
91
	  case 100:
92
		v = (v / 255) * 100;
93
		break;
94
	  case 1:
95
		v = (v / 255);
96
		break;
97
	  default:
98
		break;
99
	}
100
	h = dojo.math.round(h);
101
	s = dojo.math.round(s);
102
	v = dojo.math.round(v);
103
	return [h, s, v];
104
};
105
dojo.gfx.color.hsv2rgb = function (h, s, v, options) {
106
	if (dojo.lang.isArray(h)) {
107
		if (s) {
108
			options = s;
109
		}
110
		v = h[2] || 0;
111
		s = h[1] || 0;
112
		h = h[0] || 0;
113
	}
114
	var opt = {inputRange:(options && options.inputRange) ? options.inputRange : [255, 255, 255], outputRange:(options && options.outputRange) ? options.outputRange : 255};
115
	switch (opt.inputRange[0]) {
116
	  case 1:
117
		h = h * 360;
118
		break;
119
	  case 100:
120
		h = (h / 100) * 360;
121
		break;
122
	  case 360:
123
		h = h;
124
		break;
125
	  default:
126
		h = (h / 255) * 360;
127
	}
128
	if (h == 360) {
129
		h = 0;
130
	}
131
	switch (opt.inputRange[1]) {
132
	  case 100:
133
		s /= 100;
134
		break;
135
	  case 255:
136
		s /= 255;
137
	}
138
	switch (opt.inputRange[2]) {
139
	  case 100:
140
		v /= 100;
141
		break;
142
	  case 255:
143
		v /= 255;
144
	}
145
	var r = null;
146
	var g = null;
147
	var b = null;
148
	if (s == 0) {
149
		r = v;
150
		g = v;
151
		b = v;
152
	} else {
153
		var hTemp = h / 60;
154
		var i = Math.floor(hTemp);
155
		var f = hTemp - i;
156
		var p = v * (1 - s);
157
		var q = v * (1 - (s * f));
158
		var t = v * (1 - (s * (1 - f)));
159
		switch (i) {
160
		  case 0:
161
			r = v;
162
			g = t;
163
			b = p;
164
			break;
165
		  case 1:
166
			r = q;
167
			g = v;
168
			b = p;
169
			break;
170
		  case 2:
171
			r = p;
172
			g = v;
173
			b = t;
174
			break;
175
		  case 3:
176
			r = p;
177
			g = q;
178
			b = v;
179
			break;
180
		  case 4:
181
			r = t;
182
			g = p;
183
			b = v;
184
			break;
185
		  case 5:
186
			r = v;
187
			g = p;
188
			b = q;
189
			break;
190
		}
191
	}
192
	switch (opt.outputRange) {
193
	  case 1:
194
		r = dojo.math.round(r, 2);
195
		g = dojo.math.round(g, 2);
196
		b = dojo.math.round(b, 2);
197
		break;
198
	  case 100:
199
		r = Math.round(r * 100);
200
		g = Math.round(g * 100);
201
		b = Math.round(b * 100);
202
		break;
203
	  default:
204
		r = Math.round(r * 255);
205
		g = Math.round(g * 255);
206
		b = Math.round(b * 255);
207
	}
208
	return [r, g, b];
209
};
210