Subversion Repositories Applications.papyrus

Rev

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