Skip to content

Commit

Permalink
avoid use of 'arguments' object where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
piersh committed Dec 19, 2017
1 parent d6ebba0 commit 4102551
Show file tree
Hide file tree
Showing 31 changed files with 494 additions and 624 deletions.
22 changes: 11 additions & 11 deletions src/color/creating_reading.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,28 +440,28 @@ p5.prototype.lerpColor = function(c1, c2, amt) {
var fromArray, toArray;

if (mode === constants.RGB) {
fromArray = arguments[0].levels.map(function(level) {
fromArray = c1.levels.map(function(level) {
return level / 255;
});
toArray = arguments[1].levels.map(function(level) {
toArray = c2.levels.map(function(level) {
return level / 255;
});
} else if (mode === constants.HSB) {
arguments[0]._getBrightness(); // Cache hsba so it definitely exists.
arguments[1]._getBrightness();
fromArray = arguments[0].hsba;
toArray = arguments[1].hsba;
c1._getBrightness(); // Cache hsba so it definitely exists.
c2._getBrightness();
fromArray = c1.hsba;
toArray = c2.hsba;
} else if (mode === constants.HSL) {
arguments[0]._getLightness(); // Cache hsla so it definitely exists.
arguments[1]._getLightness();
fromArray = arguments[0].hsla;
toArray = arguments[1].hsla;
c1._getLightness(); // Cache hsla so it definitely exists.
c2._getLightness();
fromArray = c1.hsla;
toArray = c2.hsla;
} else {
throw new Error(mode + 'cannot be used for interpolation.');
}

// Prevent extrapolation.
amt = Math.max(Math.min(arguments[2], 1), 0);
amt = Math.max(Math.min(amt, 1), 0);

// Define lerp here itself if user isn't using math module.
// Maintains the definition as found in math/calculation.js
Expand Down
33 changes: 16 additions & 17 deletions src/color/p5.Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @requires color_conversion
*/

'use strict';

var p5 = require('../core/core');
var constants = require('../core/constants');
var color_conversion = require('./color_conversion');
Expand Down Expand Up @@ -600,7 +602,7 @@ var colorPatterns = {
* //todo
*
*/
p5.Color._parseInputs = function() {
p5.Color._parseInputs = function(r, g, b, a) {
var numArgs = arguments.length;
var mode = this.mode;
var maxes = this.maxes;
Expand All @@ -609,13 +611,13 @@ p5.Color._parseInputs = function() {
if (numArgs >= 3) {
// Argument is a list of component values.

results[0] = arguments[0] / maxes[mode][0];
results[1] = arguments[1] / maxes[mode][1];
results[2] = arguments[2] / maxes[mode][2];
results[0] = r / maxes[mode][0];
results[1] = g / maxes[mode][1];
results[2] = b / maxes[mode][2];

// Alpha may be undefined, so default it to 100%.
if (typeof arguments[3] === 'number') {
results[3] = arguments[3] / maxes[mode][3];
if (typeof a === 'number') {
results[3] = a / maxes[mode][3];
} else {
results[3] = 1;
}
Expand All @@ -633,8 +635,8 @@ p5.Color._parseInputs = function() {
} else {
return results;
}
} else if (numArgs === 1 && typeof arguments[0] === 'string') {
var str = arguments[0].trim().toLowerCase();
} else if (numArgs === 1 && typeof r === 'string') {
var str = r.trim().toLowerCase();

// Return if string is a named colour.
if (namedColors[str]) {
Expand Down Expand Up @@ -785,24 +787,21 @@ p5.Color._parseInputs = function() {

// Input did not match any CSS color pattern: default to white.
results = [1, 1, 1, 1];
} else if (
(numArgs === 1 || numArgs === 2) &&
typeof arguments[0] === 'number'
) {
} else if ((numArgs === 1 || numArgs === 2) && typeof r === 'number') {
// 'Grayscale' mode.

/**
* For HSB and HSL, interpret the gray level as a brightness/lightness
* value (they are equivalent when chroma is zero). For RGB, normalize the
* gray level according to the blue maximum.
*/
results[0] = arguments[0] / maxes[mode][2];
results[1] = arguments[0] / maxes[mode][2];
results[2] = arguments[0] / maxes[mode][2];
results[0] = r / maxes[mode][2];
results[1] = r / maxes[mode][2];
results[2] = r / maxes[mode][2];

// Alpha may be undefined, so default it to 100%.
if (typeof arguments[1] === 'number') {
results[3] = arguments[1] / maxes[mode][3];
if (typeof g === 'number') {
results[3] = g / maxes[mode][3];
} else {
results[3] = 1;
}
Expand Down
34 changes: 17 additions & 17 deletions src/color/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,31 +300,31 @@ p5.prototype.clear = function() {
* @param {Number} [maxA] range for the alpha
* @chainable
*/
p5.prototype.colorMode = function() {
p5.prototype.colorMode = function(mode, max1, max2, max3, maxA) {
if (
arguments[0] === constants.RGB ||
arguments[0] === constants.HSB ||
arguments[0] === constants.HSL
mode === constants.RGB ||
mode === constants.HSB ||
mode === constants.HSL
) {
// Set color mode.
this._renderer._colorMode = arguments[0];
this._renderer._colorMode = mode;

// Set color maxes.
var maxes = this._renderer._colorMaxes[this._renderer._colorMode];
var maxes = this._renderer._colorMaxes[mode];
if (arguments.length === 2) {
maxes[0] = arguments[1]; // Red
maxes[1] = arguments[1]; // Green
maxes[2] = arguments[1]; // Blue
maxes[3] = arguments[1]; // Alpha
maxes[0] = max1; // Red
maxes[1] = max1; // Green
maxes[2] = max1; // Blue
maxes[3] = max1; // Alpha
} else if (arguments.length === 4) {
maxes[0] = arguments[1]; // Red
maxes[1] = arguments[2]; // Green
maxes[2] = arguments[3]; // Blue
maxes[0] = max1; // Red
maxes[1] = max2; // Green
maxes[2] = max3; // Blue
} else if (arguments.length === 5) {
maxes[0] = arguments[1]; // Red
maxes[1] = arguments[2]; // Green
maxes[2] = arguments[3]; // Blue
maxes[3] = arguments[4]; // Alpha
maxes[0] = max1; // Red
maxes[1] = max2; // Green
maxes[2] = max3; // Blue
maxes[3] = maxA; // Alpha
}
}

Expand Down
161 changes: 39 additions & 122 deletions src/core/2d_primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ require('./error_helpers');
*
*/
p5.prototype.arc = function(x, y, w, h, start, stop, mode) {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}

p5._validateParameters('arc', args);
p5._validateParameters('arc', arguments);
if (!this._renderer._doStroke && !this._renderer._doFill) {
return this;
}
Expand Down Expand Up @@ -161,39 +156,26 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode) {
*white ellipse with black outline in middle-right of canvas that is 55x55.
*
*/
p5.prototype.ellipse = function() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
// Duplicate 3rd argument if only 3 given.
if (args.length === 3) {
args.push(args[2]);
}
p5.prototype.ellipse = function(x, y, w, h) {
p5._validateParameters('ellipse', arguments);

p5._validateParameters('ellipse', args);
// p5 supports negative width and heights for rects
if (args[2] < 0) {
args[2] = Math.abs(args[2]);
if (w < 0) {
w = Math.abs(w);
}
if (args[3] < 0) {
args[3] = Math.abs(args[3]);

if (typeof h === 'undefined') {
// Duplicate 3rd argument if only 3 given.
h = w;
} else if (h < 0) {
h = Math.abs(h);
}
if (!this._renderer._doStroke && !this._renderer._doFill) {
return this;

if (this._renderer._doStroke || this._renderer._doFill) {
var vals = canvas.modeAdjust(x, y, w, h, this._renderer._ellipseMode);
this._renderer.ellipse([vals.x, vals.y, vals.w, vals.h]);
}
var vals = canvas.modeAdjust(
args[0],
args[1],
args[2],
args[3],
this._renderer._ellipseMode
);
args[0] = vals.x;
args[1] = vals.y;
args[2] = vals.w;
args[3] = vals.h;
this._renderer.ellipse(args);

return this;
};
/**
Expand Down Expand Up @@ -233,21 +215,12 @@ p5.prototype.ellipse = function() {
*
*/
p5.prototype.line = function() {
if (!this._renderer._doStroke) {
return this;
}
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
p5._validateParameters('line', arguments);

p5._validateParameters('line', args);
//check whether we should draw a 3d line or 2d
if (this._renderer.isP3D) {
this._renderer.line.apply(this, args);
} else {
this._renderer.line(args[0], args[1], args[2], args[3]);
if (this._renderer._doStroke) {
this._renderer.line.apply(this._renderer, arguments);
}

return this;
};

Expand All @@ -260,6 +233,7 @@ p5.prototype.line = function() {
* @method point
* @param {Number} x the x-coordinate
* @param {Number} y the y-coordinate
* @param {Number} [z] the z-coordinate (for WEBGL mode)
* @chainable
* @example
* <div>
Expand All @@ -276,21 +250,12 @@ p5.prototype.line = function() {
*
*/
p5.prototype.point = function() {
if (!this._renderer._doStroke) {
return this;
}
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
p5._validateParameters('point', arguments);

p5._validateParameters('point', args);
//check whether we should draw a 3d line or 2d
if (this._renderer.isP3D) {
this._renderer.point(args[0], args[1], args[2]);
} else {
this._renderer.point(args[0], args[1]);
if (this._renderer._doStroke) {
this._renderer.point.apply(this._renderer, arguments);
}

return this;
};

Expand Down Expand Up @@ -335,42 +300,12 @@ p5.prototype.point = function() {
* @chainable
*/
p5.prototype.quad = function() {
if (!this._renderer._doStroke && !this._renderer._doFill) {
return this;
}
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
p5._validateParameters('quad', arguments);

p5._validateParameters('quad', args);
if (this._renderer.isP3D) {
this._renderer.quad(
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[6],
args[7],
args[8],
args[9],
args[10],
args[11]
);
} else {
this._renderer.quad(
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[6],
args[7]
);
if (this._renderer._doStroke || this._renderer._doFill) {
this._renderer.quad.apply(this._renderer, arguments);
}

return this;
};

Expand Down Expand Up @@ -434,28 +369,14 @@ p5.prototype.quad = function() {
* @param {Number} [detailY]
* @chainable
*/
p5.prototype.rect = function() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
if (!this._renderer._doStroke && !this._renderer._doFill) {
return this;
p5.prototype.rect = function(x, y, w, h, detailX, detailY) {
p5._validateParameters('rect', arguments);

if (this._renderer._doStroke || this._renderer._doFill) {
var vals = canvas.modeAdjust(x, y, w, h, this._renderer._rectMode);
this._renderer.rect([vals.x, vals.y, vals.w, vals.h]);
}

p5._validateParameters('rect', args);
var vals = canvas.modeAdjust(
args[0],
args[1],
args[2],
args[3],
this._renderer._rectMode
);
args[0] = vals.x;
args[1] = vals.y;
args[2] = vals.w;
args[3] = vals.h;
this._renderer.rect(args);
return this;
};

Expand Down Expand Up @@ -484,16 +405,12 @@ p5.prototype.rect = function() {
*
*/
p5.prototype.triangle = function() {
if (!this._renderer._doStroke && !this._renderer._doFill) {
return this;
}
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
p5._validateParameters('triangle', arguments);

if (this._renderer._doStroke || this._renderer._doFill) {
this._renderer.triangle(arguments);
}

p5._validateParameters('triangle', args);
this._renderer.triangle(args);
return this;
};

Expand Down
Loading

0 comments on commit 4102551

Please sign in to comment.