color-math
Version:
expressions to manipulate colors
72 lines (59 loc) • 2.23 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Chroma = require('chroma-js');
var ColorScale =
/*#__PURE__*/
function () {
function ColorScale(name, params, scaleParams) {
_classCallCheck(this, ColorScale);
this.name = name.toLowerCase();
this.params = params || [];
this.scaleParams = scaleParams || [];
}
_createClass(ColorScale, [{
key: "toString",
value: function toString() {
return "<colorScale.".concat(this.name, ">");
}
}, {
key: "clone",
value: function clone() {
var obj = new ColorScale(this.name, this.params.slice(0), this.scaleParams.slice(0));
return obj;
}
}, {
key: "_getParamValue",
value: function _getParamValue(params, name) {
for (var i = 0; i < params.length; i++) {
if (params[i].name === name) {
return params[i].value;
}
}
}
}, {
key: "_applyParams",
value: function _applyParams(fn, params) {
for (var i = 0; i < params.length; i++) {
if (params[i].name !== 'colors') {
fn = fn[params[i].name](params[i].value);
}
}
}
}, {
key: "getFn",
value: function getFn() {
var colors = this._getParamValue(this.scaleParams, 'colors');
var ctor = Chroma[this.name];
var fn = colors ? ctor(colors) : ctor();
this._applyParams(fn, this.params);
if (this.name !== 'scale') {
fn = fn.scale();
}
this._applyParams(fn, this.scaleParams);
return fn;
}
}]);
return ColorScale;
}();
export { ColorScale as default };