color-model
Version:
Operate colors in popular color models and convert between them
60 lines (51 loc) • 1.23 kB
JavaScript
module.exports = (function() { return Component; })();
/**
* Color model Component
* @param {String} name
* @param {Number} from
* @param {Number} to
*/
function Component(name, from, to) {
if (!name) {
throw new Error('Name not set');
}
this._name = name;
from = parseFloat(from);
to = parseFloat(to);
if (!(from < to)) {
throw new Error('From must be less than to');
}
this._from = from;
this._to = to;
this._value = null;
};
/**
* @param {Number} value
* @returns {Component}
*/
Component.prototype.set = function (value) {
value = value ? parseFloat(value) : 0;
if (isNaN(value)) {
throw new Error('Value for ' + this._name + ' must be numeric');
}
if (value < this._from || value > this._to) {
throw new Error('Value for ' + this._name + ' (' + value + ') must be between ' + this._from + ' and ' + this._to);
}
this._value = value;
return this;
};
/**
* @returns {Number}
*/
Component.prototype.get = function () {
return this._value;
};
/**
* @param {Component} that
* @returns {Boolean}
*/
Component.prototype.equals = function (that) {
return (that instanceof Component)
&& this._name === that._name
&& this._value === that._value;
};