Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | 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");
14
dojo.require("dojo.lang.common");
15
dojo.require("dojo.lang.array");
16
dojo.gfx.color.Color = function (r, g, b, a) {
17
	if (dojo.lang.isArray(r)) {
18
		this.r = r[0];
19
		this.g = r[1];
20
		this.b = r[2];
21
		this.a = r[3] || 1;
22
	} else {
23
		if (dojo.lang.isString(r)) {
24
			var rgb = dojo.gfx.color.extractRGB(r);
25
			this.r = rgb[0];
26
			this.g = rgb[1];
27
			this.b = rgb[2];
28
			this.a = g || 1;
29
		} else {
30
			if (r instanceof dojo.gfx.color.Color) {
31
				this.r = r.r;
32
				this.b = r.b;
33
				this.g = r.g;
34
				this.a = r.a;
35
			} else {
36
				this.r = r;
37
				this.g = g;
38
				this.b = b;
39
				this.a = a;
40
			}
41
		}
42
	}
43
};
44
dojo.gfx.color.Color.fromArray = function (arr) {
45
	return new dojo.gfx.color.Color(arr[0], arr[1], arr[2], arr[3]);
46
};
47
dojo.extend(dojo.gfx.color.Color, {toRgb:function (includeAlpha) {
48
	if (includeAlpha) {
49
		return this.toRgba();
50
	} else {
51
		return [this.r, this.g, this.b];
52
	}
53
}, toRgba:function () {
54
	return [this.r, this.g, this.b, this.a];
55
}, toHex:function () {
56
	return dojo.gfx.color.rgb2hex(this.toRgb());
57
}, toCss:function () {
58
	return "rgb(" + this.toRgb().join() + ")";
59
}, toString:function () {
60
	return this.toHex();
61
}, blend:function (color, weight) {
62
	var rgb = null;
63
	if (dojo.lang.isArray(color)) {
64
		rgb = color;
65
	} else {
66
		if (color instanceof dojo.gfx.color.Color) {
67
			rgb = color.toRgb();
68
		} else {
69
			rgb = new dojo.gfx.color.Color(color).toRgb();
70
		}
71
	}
72
	return dojo.gfx.color.blend(this.toRgb(), rgb, weight);
73
}});
74
dojo.gfx.color.named = {white:[255, 255, 255], black:[0, 0, 0], red:[255, 0, 0], green:[0, 255, 0], lime:[0, 255, 0], blue:[0, 0, 255], navy:[0, 0, 128], gray:[128, 128, 128], silver:[192, 192, 192]};
75
dojo.gfx.color.blend = function (a, b, weight) {
76
	if (typeof a == "string") {
77
		return dojo.gfx.color.blendHex(a, b, weight);
78
	}
79
	if (!weight) {
80
		weight = 0;
81
	}
82
	weight = Math.min(Math.max(-1, weight), 1);
83
	weight = ((weight + 1) / 2);
84
	var c = [];
85
	for (var x = 0; x < 3; x++) {
86
		c[x] = parseInt(b[x] + ((a[x] - b[x]) * weight));
87
	}
88
	return c;
89
};
90
dojo.gfx.color.blendHex = function (a, b, weight) {
91
	return dojo.gfx.color.rgb2hex(dojo.gfx.color.blend(dojo.gfx.color.hex2rgb(a), dojo.gfx.color.hex2rgb(b), weight));
92
};
93
dojo.gfx.color.extractRGB = function (color) {
94
	var hex = "0123456789abcdef";
95
	color = color.toLowerCase();
96
	if (color.indexOf("rgb") == 0) {
97
		var matches = color.match(/rgba*\((\d+), *(\d+), *(\d+)/i);
98
		var ret = matches.splice(1, 3);
99
		return ret;
100
	} else {
101
		var colors = dojo.gfx.color.hex2rgb(color);
102
		if (colors) {
103
			return colors;
104
		} else {
105
			return dojo.gfx.color.named[color] || [255, 255, 255];
106
		}
107
	}
108
};
109
dojo.gfx.color.hex2rgb = function (hex) {
110
	var hexNum = "0123456789ABCDEF";
111
	var rgb = new Array(3);
112
	if (hex.indexOf("#") == 0) {
113
		hex = hex.substring(1);
114
	}
115
	hex = hex.toUpperCase();
116
	if (hex.replace(new RegExp("[" + hexNum + "]", "g"), "") != "") {
117
		return null;
118
	}
119
	if (hex.length == 3) {
120
		rgb[0] = hex.charAt(0) + hex.charAt(0);
121
		rgb[1] = hex.charAt(1) + hex.charAt(1);
122
		rgb[2] = hex.charAt(2) + hex.charAt(2);
123
	} else {
124
		rgb[0] = hex.substring(0, 2);
125
		rgb[1] = hex.substring(2, 4);
126
		rgb[2] = hex.substring(4);
127
	}
128
	for (var i = 0; i < rgb.length; i++) {
129
		rgb[i] = hexNum.indexOf(rgb[i].charAt(0)) * 16 + hexNum.indexOf(rgb[i].charAt(1));
130
	}
131
	return rgb;
132
};
133
dojo.gfx.color.rgb2hex = function (r, g, b) {
134
	if (dojo.lang.isArray(r)) {
135
		g = r[1] || 0;
136
		b = r[2] || 0;
137
		r = r[0] || 0;
138
	}
139
	var ret = dojo.lang.map([r, g, b], function (x) {
140
		x = new Number(x);
141
		var s = x.toString(16);
142
		while (s.length < 2) {
143
			s = "0" + s;
144
		}
145
		return s;
146
	});
147
	ret.unshift("#");
148
	return ret.join("");
149
};
150