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.hsl");
14
dojo.require("dojo.lang.array");
15
dojo.lang.extend(dojo.gfx.color.Color, {toHsl:function () {
16
	return dojo.gfx.color.rgb2hsl(this.toRgb());
17
}});
18
dojo.gfx.color.rgb2hsl = function (r, g, b) {
19
	if (dojo.lang.isArray(r)) {
20
		b = r[2] || 0;
21
		g = r[1] || 0;
22
		r = r[0] || 0;
23
	}
24
	r /= 255;
25
	g /= 255;
26
	b /= 255;
27
	var h = null;
28
	var s = null;
29
	var l = null;
30
	var min = Math.min(r, g, b);
31
	var max = Math.max(r, g, b);
32
	var delta = max - min;
33
	l = (min + max) / 2;
34
	s = 0;
35
	if ((l > 0) && (l < 1)) {
36
		s = delta / ((l < 0.5) ? (2 * l) : (2 - 2 * l));
37
	}
38
	h = 0;
39
	if (delta > 0) {
40
		if ((max == r) && (max != g)) {
41
			h += (g - b) / delta;
42
		}
43
		if ((max == g) && (max != b)) {
44
			h += (2 + (b - r) / delta);
45
		}
46
		if ((max == b) && (max != r)) {
47
			h += (4 + (r - g) / delta);
48
		}
49
		h *= 60;
50
	}
51
	h = (h == 0) ? 360 : Math.ceil((h / 360) * 255);
52
	s = Math.ceil(s * 255);
53
	l = Math.ceil(l * 255);
54
	return [h, s, l];
55
};
56
dojo.gfx.color.hsl2rgb = function (h, s, l) {
57
	if (dojo.lang.isArray(h)) {
58
		l = h[2] || 0;
59
		s = h[1] || 0;
60
		h = h[0] || 0;
61
	}
62
	h = (h / 255) * 360;
63
	if (h == 360) {
64
		h = 0;
65
	}
66
	s = s / 255;
67
	l = l / 255;
68
	while (h < 0) {
69
		h += 360;
70
	}
71
	while (h > 360) {
72
		h -= 360;
73
	}
74
	var r, g, b;
75
	if (h < 120) {
76
		r = (120 - h) / 60;
77
		g = h / 60;
78
		b = 0;
79
	} else {
80
		if (h < 240) {
81
			r = 0;
82
			g = (240 - h) / 60;
83
			b = (h - 120) / 60;
84
		} else {
85
			r = (h - 240) / 60;
86
			g = 0;
87
			b = (360 - h) / 60;
88
		}
89
	}
90
	r = Math.min(r, 1);
91
	g = Math.min(g, 1);
92
	b = Math.min(b, 1);
93
	r = 2 * s * r + (1 - s);
94
	g = 2 * s * g + (1 - s);
95
	b = 2 * s * b + (1 - s);
96
	if (l < 0.5) {
97
		r = l * r;
98
		g = l * g;
99
		b = l * b;
100
	} else {
101
		r = (1 - l) * r + 2 * l - 1;
102
		g = (1 - l) * g + 2 * l - 1;
103
		b = (1 - l) * b + 2 * l - 1;
104
	}
105
	r = Math.ceil(r * 255);
106
	g = Math.ceil(g * 255);
107
	b = Math.ceil(b * 255);
108
	return [r, g, b];
109
};
110
dojo.gfx.color.hsl2hex = function (h, s, l) {
111
	var rgb = dojo.gfx.color.hsl2rgb(h, s, l);
112
	return dojo.gfx.color.rgb2hex(rgb[0], rgb[1], rgb[2]);
113
};
114
dojo.gfx.color.hex2hsl = function (hex) {
115
	var rgb = dojo.gfx.color.hex2rgb(hex);
116
	return dojo.gfx.color.rgb2hsl(rgb[0], rgb[1], rgb[2]);
117
};
118