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