Subversion Repositories Applications.papyrus

Rev

Details | 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.Colorspace");
14
dojo.require("dojo.lang.common");
15
dojo.require("dojo.math.matrix");
16
dojo.gfx.Colorspace = function () {
17
	this.whitePoint = "D65";
18
	this.stdObserver = "10";
19
	this.chromaticAdaptationAlg = "bradford";
20
	this.RGBWorkingSpace = "s_rgb";
21
	this.useApproxCIELabMapping = 1;
22
	this.chainMaps = {"RGB_to_xyY":["XYZ"], "xyY_to_RGB":["XYZ"], "RGB_to_Lab":["XYZ"], "Lab_to_RGB":["XYZ"], "RGB_to_LCHab":["XYZ", "Lab"], "LCHab_to_RGB":["Lab"], "xyY_to_Lab":["XYZ"], "Lab_to_xyY":["XYZ"], "XYZ_to_LCHab":["Lab"], "LCHab_to_XYZ":["Lab"], "xyY_to_LCHab":["XYZ", "Lab"], "LCHab_to_xyY":["Lab", "XYZ"], "RGB_to_Luv":["XYZ"], "Luv_to_RGB":["XYZ"], "xyY_to_Luv":["XYZ"], "Luv_to_xyY":["XYZ"], "Lab_to_Luv":["XYZ"], "Luv_to_Lab":["XYZ"], "LCHab_to_Luv":["Lab", "XYZ"], "Luv_to_LCHab":["XYZ", "Lab"], "RGB_to_LCHuv":["XYZ", "Luv"], "LCHuv_to_RGB":["Luv", "XYZ"], "XYZ_to_LCHuv":["Luv"], "LCHuv_to_XYZ":["Luv"], "xyY_to_LCHuv":["XYZ", "Luv"], "LCHuv_to_xyY":["Luv", "XYZ"], "Lab_to_LCHuv":["XYZ", "Luv"], "LCHuv_to_Lab":["Luv", "XYZ"], "LCHab_to_LCHuv":["Lab", "XYZ", "Luv"], "LCHuv_to_LCHab":["Luv", "XYZ", "Lab"], "XYZ_to_CMY":["RGB"], "CMY_to_XYZ":["RGB"], "xyY_to_CMY":["RGB"], "CMY_to_xyY":["RGB"], "Lab_to_CMY":["RGB"], "CMY_to_Lab":["RGB"], "LCHab_to_CMY":["RGB"], "CMY_to_LCHab":["RGB"], "Luv_to_CMY":["RGB"], "CMY_to_Luv":["RGB"], "LCHuv_to_CMY":["RGB"], "CMY_to_LCHuv":["RGB"], "XYZ_to_HSL":["RGB"], "HSL_to_XYZ":["RGB"], "xyY_to_HSL":["RGB"], "HSL_to_xyY":["RGB"], "Lab_to_HSL":["RGB"], "HSL_to_Lab":["RGB"], "LCHab_to_HSL":["RGB"], "HSL_to_LCHab":["RGB"], "Luv_to_HSL":["RGB"], "HSL_to_Luv":["RGB"], "LCHuv_to_HSL":["RGB"], "HSL_to_LCHuv":["RGB"], "CMY_to_HSL":["RGB"], "HSL_to_CMY":["RGB"], "CMYK_to_HSL":["RGB"], "HSL_to_CMYK":["RGB"], "XYZ_to_HSV":["RGB"], "HSV_to_XYZ":["RGB"], "xyY_to_HSV":["RGB"], "HSV_to_xyY":["RGB"], "Lab_to_HSV":["RGB"], "HSV_to_Lab":["RGB"], "LCHab_to_HSV":["RGB"], "HSV_to_LCHab":["RGB"], "Luv_to_HSV":["RGB"], "HSV_to_Luv":["RGB"], "LCHuv_to_HSV":["RGB"], "HSV_to_LCHuv":["RGB"], "CMY_to_HSV":["RGB"], "HSV_to_CMY":["RGB"], "CMYK_to_HSV":["RGB"], "HSV_to_CMYK":["RGB"], "HSL_to_HSV":["RGB"], "HSV_to_HSL":["RGB"], "XYZ_to_CMYK":["RGB"], "CMYK_to_XYZ":["RGB"], "xyY_to_CMYK":["RGB"], "CMYK_to_xyY":["RGB"], "Lab_to_CMYK":["RGB"], "CMYK_to_Lab":["RGB"], "LCHab_to_CMYK":["RGB"], "CMYK_to_LCHab":["RGB"], "Luv_to_CMYK":["RGB"], "CMYK_to_Luv":["RGB"], "LCHuv_to_CMYK":["RGB"], "CMYK_to_LCHuv":["RGB"]};
23
	return this;
24
};
25
dojo.gfx.Colorspace.prototype.convert = function (col, model_from, model_to) {
26
	var k = model_from + "_to_" + model_to;
27
	if (this[k]) {
28
		return this[k](col);
29
	} else {
30
		if (this.chainMaps[k]) {
31
			var cur = model_from;
32
			var models = this.chainMaps[k].concat();
33
			models.push(model_to);
34
			for (var i = 0; i < models.length; i++) {
35
				col = this.convert(col, cur, models[i]);
36
				cur = models[i];
37
			}
38
			return col;
39
		} else {
40
			dojo.debug("Can't convert from " + model_from + " to " + model_to);
41
		}
42
	}
43
};
44
dojo.gfx.Colorspace.prototype.munge = function (keys, args) {
45
	if (dojo.lang.isArray(args[0])) {
46
		args = args[0];
47
	}
48
	var out = new Array();
49
	for (var i = 0; i < keys.length; i++) {
50
		out[keys.charAt(i)] = args[i];
51
	}
52
	return out;
53
};
54
dojo.gfx.Colorspace.prototype.getWhitePoint = function () {
55
	var x = 0;
56
	var y = 0;
57
	var t = 0;
58
	switch (this.stdObserver) {
59
	  case "2":
60
		switch (this.whitePoint) {
61
		  case "E":
62
			x = 1 / 3;
63
			y = 1 / 3;
64
			t = 5400;
65
			break;
66
		  case "D50":
67
			x = 0.34567;
68
			y = 0.3585;
69
			t = 5000;
70
			break;
71
		  case "D55":
72
			x = 0.33242;
73
			y = 0.34743;
74
			t = 5500;
75
			break;
76
		  case "D65":
77
			x = 0.31271;
78
			y = 0.32902;
79
			t = 6500;
80
			break;
81
		  case "D75":
82
			x = 0.29902;
83
			y = 0.31485;
84
			t = 7500;
85
			break;
86
		  case "A":
87
			x = 0.44757;
88
			y = 0.40745;
89
			t = 2856;
90
			break;
91
		  case "B":
92
			x = 0.34842;
93
			y = 0.35161;
94
			t = 4874;
95
			break;
96
		  case "C":
97
			x = 0.31006;
98
			y = 0.31616;
99
			t = 6774;
100
			break;
101
		  case "9300":
102
			x = 0.2848;
103
			y = 0.2932;
104
			t = 9300;
105
			break;
106
		  case "F2":
107
			x = 0.37207;
108
			y = 0.37512;
109
			t = 4200;
110
			break;
111
		  case "F7":
112
			x = 0.31285;
113
			y = 0.32918;
114
			t = 6500;
115
			break;
116
		  case "F11":
117
			x = 0.38054;
118
			y = 0.37691;
119
			t = 4000;
120
			break;
121
		  default:
122
			dojo.debug("White point " + this.whitePoint + " isn't defined for Std. Observer " + this.strObserver);
123
		}
124
		break;
125
	  case "10":
126
		switch (this.whitePoint) {
127
		  case "E":
128
			x = 1 / 3;
129
			y = 1 / 3;
130
			t = 5400;
131
			break;
132
		  case "D50":
133
			x = 0.34773;
134
			y = 0.35952;
135
			t = 5000;
136
			break;
137
		  case "D55":
138
			x = 0.33411;
139
			y = 0.34877;
140
			t = 5500;
141
			break;
142
		  case "D65":
143
			x = 0.31382;
144
			y = 0.331;
145
			t = 6500;
146
			break;
147
		  case "D75":
148
			x = 0.29968;
149
			y = 0.3174;
150
			t = 7500;
151
			break;
152
		  case "A":
153
			x = 0.45117;
154
			y = 0.40594;
155
			t = 2856;
156
			break;
157
		  case "B":
158
			x = 0.3498;
159
			y = 0.3527;
160
			t = 4874;
161
			break;
162
		  case "C":
163
			x = 0.31039;
164
			y = 0.31905;
165
			t = 6774;
166
			break;
167
		  case "F2":
168
			x = 0.37928;
169
			y = 0.36723;
170
			t = 4200;
171
			break;
172
		  case "F7":
173
			x = 0.31565;
174
			y = 0.32951;
175
			t = 6500;
176
			break;
177
		  case "F11":
178
			x = 0.38543;
179
			y = 0.3711;
180
			t = 4000;
181
			break;
182
		  default:
183
			dojo.debug("White point " + this.whitePoint + " isn't defined for Std. Observer " + this.strObserver);
184
		}
185
		break;
186
	  default:
187
		dojo.debug("Std. Observer " + this.strObserver + " isn't defined");
188
	}
189
	var z = 1 - x - y;
190
	var wp = {"x":x, "y":y, "z":z, "t":t};
191
	wp.Y = 1;
192
	var XYZ = this.xyY_to_XYZ([wp.x, wp.y, wp.Y]);
193
	wp.X = XYZ[0];
194
	wp.Y = XYZ[1];
195
	wp.Z = XYZ[2];
196
	return wp;
197
};
198
dojo.gfx.Colorspace.prototype.getPrimaries = function () {
199
	var m = [];
200
	switch (this.RGBWorkingSpace) {
201
	  case "adobe_rgb_1998":
202
		m = [2.2, "D65", 0.64, 0.33, 0.297361, 0.21, 0.71, 0.627355, 0.15, 0.06, 0.075285];
203
		break;
204
	  case "apple_rgb":
205
		m = [1.8, "D65", 0.625, 0.34, 0.244634, 0.28, 0.595, 0.672034, 0.155, 0.07, 0.083332];
206
		break;
207
	  case "best_rgb":
208
		m = [2.2, "D50", 0.7347, 0.2653, 0.228457, 0.215, 0.775, 0.737352, 0.13, 0.035, 0.034191];
209
		break;
210
	  case "beta_rgb":
211
		m = [2.2, "D50", 0.6888, 0.3112, 0.303273, 0.1986, 0.7551, 0.663786, 0.1265, 0.0352, 0.032941];
212
		break;
213
	  case "bruce_rgb":
214
		m = [2.2, "D65", 0.64, 0.33, 0.240995, 0.28, 0.65, 0.683554, 0.15, 0.06, 0.075452];
215
		break;
216
	  case "cie_rgb":
217
		m = [2.2, "E", 0.735, 0.265, 0.176204, 0.274, 0.717, 0.812985, 0.167, 0.009, 0.010811];
218
		break;
219
	  case "color_match_rgb":
220
		m = [1.8, "D50", 0.63, 0.34, 0.274884, 0.295, 0.605, 0.658132, 0.15, 0.075, 0.066985];
221
		break;
222
	  case "don_rgb_4":
223
		m = [2.2, "D50", 0.696, 0.3, 0.27835, 0.215, 0.765, 0.68797, 0.13, 0.035, 0.03368];
224
		break;
225
	  case "eci_rgb":
226
		m = [1.8, "D50", 0.67, 0.33, 0.32025, 0.21, 0.71, 0.602071, 0.14, 0.08, 0.077679];
227
		break;
228
	  case "ekta_space_ps5":
229
		m = [2.2, "D50", 0.695, 0.305, 0.260629, 0.26, 0.7, 0.734946, 0.11, 0.005, 0.004425];
230
		break;
231
	  case "ntsc_rgb":
232
		m = [2.2, "C", 0.67, 0.33, 0.298839, 0.21, 0.71, 0.586811, 0.14, 0.08, 0.11435];
233
		break;
234
	  case "pal_secam_rgb":
235
		m = [2.2, "D65", 0.64, 0.33, 0.222021, 0.29, 0.6, 0.706645, 0.15, 0.06, 0.071334];
236
		break;
237
	  case "pro_photo_rgb":
238
		m = [1.8, "D50", 0.7347, 0.2653, 0.28804, 0.1596, 0.8404, 0.711874, 0.0366, 0.0001, 0.000086];
239
		break;
240
	  case "smpte-c_rgb":
241
		m = [2.2, "D65", 0.63, 0.34, 0.212395, 0.31, 0.595, 0.701049, 0.155, 0.07, 0.086556];
242
		break;
243
	  case "s_rgb":
244
		m = [2.2, "D65", 0.64, 0.33, 0.212656, 0.3, 0.6, 0.715158, 0.15, 0.06, 0.072186];
245
		break;
246
	  case "wide_gamut_rgb":
247
		m = [2.2, "D50", 0.735, 0.265, 0.258187, 0.115, 0.826, 0.724938, 0.157, 0.018, 0.016875];
248
		break;
249
	  default:
250
		dojo.debug("RGB working space " + this.RGBWorkingSpace + " isn't defined");
251
	}
252
	var p = {name:this.RGBWorkingSpace, gamma:m[0], wp:m[1], xr:m[2], yr:m[3], Yr:m[4], xg:m[5], yg:m[6], Yg:m[7], xb:m[8], yb:m[9], Yb:m[10]};
253
	if (p.wp != this.whitePoint) {
254
		var r = this.XYZ_to_xyY(this.chromaticAdaptation(this.xyY_to_XYZ([p.xr, p.yr, p.Yr]), p.wp, this.whitePoint));
255
		var g = this.XYZ_to_xyY(this.chromaticAdaptation(this.xyY_to_XYZ([p.xg, p.yg, p.Yg]), p.wp, this.whitePoint));
256
		var b = this.XYZ_to_xyY(this.chromaticAdaptation(this.xyY_to_XYZ([p.xb, p.yb, p.Yb]), p.wp, this.whitePoint));
257
		p.xr = r[0];
258
		p.yr = r[1];
259
		p.Yr = r[2];
260
		p.xg = g[0];
261
		p.yg = g[1];
262
		p.Yg = g[2];
263
		p.xb = b[0];
264
		p.yb = b[1];
265
		p.Yb = b[2];
266
		p.wp = this.whitePoint;
267
	}
268
	p.zr = 1 - p.xr - p.yr;
269
	p.zg = 1 - p.xg - p.yg;
270
	p.zb = 1 - p.xb - p.yb;
271
	return p;
272
};
273
dojo.gfx.Colorspace.prototype.epsilon = function () {
274
	return this.useApproxCIELabMapping ? 0.008856 : 216 / 24289;
275
};
276
dojo.gfx.Colorspace.prototype.kappa = function () {
277
	return this.useApproxCIELabMapping ? 903.3 : 24389 / 27;
278
};
279
dojo.gfx.Colorspace.prototype.XYZ_to_xyY = function () {
280
	var src = this.munge("XYZ", arguments);
281
	var sum = src.X + src.Y + src.Z;
282
	if (sum == 0) {
283
		var wp = this.getWhitePoint();
284
		var x = wp.x;
285
		var y = wp.y;
286
	} else {
287
		var x = src.X / sum;
288
		var y = src.Y / sum;
289
	}
290
	var Y = src.Y;
291
	return [x, y, Y];
292
};
293
dojo.gfx.Colorspace.prototype.xyY_to_XYZ = function () {
294
	var src = this.munge("xyY", arguments);
295
	if (src.y == 0) {
296
		var X = 0;
297
		var Y = 0;
298
		var Z = 0;
299
	} else {
300
		var X = (src.x * src.Y) / src.y;
301
		var Y = src.Y;
302
		var Z = ((1 - src.x - src.y) * src.Y) / src.y;
303
	}
304
	return [X, Y, Z];
305
};
306
dojo.gfx.Colorspace.prototype.RGB_to_XYZ = function () {
307
	var src = this.munge("RGB", arguments);
308
	var m = this.getRGB_XYZ_Matrix();
309
	var pr = this.getPrimaries();
310
	if (this.RGBWorkingSpace == "s_rgb") {
311
		var r = (src.R > 0.04045) ? Math.pow(((src.R + 0.055) / 1.055), 2.4) : src.R / 12.92;
312
		var g = (src.G > 0.04045) ? Math.pow(((src.G + 0.055) / 1.055), 2.4) : src.G / 12.92;
313
		var b = (src.B > 0.04045) ? Math.pow(((src.B + 0.055) / 1.055), 2.4) : src.B / 12.92;
314
	} else {
315
		var r = Math.pow(src.R, pr.gamma);
316
		var g = Math.pow(src.G, pr.gamma);
317
		var b = Math.pow(src.B, pr.gamma);
318
	}
319
	var XYZ = dojo.math.matrix.multiply([[r, g, b]], m);
320
	return [XYZ[0][0], XYZ[0][1], XYZ[0][2]];
321
};
322
dojo.gfx.Colorspace.prototype.XYZ_to_RGB = function () {
323
	var src = this.munge("XYZ", arguments);
324
	var mi = this.getXYZ_RGB_Matrix();
325
	var pr = this.getPrimaries();
326
	var rgb = dojo.math.matrix.multiply([[src.X, src.Y, src.Z]], mi);
327
	var r = rgb[0][0];
328
	var g = rgb[0][1];
329
	var b = rgb[0][2];
330
	if (this.RGBWorkingSpace == "s_rgb") {
331
		var R = (r > 0.0031308) ? (1.055 * Math.pow(r, 1 / 2.4)) - 0.055 : 12.92 * r;
332
		var G = (g > 0.0031308) ? (1.055 * Math.pow(g, 1 / 2.4)) - 0.055 : 12.92 * g;
333
		var B = (b > 0.0031308) ? (1.055 * Math.pow(b, 1 / 2.4)) - 0.055 : 12.92 * b;
334
	} else {
335
		var R = Math.pow(r, 1 / pr.gamma);
336
		var G = Math.pow(g, 1 / pr.gamma);
337
		var B = Math.pow(b, 1 / pr.gamma);
338
	}
339
	return [R, G, B];
340
};
341
dojo.gfx.Colorspace.prototype.XYZ_to_Lab = function () {
342
	var src = this.munge("XYZ", arguments);
343
	var wp = this.getWhitePoint();
344
	var xr = src.X / wp.X;
345
	var yr = src.Y / wp.Y;
346
	var zr = src.Z / wp.Z;
347
	var fx = (xr > this.epsilon()) ? Math.pow(xr, 1 / 3) : (this.kappa() * xr + 16) / 116;
348
	var fy = (yr > this.epsilon()) ? Math.pow(yr, 1 / 3) : (this.kappa() * yr + 16) / 116;
349
	var fz = (zr > this.epsilon()) ? Math.pow(zr, 1 / 3) : (this.kappa() * zr + 16) / 116;
350
	var L = 116 * fy - 16;
351
	var a = 500 * (fx - fy);
352
	var b = 200 * (fy - fz);
353
	return [L, a, b];
354
};
355
dojo.gfx.Colorspace.prototype.Lab_to_XYZ = function () {
356
	var src = this.munge("Lab", arguments);
357
	var wp = this.getWhitePoint();
358
	var yr = (src.L > (this.kappa() * this.epsilon())) ? Math.pow((src.L + 16) / 116, 3) : src.L / this.kappa();
359
	var fy = (yr > this.epsilon()) ? (src.L + 16) / 116 : (this.kappa() * yr + 16) / 116;
360
	var fx = (src.a / 500) + fy;
361
	var fz = fy - (src.b / 200);
362
	var fxcube = Math.pow(fx, 3);
363
	var fzcube = Math.pow(fz, 3);
364
	var xr = (fxcube > this.epsilon()) ? fxcube : (116 * fx - 16) / this.kappa();
365
	var zr = (fzcube > this.epsilon()) ? fzcube : (116 * fz - 16) / this.kappa();
366
	var X = xr * wp.X;
367
	var Y = yr * wp.Y;
368
	var Z = zr * wp.Z;
369
	return [X, Y, Z];
370
};
371
dojo.gfx.Colorspace.prototype.Lab_to_LCHab = function () {
372
	var src = this.munge("Lab", arguments);
373
	var L = src.L;
374
	var C = Math.pow(src.a * src.a + src.b * src.b, 0.5);
375
	var H = Math.atan2(src.b, src.a) * (180 / Math.PI);
376
	if (H < 0) {
377
		H += 360;
378
	}
379
	if (H > 360) {
380
		H -= 360;
381
	}
382
	return [L, C, H];
383
};
384
dojo.gfx.Colorspace.prototype.LCHab_to_Lab = function () {
385
	var src = this.munge("LCH", arguments);
386
	var H_rad = src.H * (Math.PI / 180);
387
	var L = src.L;
388
	var a = src.C / Math.pow(Math.pow(Math.tan(H_rad), 2) + 1, 0.5);
389
	if ((90 < src.H) && (src.H < 270)) {
390
		a = -a;
391
	}
392
	var b = Math.pow(Math.pow(src.C, 2) - Math.pow(a, 2), 0.5);
393
	if (src.H > 180) {
394
		b = -b;
395
	}
396
	return [L, a, b];
397
};
398
dojo.gfx.Colorspace.prototype.chromaticAdaptation = function (col, src_w, dst_w) {
399
	col = this.munge("XYZ", [col]);
400
	var old_wp = this.whitePoint;
401
	this.whitePoint = src_w;
402
	var wp_src = this.getWhitePoint();
403
	this.whitePoint = dst_w;
404
	var wp_dst = this.getWhitePoint();
405
	this.whitePoint = old_wp;
406
	switch (this.chromaticAdaptationAlg) {
407
	  case "xyz_scaling":
408
		var ma = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
409
		var mai = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
410
		break;
411
	  case "bradford":
412
		var ma = [[0.8951, -0.7502, 0.0389], [0.2664, 1.7135, -0.0685], [-0.1614, 0.0367, 1.0296]];
413
		var mai = [[0.986993, 0.432305, -0.008529], [-0.147054, 0.51836, 0.040043], [0.159963, 0.049291, 0.968487]];
414
		break;
415
	  case "von_kries":
416
		var ma = [[0.40024, -0.2263, 0], [0.7076, 1.16532, 0], [-0.08081, 0.0457, 0.91822]];
417
		var mai = [[1.859936, 0.361191, 0], [-1.129382, 0.638812, 0], [0.219897, -0.000006, 1.089064]];
418
		break;
419
	  default:
420
		dojo.debug("The " + this.chromaticAdaptationAlg + " chromatic adaptation algorithm matricies are not defined");
421
	}
422
	var domain_src = dojo.math.matrix.multiply([[wp_src.x, wp_src.y, wp_src.z]], ma);
423
	var domain_dst = dojo.math.matrix.multiply([[wp_dst.x, wp_dst.y, wp_dst.z]], ma);
424
	var centre = [[domain_dst[0][0] / domain_src[0][0], 0, 0], [0, domain_dst[0][1] / domain_src[0][1], 0], [0, 0, domain_dst[0][2] / domain_src[0][2]]];
425
	var m = dojo.math.matrix.multiply(dojo.math.matrix.multiply(ma, centre), mai);
426
	var dst = dojo.math.matrix.multiply([[col.X, col.Y, col.Z]], m);
427
	return dst[0];
428
};
429
dojo.gfx.Colorspace.prototype.getRGB_XYZ_Matrix = function () {
430
	var wp = this.getWhitePoint();
431
	var pr = this.getPrimaries();
432
	var Xr = pr.xr / pr.yr;
433
	var Yr = 1;
434
	var Zr = (1 - pr.xr - pr.yr) / pr.yr;
435
	var Xg = pr.xg / pr.yg;
436
	var Yg = 1;
437
	var Zg = (1 - pr.xg - pr.yg) / pr.yg;
438
	var Xb = pr.xb / pr.yb;
439
	var Yb = 1;
440
	var Zb = (1 - pr.xb - pr.yb) / pr.yb;
441
	var m1 = [[Xr, Yr, Zr], [Xg, Yg, Zg], [Xb, Yb, Zb]];
442
	var m2 = [[wp.X, wp.Y, wp.Z]];
443
	var sm = dojo.math.matrix.multiply(m2, dojo.math.matrix.inverse(m1));
444
	var Sr = sm[0][0];
445
	var Sg = sm[0][1];
446
	var Sb = sm[0][2];
447
	var m4 = [[Sr * Xr, Sr * Yr, Sr * Zr], [Sg * Xg, Sg * Yg, Sg * Zg], [Sb * Xb, Sb * Yb, Sb * Zb]];
448
	return m4;
449
};
450
dojo.gfx.Colorspace.prototype.getXYZ_RGB_Matrix = function () {
451
	var m = this.getRGB_XYZ_Matrix();
452
	return dojo.math.matrix.inverse(m);
453
};
454
dojo.gfx.Colorspace.prototype.XYZ_to_Luv = function () {
455
	var src = this.munge("XYZ", arguments);
456
	var wp = this.getWhitePoint();
457
	var ud = (4 * src.X) / (src.X + 15 * src.Y + 3 * src.Z);
458
	var vd = (9 * src.Y) / (src.X + 15 * src.Y + 3 * src.Z);
459
	var udr = (4 * wp.X) / (wp.X + 15 * wp.Y + 3 * wp.Z);
460
	var vdr = (9 * wp.Y) / (wp.X + 15 * wp.Y + 3 * wp.Z);
461
	var yr = src.Y / wp.Y;
462
	var L = (yr > this.epsilon()) ? 116 * Math.pow(yr, 1 / 3) - 16 : this.kappa() * yr;
463
	var u = 13 * L * (ud - udr);
464
	var v = 13 * L * (vd - vdr);
465
	return [L, u, v];
466
};
467
dojo.gfx.Colorspace.prototype.Luv_to_XYZ = function () {
468
	var src = this.munge("Luv", arguments);
469
	var wp = this.getWhitePoint();
470
	var uz = (4 * wp.X) / (wp.X + 15 * wp.Y + 3 * wp.Z);
471
	var vz = (9 * wp.Y) / (wp.X + 15 * wp.Y + 3 * wp.Z);
472
	var Y = (src.L > this.kappa() * this.epsilon()) ? Math.pow((src.L + 16) / 116, 3) : src.L / this.kappa();
473
	var a = (1 / 3) * (((52 * src.L) / (src.u + 13 * src.L * uz)) - 1);
474
	var b = -5 * Y;
475
	var c = -(1 / 3);
476
	var d = Y * (((39 * src.L) / (src.v + 13 * src.L * vz)) - 5);
477
	var X = (d - b) / (a - c);
478
	var Z = X * a + b;
479
	return [X, Y, Z];
480
};
481
dojo.gfx.Colorspace.prototype.Luv_to_LCHuv = function () {
482
	var src = this.munge("Luv", arguments);
483
	var L = src.L;
484
	var C = Math.pow(src.u * src.u + src.v * src.v, 0.5);
485
	var H = Math.atan2(src.v, src.u) * (180 / Math.PI);
486
	if (H < 0) {
487
		H += 360;
488
	}
489
	if (H > 360) {
490
		H -= 360;
491
	}
492
	return [L, C, H];
493
};
494
dojo.gfx.Colorspace.prototype.LCHuv_to_Luv = function () {
495
	var src = this.munge("LCH", arguments);
496
	var H_rad = src.H * (Math.PI / 180);
497
	var L = src.L;
498
	var u = src.C / Math.pow(Math.pow(Math.tan(H_rad), 2) + 1, 0.5);
499
	var v = Math.pow(src.C * src.C - u * u, 0.5);
500
	if ((90 < src.H) && (src.H < 270)) {
501
		u *= -1;
502
	}
503
	if (src.H > 180) {
504
		v *= -1;
505
	}
506
	return [L, u, v];
507
};
508
dojo.gfx.Colorspace.colorTemp_to_whitePoint = function (T) {
509
	if (T < 4000) {
510
		dojo.debug("Can't find a white point for temperatures under 4000K");
511
		return [0, 0];
512
	}
513
	if (T > 25000) {
514
		dojo.debug("Can't find a white point for temperatures over 25000K");
515
		return [0, 0];
516
	}
517
	var T1 = T;
518
	var T2 = T * T;
519
	var T3 = T2 * T;
520
	var ten9 = Math.pow(10, 9);
521
	var ten6 = Math.pow(10, 6);
522
	var ten3 = Math.pow(10, 3);
523
	if (T <= 7000) {
524
		var x = (-4.607 * ten9 / T3) + (2.9678 * ten6 / T2) + (0.09911 * ten3 / T) + 0.244063;
525
	} else {
526
		var x = (-2.0064 * ten9 / T3) + (1.9018 * ten6 / T2) + (0.24748 * ten3 / T) + 0.23704;
527
	}
528
	var y = -3 * x * x + 2.87 * x - 0.275;
529
	return [x, y];
530
};
531
dojo.gfx.Colorspace.prototype.RGB_to_CMY = function () {
532
	var src = this.munge("RGB", arguments);
533
	var C = 1 - src.R;
534
	var M = 1 - src.G;
535
	var Y = 1 - src.B;
536
	return [C, M, Y];
537
};
538
dojo.gfx.Colorspace.prototype.CMY_to_RGB = function () {
539
	var src = this.munge("CMY", arguments);
540
	var R = 1 - src.C;
541
	var G = 1 - src.M;
542
	var B = 1 - src.Y;
543
	return [R, G, B];
544
};
545
dojo.gfx.Colorspace.prototype.RGB_to_CMYK = function () {
546
	var src = this.munge("RGB", arguments);
547
	var K = Math.min(1 - src.R, 1 - src.G, 1 - src.B);
548
	var C = (1 - src.R - K) / (1 - K);
549
	var M = (1 - src.G - K) / (1 - K);
550
	var Y = (1 - src.B - K) / (1 - K);
551
	return [C, M, Y, K];
552
};
553
dojo.gfx.Colorspace.prototype.CMYK_to_RGB = function () {
554
	var src = this.munge("CMYK", arguments);
555
	var R = 1 - Math.min(1, src.C * (1 - src.K) + src.K);
556
	var G = 1 - Math.min(1, src.M * (1 - src.K) + src.K);
557
	var B = 1 - Math.min(1, src.Y * (1 - src.K) + src.K);
558
	return [R, G, B];
559
};
560
dojo.gfx.Colorspace.prototype.CMY_to_CMYK = function () {
561
	var src = this.munge("CMY", arguments);
562
	var K = Math.min(src.C, src.M, src.Y);
563
	var C = (src.C - K) / (1 - K);
564
	var M = (src.M - K) / (1 - K);
565
	var Y = (src.Y - K) / (1 - K);
566
	return [C, M, Y, K];
567
};
568
dojo.gfx.Colorspace.prototype.CMYK_to_CMY = function () {
569
	var src = this.munge("CMYK", arguments);
570
	var C = Math.min(1, src.C * (1 - src.K) + src.K);
571
	var M = Math.min(1, src.M * (1 - src.K) + src.K);
572
	var Y = Math.min(1, src.Y * (1 - src.K) + src.K);
573
	return [C, M, Y];
574
};
575
dojo.gfx.Colorspace.prototype.RGB_to_HSV = function () {
576
	var src = this.munge("RGB", arguments);
577
	var min = Math.min(src.R, src.G, src.B);
578
	var V = Math.max(src.R, src.G, src.B);
579
	var delta = V - min;
580
	var H = null;
581
	var S = (V == 0) ? 0 : delta / V;
582
	if (S == 0) {
583
		H = 0;
584
	} else {
585
		if (src.R == V) {
586
			H = 60 * (src.G - src.B) / delta;
587
		} else {
588
			if (src.G == V) {
589
				H = 120 + 60 * (src.B - src.R) / delta;
590
			} else {
591
				if (src.B == V) {
592
					H = 240 + 60 * (src.R - src.G) / delta;
593
				}
594
			}
595
		}
596
		if (H < 0) {
597
			H += 360;
598
		}
599
	}
600
	H = (H == 0) ? 360 : H;
601
	return [H, S, V];
602
};
603
dojo.gfx.Colorspace.prototype.HSV_to_RGB = function () {
604
	var src = this.munge("HSV", arguments);
605
	if (src.H == 360) {
606
		src.H = 0;
607
	}
608
	var r = null;
609
	var g = null;
610
	var b = null;
611
	if (src.S == 0) {
612
		var R = src.V;
613
		var G = src.V;
614
		var B = src.V;
615
	} else {
616
		var hTemp = src.H / 60;
617
		var i = Math.floor(hTemp);
618
		var f = hTemp - i;
619
		var p = src.V * (1 - src.S);
620
		var q = src.V * (1 - (src.S * f));
621
		var t = src.V * (1 - (src.S * (1 - f)));
622
		switch (i) {
623
		  case 0:
624
			R = src.V;
625
			G = t;
626
			B = p;
627
			break;
628
		  case 1:
629
			R = q;
630
			G = src.V;
631
			B = p;
632
			break;
633
		  case 2:
634
			R = p;
635
			G = src.V;
636
			B = t;
637
			break;
638
		  case 3:
639
			R = p;
640
			G = q;
641
			B = src.V;
642
			break;
643
		  case 4:
644
			R = t;
645
			G = p;
646
			B = src.V;
647
			break;
648
		  case 5:
649
			R = src.V;
650
			G = p;
651
			B = q;
652
			break;
653
		}
654
	}
655
	return [R, G, B];
656
};
657
dojo.gfx.Colorspace.prototype.RGB_to_HSL = function () {
658
	var src = this.munge("RGB", arguments);
659
	var min = Math.min(src.R, src.G, src.B);
660
	var max = Math.max(src.R, src.G, src.B);
661
	var delta = max - min;
662
	var H = 0;
663
	var S = 0;
664
	var L = (min + max) / 2;
665
	if ((L > 0) && (L < 1)) {
666
		S = delta / ((L < 0.5) ? (2 * L) : (2 - 2 * L));
667
	}
668
	if (delta > 0) {
669
		if ((max == src.R) && (max != src.G)) {
670
			H += (src.G - src.B) / delta;
671
		}
672
		if ((max == src.G) && (max != src.B)) {
673
			H += (2 + (src.B - src.R) / delta);
674
		}
675
		if ((max == src.B) && (max != src.R)) {
676
			H += (4 + (src.R - src.G) / delta);
677
		}
678
		H *= 60;
679
	}
680
	H = (H == 0) ? 360 : H;
681
	return [H, S, L];
682
};
683
dojo.gfx.Colorspace.prototype.HSL_to_RGB = function () {
684
	var src = this.munge("HSL", arguments);
685
	while (src.H < 0) {
686
		src.H += 360;
687
	}
688
	while (src.H >= 360) {
689
		src.H -= 360;
690
	}
691
	var R = 0;
692
	var G = 0;
693
	var B = 0;
694
	if (src.H < 120) {
695
		R = (120 - src.H) / 60;
696
		G = src.H / 60;
697
		B = 0;
698
	} else {
699
		if (src.H < 240) {
700
			R = 0;
701
			G = (240 - src.H) / 60;
702
			B = (src.H - 120) / 60;
703
		} else {
704
			R = (src.H - 240) / 60;
705
			G = 0;
706
			B = (360 - src.H) / 60;
707
		}
708
	}
709
	R = 2 * src.S * Math.min(R, 1) + (1 - src.S);
710
	G = 2 * src.S * Math.min(G, 1) + (1 - src.S);
711
	B = 2 * src.S * Math.min(B, 1) + (1 - src.S);
712
	if (src.L < 0.5) {
713
		R = src.L * R;
714
		G = src.L * G;
715
		B = src.L * B;
716
	} else {
717
		R = (1 - src.L) * R + 2 * src.L - 1;
718
		G = (1 - src.L) * G + 2 * src.L - 1;
719
		B = (1 - src.L) * B + 2 * src.L - 1;
720
	}
721
	return [R, G, B];
722
};
723