Color.js 1.7 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mr.doob / http://mrdoob.com/
 */

5
THREE.Color = function ( hex ) {
M
Mr.doob 已提交
6

M
Mr.doob 已提交
7 8
	if ( hex !== undefined ) this.setHex( hex );
	return this;
M
Mr.doob 已提交
9

10
};
M
Mr.doob 已提交
11

P
philogb 已提交
12 13
THREE.Color.prototype = {

14
	constructor: THREE.Color,
M
Mr.doob 已提交
15 16
	
	r: 1, g: 1, b: 1,
17 18

	copy: function ( color ) {
M
Mr.doob 已提交
19 20 21 22

		this.r = color.r;
		this.g = color.g;
		this.b = color.b;
23

M
Mr.doob 已提交
24
		return this;
M
Mr.doob 已提交
25 26 27

	},

28
	setRGB: function ( r, g, b ) {
M
Mr.doob 已提交
29 30 31 32 33

		this.r = r;
		this.g = g;
		this.b = b;

M
Mr.doob 已提交
34
		return this;
M
Mr.doob 已提交
35 36 37

	},

38
	setHSV: function ( h, s, v ) {
39

40 41 42
		// based on MochiKit implementation by Bob Ippolito
		// h,s,v ranges are < 0.0 - 1.0 >

M
Mr.doob 已提交
43
		var i, f, p, q, t;
44

M
Mr.doob 已提交
45
		if ( v == 0 ) {
46

M
Mr.doob 已提交
47
			this.r = this.g = this.b = 0;
48

49
		} else {
50

51 52 53 54 55
			i = Math.floor( h * 6 );
			f = ( h * 6 ) - i;
			p = v * ( 1 - s );
			q = v * ( 1 - ( s * f ) );
			t = v * ( 1 - ( s * ( 1 - f ) ) );
56

57
			switch ( i ) {
58

M
Mr.doob 已提交
59 60 61 62 63
				case 1: this.r = q; this.g = v; this.b = p; break;
				case 2: this.r = p; this.g = v; this.b = t; break;
				case 3: this.r = p; this.g = q; this.b = v; break;
				case 4: this.r = t; this.g = p; this.b = v; break;
				case 5: this.r = v; this.g = p; this.b = q; break;
64
				case 6: // fall through
M
Mr.doob 已提交
65
				case 0: this.r = v; this.g = t; this.b = p; break;
66

67
			}
68

69
		}
70

M
Mr.doob 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
		return this;

	},

	setHex: function ( hex ) {

		hex = Math.floor( hex );

		this.r = ( hex >> 16 & 255 ) / 255;
		this.g = ( hex >> 8 & 255 ) / 255;
		this.b = ( hex & 255 ) / 255;

		return this;
M
Mr.doob 已提交
84

P
philogb 已提交
85
	},
M
Mr.doob 已提交
86

M
Mr.doob 已提交
87
	getHex: function () {
M
Mr.doob 已提交
88

M
Mr.doob 已提交
89
		return ~~ ( this.r * 255 ) << 16 ^ ~~ ( this.g * 255 ) << 8 ^ ~~ ( this.b * 255 );
M
Mr.doob 已提交
90

P
philogb 已提交
91
	},
M
Mr.doob 已提交
92

M
Mr.doob 已提交
93
	getContextStyle: function () {
M
Mr.doob 已提交
94

M
Mr.doob 已提交
95
		return 'rgb(' + Math.floor( this.r * 255 ) + ',' + Math.floor( this.g * 255 ) + ',' + Math.floor( this.b * 255 ) + ')';
M
Mr.doob 已提交
96

P
philogb 已提交
97
	},
M
Mr.doob 已提交
98

99
	clone: function () {
100

M
Mr.doob 已提交
101
		return new THREE.Color().setRGB( this.r, this.g, this.b );
102

103
	}
M
Mr.doob 已提交
104

M
Mr.doob 已提交
105
};