Plato on Github
Report Home
src/colorpalette.js
Maintainability
60.87
Lines of code
162
Difficulty
34.53
Estimated Errors
0.62
Function weight
By Complexity
By SLOC
var Utils = require('drawing-utils-lib'); /** * Creates a new ColorPalette object. * * Use this class to create a palette of colors randomly selected * from a range created with initial start and end colors. You * can also generate gradients that smoothly interpolate from * start and end colors. * * @param {string|number} [opt_id=] An optional id. If an id is not passed, a default id is created. * @constructor */ function ColorPalette(opt_id) { /** * Holds a list of arrays representing 3-digit color values * smoothly interpolated between start and end colors. * @private */ this._gradients = []; /** * Holds a list of arrays representing 3-digit color values * randomly selected from start and end colors. * @private */ this._colors = []; this.id = opt_id || ColorPalette._idCount; ColorPalette._idCount++; // increment id } /** * Increments as each ColorPalette is created. * @type number * @default 0 * @private */ ColorPalette._idCount = 0; ColorPalette.prototype.name = 'ColorPalette'; /** * Creates a color range of 255 colors from the passed start and end colors. * Adds a random selection of these colors to the color property of * the color palette. * * @param {Object} options A set of required options * that includes: * options.min {number} The minimum number of colors to add. * options.max {number} The maximum number of color to add. * options.startColor {Array} The beginning color of the color range. * options.endColor {Array} The end color of the color range. */ ColorPalette.prototype.addColor = function(options) { if (!options.min || !options.max || !options.startColor || !options.endColor) { throw new Error('ColorPalette.addColor must pass min, max, startColor and endColor options.'); } var i, ln, colors; ln = Utils.getRandomNumber(options.min, options.max); colors = ColorPalette._createColorRange(options.startColor, options.endColor, 255); for (i = 0; i < ln; i++) { this._colors.push(colors[Utils.getRandomNumber(0, colors.length - 1)]); } return this; }; /** * Creates an array of RGB color values interpolated between * a passed startColor and endColor. * * @param {Array} startColor The beginning of the color array. * @param {Array} startColor The end of the color array. * @param {number} totalColors The total numnber of colors to create. * @returns {Array} An array of color values. */ ColorPalette._createColorRange = function(startColor, endColor, totalColors) { var i, colors = [], startRed = startColor[0], startGreen = startColor[1], startBlue = startColor[2], endRed = endColor[0], endGreen = endColor[1], endBlue = endColor[2], diffRed, diffGreen, diffBlue, newRed, newGreen, newBlue; diffRed = endRed - startRed; diffGreen = endGreen - startGreen; diffBlue = endBlue - startBlue; for (i = 0; i < totalColors; i++) { newRed = parseInt(diffRed * i/totalColors, 10) + startRed; newGreen = parseInt(diffGreen * i/totalColors, 10) + startGreen; newBlue = parseInt(diffBlue * i/totalColors, 10) + startBlue; colors.push([newRed, newGreen, newBlue]); } return colors; }; /** * @returns An array representing a randomly selected color * from the colors property. * @throws {Error} If the colors property is empty. */ ColorPalette.prototype.getColor = function() { if (this._colors.length > 0) { return this._colors[Utils.getRandomNumber(0, this._colors.length - 1)]; } else { throw new Error('ColorPalette.getColor: You must add colors via addColor() before using getColor().'); } }; // TODO: add the following /** * Adds color arrays representing a color range to the gradients property. * * @param {Object} options A set of required options * that includes: * options.startColor {Array} The beginning color of the color range. * options.endColor {Array} The end color of the color range. * options.totalColors {number} The total number of colors in the gradient. * @private */ /*ColorPalette.prototype.createGradient = function(options) { if (!options.startColor || !options.endColor || !options.totalColors) { throw new Error('ColorPalette.addColor must pass startColor, endColor and totalColors options.'); } this.startColor = options.startColor; this.endColor = options.endColor; this.totalColors = options.totalColors || 255; this._gradients.push(ColorPalette._createColorRange(this.startColor, this.endColor, this.totalColors)); };*/ /** * Renders a strip of colors representing the color range * in the colors property. * * @param {Object} parent A DOM object to contain the color strip. */ /*ColorPalette.prototype.createSampleStrip = function(parent) { var i, max, div; for (i = 0, max = this._colors.length; i < max; i++) { div = document.createElement('div'); div.className = 'color-sample-strip'; div.style.background = 'rgb(' + this._colors[i].toString() + ')'; parent.appendChild(div); } };*/ module.exports = ColorPalette;