Color.js 2.3 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	convertGammaToLinear: function () {

		var r = this.r, g = this.g, b = this.b;

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

		return this;

	},

	convertLinearToGamma: function () {

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

		return this;

	},

70
	setRGB: function ( r, g, b ) {
M
Mr.doob 已提交
71 72 73 74 75

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

M
Mr.doob 已提交
76
		return this;
M
Mr.doob 已提交
77 78 79

	},

80
	setHSV: function ( h, s, v ) {
81

82 83 84
		// based on MochiKit implementation by Bob Ippolito
		// h,s,v ranges are < 0.0 - 1.0 >

M
Mr.doob 已提交
85
		var i, f, p, q, t;
86

87
		if ( v === 0 ) {
88

M
Mr.doob 已提交
89
			this.r = this.g = this.b = 0;
90

91
		} else {
92

93 94 95 96 97
			i = Math.floor( h * 6 );
			f = ( h * 6 ) - i;
			p = v * ( 1 - s );
			q = v * ( 1 - ( s * f ) );
			t = v * ( 1 - ( s * ( 1 - f ) ) );
98

99
			switch ( i ) {
100

M
Mr.doob 已提交
101 102 103 104 105
				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;
106
				case 6: // fall through
M
Mr.doob 已提交
107
				case 0: this.r = v; this.g = t; this.b = p; break;
108

109
			}
110

111
		}
112

M
Mr.doob 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
		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 已提交
126

P
philogb 已提交
127
	},
M
Mr.doob 已提交
128

M
Mr.doob 已提交
129
	getHex: function () {
M
Mr.doob 已提交
130

M
Mr.doob 已提交
131
		return Math.floor( this.r * 255 ) << 16 ^ Math.floor( this.g * 255 ) << 8 ^ Math.floor( this.b * 255 );
M
Mr.doob 已提交
132

P
philogb 已提交
133
	},
M
Mr.doob 已提交
134

M
Mr.doob 已提交
135
	getContextStyle: function () {
M
Mr.doob 已提交
136

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

P
philogb 已提交
139
	},
M
Mr.doob 已提交
140

141
	clone: function () {
142

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

145
	}
M
Mr.doob 已提交
146

M
Mr.doob 已提交
147
};