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