Skip to content
Snippets Groups Projects
dat.gui.module.js 83.5 KiB
Newer Older
Amira Abdel-Rahman's avatar
Amira Abdel-Rahman committed
/**
 * dat-gui JavaScript Controller Library
 * http://code.google.com/p/dat-gui
 *
 * Copyright 2011 Data Arts Team, Google Creative Lab
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 */

function ___$insertStyle( css ) {

	if ( ! css ) {

		return;

	}
	if ( typeof window === 'undefined' ) {

		return;

	}

	var style = document.createElement( 'style' );

	style.setAttribute( 'type', 'text/css' );
	style.innerHTML = css;
	document.head.appendChild( style );

	return css;

}

function colorToString( color, forceCSSHex ) {

	var colorFormat = color.__state.conversionName.toString();
	var r = Math.round( color.r );
	var g = Math.round( color.g );
	var b = Math.round( color.b );
	var a = color.a;
	var h = Math.round( color.h );
	var s = color.s.toFixed( 1 );
	var v = color.v.toFixed( 1 );
	if ( forceCSSHex || colorFormat === 'THREE_CHAR_HEX' || colorFormat === 'SIX_CHAR_HEX' ) {

		var str = color.hex.toString( 16 );
		while ( str.length < 6 ) {

			str = '0' + str;

		}
		return '#' + str;

	} else if ( colorFormat === 'CSS_RGB' ) {

		return 'rgb(' + r + ',' + g + ',' + b + ')';

	} else if ( colorFormat === 'CSS_RGBA' ) {

		return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';

	} else if ( colorFormat === 'HEX' ) {

		return '0x' + color.hex.toString( 16 );

	} else if ( colorFormat === 'RGB_ARRAY' ) {

		return '[' + r + ',' + g + ',' + b + ']';

	} else if ( colorFormat === 'RGBA_ARRAY' ) {

		return '[' + r + ',' + g + ',' + b + ',' + a + ']';

	} else if ( colorFormat === 'RGB_OBJ' ) {

		return '{r:' + r + ',g:' + g + ',b:' + b + '}';

	} else if ( colorFormat === 'RGBA_OBJ' ) {

		return '{r:' + r + ',g:' + g + ',b:' + b + ',a:' + a + '}';

	} else if ( colorFormat === 'HSV_OBJ' ) {

		return '{h:' + h + ',s:' + s + ',v:' + v + '}';

	} else if ( colorFormat === 'HSVA_OBJ' ) {

		return '{h:' + h + ',s:' + s + ',v:' + v + ',a:' + a + '}';

	}
	return 'unknown format';

}

var ARR_EACH = Array.prototype.forEach;
var ARR_SLICE = Array.prototype.slice;
var Common = {
	BREAK: {},
	extend: function extend( target ) {

		this.each( ARR_SLICE.call( arguments, 1 ), function ( obj ) {

			var keys = this.isObject( obj ) ? Object.keys( obj ) : [];
			keys.forEach( function ( key ) {

				if ( ! this.isUndefined( obj[ key ] ) ) {

					target[ key ] = obj[ key ];

				}

			}.bind( this ) );

		}, this );
		return target;

	},
	defaults: function defaults( target ) {

		this.each( ARR_SLICE.call( arguments, 1 ), function ( obj ) {

			var keys = this.isObject( obj ) ? Object.keys( obj ) : [];
			keys.forEach( function ( key ) {

				if ( this.isUndefined( target[ key ] ) ) {

					target[ key ] = obj[ key ];

				}

			}.bind( this ) );

		}, this );
		return target;

	},
	compose: function compose() {

		var toCall = ARR_SLICE.call( arguments );
		return function () {

			var args = ARR_SLICE.call( arguments );
			for ( var i = toCall.length - 1; i >= 0; i -- ) {

				args = [ toCall[ i ].apply( this, args ) ];

			}
			return args[ 0 ];

		};

	},
	each: function each( obj, itr, scope ) {

		if ( ! obj ) {

			return;

		}
		if ( ARR_EACH && obj.forEach && obj.forEach === ARR_EACH ) {

			obj.forEach( itr, scope );

		} else if ( obj.length === obj.length + 0 ) {

			var key = void 0;
			var l = void 0;
			for ( key = 0, l = obj.length; key < l; key ++ ) {

				if ( key in obj && itr.call( scope, obj[ key ], key ) === this.BREAK ) {

					return;

				}

			}

		} else {

			for ( var _key in obj ) {

				if ( itr.call( scope, obj[ _key ], _key ) === this.BREAK ) {

					return;

				}

			}

		}

	},
	defer: function defer( fnc ) {

		setTimeout( fnc, 0 );

	},
Loading
Loading full blame...