Color.js 2.0 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,
15

M
Mr.doob 已提交
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	copyGammaToLinear: function ( color ) {

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

		return this;

	},

	copyLinearToGamma: function ( color ) {

		this.r = Math.sqrt( color.r );
		this.g = Math.sqrt( color.g );
		this.b = Math.sqrt( color.b );

		return this;

	},

48
	setRGB: function ( r, g, b ) {
M
Mr.doob 已提交
49 50 51 52 53

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

M
Mr.doob 已提交
54
		return this;
M
Mr.doob 已提交
55 56 57

	},

58
	setHSV: function ( h, s, v ) {
59

60 61 62
		// based on MochiKit implementation by Bob Ippolito
		// h,s,v ranges are < 0.0 - 1.0 >

M
Mr.doob 已提交
63
		var i, f, p, q, t;
64

A
alteredq 已提交
65
		if ( v === 0 ) {
66

M
Mr.doob 已提交
67
			this.r = this.g = this.b = 0;
68

69
		} else {
70

71 72 73 74 75
			i = Math.floor( h * 6 );
			f = ( h * 6 ) - i;
			p = v * ( 1 - s );
			q = v * ( 1 - ( s * f ) );
			t = v * ( 1 - ( s * ( 1 - f ) ) );
76

77
			switch ( i ) {
78

M
Mr.doob 已提交
79 80 81 82 83
				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;
84
				case 6: // fall through
M
Mr.doob 已提交
85
				case 0: this.r = v; this.g = t; this.b = p; break;
86

87
			}
88

89
		}
90

M
Mr.doob 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103
		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 已提交
104

P
philogb 已提交
105
	},
M
Mr.doob 已提交
106

M
Mr.doob 已提交
107
	getHex: function () {
M
Mr.doob 已提交
108

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

P
philogb 已提交
111
	},
M
Mr.doob 已提交
112

M
Mr.doob 已提交
113
	getContextStyle: function () {
M
Mr.doob 已提交
114

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

P
philogb 已提交
117
	},
M
Mr.doob 已提交
118

119
	clone: function () {
120

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

123
	}
M
Mr.doob 已提交
124

M
Mr.doob 已提交
125
};