Skip to content

Commit 24471ae

Browse files
committed
Color: Simplified setStyle().
1 parent a9c5076 commit 24471ae

File tree

1 file changed

+16
-54
lines changed

1 file changed

+16
-54
lines changed

src/math/Color.js

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,16 @@ THREE.Color.prototype = {
105105

106106
setStyle: function ( style ) {
107107

108-
function parseAlpha( strAlpha ) {
108+
function handleAlpha( string ) {
109109

110-
var alpha = parseFloat( strAlpha );
110+
if ( string === undefined ) return;
111111

112-
if ( alpha < 1 ) {
112+
if ( parseFloat( string ) < 1 ) {
113113

114-
console.warn( 'THREE.Color: Alpha component of color ' + style + ' will be ignored.' );
114+
console.warn( 'THREE.Color: Alpha component of ' + style + ' will be ignored.' );
115115

116116
}
117117

118-
return alpha;
119-
120118
}
121119

122120

@@ -133,52 +131,29 @@ THREE.Color.prototype = {
133131
switch ( name ) {
134132

135133
case 'rgb':
136-
137-
if ( color = /^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*$/.exec( components ) ) {
138-
139-
// rgb(255,0,0)
140-
this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255;
141-
this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255;
142-
this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255;
143-
144-
return this;
145-
146-
}
147-
148-
if ( color = /^(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*$/.exec( components ) ) {
149-
150-
// rgb(100%,0%,0%)
151-
this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100;
152-
this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100;
153-
this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100;
154-
155-
return this;
156-
157-
}
158-
159-
break;
160-
161134
case 'rgba':
162135

163-
if ( color = /^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([0-9]*\.?[0-9]+)\s*$/.exec( components ) ) {
136+
if ( color = /^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
164137

165-
// rgba(255,0,0,0.5)
138+
// rgb(255,0,0) rgba(255,0,0,0.5)
166139
this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255;
167140
this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255;
168141
this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255;
169-
parseAlpha( color[ 4 ] );
142+
143+
handleAlpha( color[ 5 ] );
170144

171145
return this;
172146

173147
}
174148

175-
if ( color = /^(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*,\s*([0-9]*\.?[0-9]+)\s*$/.exec( components ) ) {
149+
if ( color = /^(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
176150

177-
// rgba(100%,0%,0%,0.5)
151+
// rgb(100%,0%,0%) rgba(100%,0%,0%,0.5)
178152
this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100;
179153
this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100;
180154
this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100;
181-
parseAlpha( color[ 4 ] );
155+
156+
handleAlpha( color[ 5 ] );
182157

183158
return this;
184159

@@ -187,29 +162,16 @@ THREE.Color.prototype = {
187162
break;
188163

189164
case 'hsl':
190-
191-
if ( color = /^([0-9]*\.?[0-9]+)\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*$/.exec( components ) ) {
192-
193-
// hsl(120,50%,50%)
194-
var h = parseFloat( color[ 1 ] ) / 360;
195-
var s = parseInt( color[ 2 ], 10 ) / 100;
196-
var l = parseInt( color[ 3 ], 10 ) / 100;
197-
198-
return this.setHSL( h, s, l );
199-
200-
}
201-
202-
break;
203-
204165
case 'hsla':
205166

206-
if ( color = /^([0-9]*\.?[0-9]+)\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*,\s*([0-9]*\.?[0-9]+)\s*$/.exec( components ) ) {
167+
if ( color = /^([0-9]*\.?[0-9]+)\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
207168

208-
// hsla(120,50%,50%,0.5)
169+
// hsl(120,50%,50%) hsla(120,50%,50%,0.5)
209170
var h = parseFloat( color[ 1 ] ) / 360;
210171
var s = parseInt( color[ 2 ], 10 ) / 100;
211172
var l = parseInt( color[ 3 ], 10 ) / 100;
212-
parseAlpha( color[ 4 ] );
173+
174+
handleAlpha( color[ 5 ] );
213175

214176
return this.setHSL( h, s, l );
215177

0 commit comments

Comments
 (0)