1111var p5 = require ( '../core/core' ) ;
1212var constants = require ( '../core/constants' ) ;
1313require ( './p5.Color' ) ;
14+ require ( '../core/error_helpers' ) ;
1415
1516/**
1617 * Extracts the alpha value from a color or pixel array.
1718 *
1819 * @method alpha
19- * @param {p5.Color|Number[] } obj p5.Color object or pixel array
20+ * @param {p5.Color|Array } color p5.Color object or pixel array
2021 * @return {Number } the alpha value
2122 * @example
2223 * <div>
@@ -52,18 +53,15 @@ require('./p5.Color');
5253 * deep pink rect on left and grey rect on right, both 35x60.
5354 */
5455p5 . prototype . alpha = function ( c ) {
55- if ( c instanceof p5 . Color || c instanceof Array ) {
56- return this . color ( c ) . _getAlpha ( ) ;
57- } else {
58- throw new Error ( 'Needs p5.Color or pixel array as argument.' ) ;
59- }
56+ p5 . _validateParameters ( 'alpha' , arguments ) ;
57+ return this . color ( c ) . _getAlpha ( ) ;
6058} ;
6159
6260/**
6361 * Extracts the blue value from a color or pixel array.
6462 *
6563 * @method blue
66- * @param {p5.Color|Number[] } obj p5.Color object or pixel array
64+ * @param {p5.Color|Array } color p5.Color object or pixel array
6765 * @return {Number } the blue value
6866 * @example
6967 * <div>
@@ -84,18 +82,15 @@ p5.prototype.alpha = function(c) {
8482 *
8583 */
8684p5 . prototype . blue = function ( c ) {
87- if ( c instanceof p5 . Color || c instanceof Array ) {
88- return this . color ( c ) . _getBlue ( ) ;
89- } else {
90- throw new Error ( 'Needs p5.Color or pixel array as argument.' ) ;
91- }
85+ p5 . _validateParameters ( 'blue' , arguments ) ;
86+ return this . color ( c ) . _getBlue ( ) ;
9287} ;
9388
9489/**
9590 * Extracts the HSB brightness value from a color or pixel array.
9691 *
9792 * @method brightness
98- * @param {p5.Color|Number[] } color p5.Color object or pixel array
93+ * @param {p5.Color|Array } color p5.Color object or pixel array
9994 * @return {Number } the brightness value
10095 * @example
10196 * <div>
@@ -116,11 +111,8 @@ p5.prototype.blue = function(c) {
116111 *
117112 */
118113p5 . prototype . brightness = function ( c ) {
119- if ( c instanceof p5 . Color || c instanceof Array ) {
120- return this . color ( c ) . _getBrightness ( ) ;
121- } else {
122- throw new Error ( 'Needs p5.Color or pixel array as argument.' ) ;
123- }
114+ p5 . _validateParameters ( 'brightness' , arguments ) ;
115+ return this . color ( c ) . _getBrightness ( ) ;
124116} ;
125117
126118/**
@@ -282,7 +274,6 @@ p5.prototype.brightness = function(c) {
282274 * Dark blue rect on left and light teal rect on right of canvas, both 45x80.
283275 *
284276 */
285-
286277/**
287278 * @method color
288279 * @param {Number } v1 red or hue value relative to
@@ -301,13 +292,17 @@ p5.prototype.brightness = function(c) {
301292 * @param {Number } [alpha]
302293 * @return {p5.Color }
303294 */
304-
305295/**
306296 * @method color
307- * @param {Number[] } values an array containing the red,green,blue &
297+ * @param {Array } values an array containing the red,green,blue &
308298 * and alpha components of the color
309299 * @return {p5.Color }
310300 */
301+ /**
302+ * @method color
303+ * @param {p5.Color } color
304+ * @return {p5.Color }
305+ */
311306
312307p5 . prototype . color = function ( ) {
313308 if ( arguments [ 0 ] instanceof p5 . Color ) {
@@ -319,6 +314,7 @@ p5.prototype.color = function() {
319314 return new p5 . Color ( this . _renderer , arguments [ 0 ] ) ;
320315 }
321316 } else {
317+ p5 . _validateParameters ( 'color' , arguments ) ;
322318 if ( this instanceof p5 . Renderer ) {
323319 return new p5 . Color ( this , arguments ) ;
324320 } else {
@@ -331,7 +327,7 @@ p5.prototype.color = function() {
331327 * Extracts the green value from a color or pixel array.
332328 *
333329 * @method green
334- * @param {p5.Color|Number[] } color p5.Color object or pixel array
330+ * @param {p5.Color|Array } color p5.Color object or pixel array
335331 * @return {Number } the green value
336332 * @example
337333 * <div>
@@ -353,11 +349,8 @@ p5.prototype.color = function() {
353349 */
354350
355351p5 . prototype . green = function ( c ) {
356- if ( c instanceof p5 . Color || c instanceof Array ) {
357- return this . color ( c ) . _getGreen ( ) ;
358- } else {
359- throw new Error ( 'Needs p5.Color or pixel array as argument.' ) ;
360- }
352+ p5 . _validateParameters ( 'green' , arguments ) ;
353+ return this . color ( c ) . _getGreen ( ) ;
361354} ;
362355
363356/**
@@ -370,7 +363,7 @@ p5.prototype.green = function(c) {
370363 * maximum hue setting for each system is different.)
371364 *
372365 * @method hue
373- * @param {p5.Color|Number[] } color p5.Color object or pixel array
366+ * @param {p5.Color|Array } color p5.Color object or pixel array
374367 * @return {Number } the hue
375368 * @example
376369 * <div>
@@ -392,11 +385,8 @@ p5.prototype.green = function(c) {
392385 */
393386
394387p5 . prototype . hue = function ( c ) {
395- if ( c instanceof p5 . Color || c instanceof Array ) {
396- return this . color ( c ) . _getHue ( ) ;
397- } else {
398- throw new Error ( 'Needs p5.Color or pixel array as argument.' ) ;
399- }
388+ p5 . _validateParameters ( 'hue' , arguments ) ;
389+ return this . color ( c ) . _getHue ( ) ;
400390} ;
401391
402392/**
@@ -443,34 +433,35 @@ p5.prototype.hue = function(c) {
443433 */
444434
445435p5 . prototype . lerpColor = function ( c1 , c2 , amt ) {
436+ p5 . _validateParameters ( 'lerpColor' , arguments ) ;
446437 var mode = this . _renderer . _colorMode ;
447438 var maxes = this . _renderer . _colorMaxes ;
448439 var l0 , l1 , l2 , l3 ;
449440 var fromArray , toArray ;
450441
451442 if ( mode === constants . RGB ) {
452- fromArray = c1 . levels . map ( function ( level ) {
443+ fromArray = arguments [ 0 ] . levels . map ( function ( level ) {
453444 return level / 255 ;
454445 } ) ;
455- toArray = c2 . levels . map ( function ( level ) {
446+ toArray = arguments [ 1 ] . levels . map ( function ( level ) {
456447 return level / 255 ;
457448 } ) ;
458449 } else if ( mode === constants . HSB ) {
459- c1 . _getBrightness ( ) ; // Cache hsba so it definitely exists.
460- c2 . _getBrightness ( ) ;
461- fromArray = c1 . hsba ;
462- toArray = c2 . hsba ;
450+ arguments [ 0 ] . _getBrightness ( ) ; // Cache hsba so it definitely exists.
451+ arguments [ 1 ] . _getBrightness ( ) ;
452+ fromArray = arguments [ 0 ] . hsba ;
453+ toArray = arguments [ 1 ] . hsba ;
463454 } else if ( mode === constants . HSL ) {
464- c1 . _getLightness ( ) ; // Cache hsla so it definitely exists.
465- c2 . _getLightness ( ) ;
466- fromArray = c1 . hsla ;
467- toArray = c2 . hsla ;
455+ arguments [ 0 ] . _getLightness ( ) ; // Cache hsla so it definitely exists.
456+ arguments [ 1 ] . _getLightness ( ) ;
457+ fromArray = arguments [ 0 ] . hsla ;
458+ toArray = arguments [ 1 ] . hsla ;
468459 } else {
469460 throw new Error ( mode + 'cannot be used for interpolation.' ) ;
470461 }
471462
472463 // Prevent extrapolation.
473- amt = Math . max ( Math . min ( amt , 1 ) , 0 ) ;
464+ amt = Math . max ( Math . min ( arguments [ 2 ] , 1 ) , 0 ) ;
474465
475466 // Define lerp here itself if user isn't using math module.
476467 // Maintains the definition as found in math/calculation.js
@@ -499,7 +490,7 @@ p5.prototype.lerpColor = function(c1, c2, amt) {
499490 * Extracts the HSL lightness value from a color or pixel array.
500491 *
501492 * @method lightness
502- * @param {p5.Color|Number[] } color p5.Color object or pixel array
493+ * @param {p5.Color|Array } color p5.Color object or pixel array
503494 * @return {Number } the lightness
504495 * @example
505496 * <div>
@@ -520,18 +511,15 @@ p5.prototype.lerpColor = function(c1, c2, amt) {
520511 *
521512 */
522513p5 . prototype . lightness = function ( c ) {
523- if ( c instanceof p5 . Color || c instanceof Array ) {
524- return this . color ( c ) . _getLightness ( ) ;
525- } else {
526- throw new Error ( 'Needs p5.Color or pixel array as argument.' ) ;
527- }
514+ p5 . _validateParameters ( 'lightness' , arguments ) ;
515+ return this . color ( c ) . _getLightness ( ) ;
528516} ;
529517
530518/**
531519 * Extracts the red value from a color or pixel array.
532520 *
533521 * @method red
534- * @param {p5.Color|Number[] } obj p5.Color object or pixel array
522+ * @param {p5.Color|Array } color p5.Color object or pixel array
535523 * @return {Number } the red value
536524 * @example
537525 * <div>
@@ -562,11 +550,8 @@ p5.prototype.lightness = function(c) {
562550 * grey canvas
563551 */
564552p5 . prototype . red = function ( c ) {
565- if ( c instanceof p5 . Color || c instanceof Array ) {
566- return this . color ( c ) . _getRed ( ) ;
567- } else {
568- throw new Error ( 'Needs p5.Color or pixel array as argument.' ) ;
569- }
553+ p5 . _validateParameters ( 'red' , arguments ) ;
554+ return this . color ( c ) . _getRed ( ) ;
570555} ;
571556
572557/**
@@ -578,8 +563,8 @@ p5.prototype.red = function(c) {
578563 * HSL saturation otherwise.
579564 *
580565 * @method saturation
581- * @param {p5.Color|Number[] } color p5.Color object or pixel array
582- * @return {Number } the saturation
566+ * @param {p5.Color|Array } color p5.Color object or pixel array
567+ * @return {Number } the saturation value
583568 * @example
584569 * <div>
585570 * <code>
@@ -600,11 +585,8 @@ p5.prototype.red = function(c) {
600585 */
601586
602587p5 . prototype . saturation = function ( c ) {
603- if ( c instanceof p5 . Color || c instanceof Array ) {
604- return this . color ( c ) . _getSaturation ( ) ;
605- } else {
606- throw new Error ( 'Needs p5.Color or pixel array as argument.' ) ;
607- }
588+ p5 . _validateParameters ( 'saturation' , arguments ) ;
589+ return this . color ( c ) . _getSaturation ( ) ;
608590} ;
609591
610592module . exports = p5 ;
0 commit comments