1 /**
  2  * Source: filter.js
  3  * Copyright (c) 2013-2014 Oculus Info Inc.
  4  * @fileOverview Aperture Filter API Implementations
  5  */
  6 
  7 /**
  8  * @namespace Aperture filter APIs
  9  */
 10 aperture.filter = (function(namespace) {
 11 
 12 	var effects =
 13 	/** @lends aperture.filter */
 14 	{
 15 
 16 		/**
 17 		 * Returns a function that always returns the supplied constant value
 18 		 */
 19 		constant : function(value) {
 20 			return function() {
 21 				return value;
 22 			};
 23 		},
 24 
 25 		/**
 26 		 * Returns a effect function that scales a provided number by the given
 27 		 * scalar value.
 28 		 */
 29 		scale : function(amount) {
 30 			return function( value ) {
 31 				return value * amount;
 32 			};
 33 		},
 34 		
 35 		/**
 36 		 * Returns a effect function that shifts a provided number by the given
 37 		 * scalar value.
 38 		 */
 39 		shift : function(amount) {
 40 			return function( value ) {
 41 				return value + amount;
 42 			};
 43 		},
 44 		brighter : function(color) {
 45 			// TODO
 46 		},
 47 
 48 		/**
 49 		 * Takes a conditional function and a effect function and returns a function
 50 		 * that will apply the given effect to the supplied arguments only when the truth
 51 		 * function returns a truthy value when called with the supplied arguments.  For
 52 		 * example:
 53 		 * <code>
 54 		 * var makeBigBigger = conditional(
 55 		 *      function(value) { return value > 1000; },
 56 		 *      aperture.filter.scale( 2 )
 57 		 * );
 58 		 *
 59 		 * var makeRedBlue = conditional(
 60 		 *      function(value) { return value === 'red'; },
 61 		 *      function() { return 'blue'; }
 62 		 * );
 63 		 * </code>
 64 		 *
 65 		 * @param {Function} checkFunction
 66 		 * @param {Function} filterFunction
 67 		 */
 68 		conditional : function( checkFunction, filterFunction ) {
 69 			return function(value) {
 70 				// If supplied conditional...
 71 				if( checkFunction.apply(this, arguments) ) {
 72 					// Apply effect
 73 					return filterFunction.apply(this, arguments);
 74 				} else {
 75 					return value;
 76 				}
 77 			};
 78 		}
 79 	};
 80 
 81 	// Mix in effect definitions into provided namespace
 82 	aperture.util.extend(namespace, effects);
 83 
 84 	return namespace;
 85 
 86 }(aperture.filter || {}));
 87