transcend-charts
Version:
Transcend is a charting and graph library for NUVI
224 lines (191 loc) • 5.73 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
exports.default = {
Color: function Color(mixedColor) {
this.r = 0;
this.g = 0;
this.b = 0;
this.a = 1;
// parse the mixedColor input to create the Color model
if (typeof mixedColor === 'string') {
if (mixedColor === 'transparent') {
this.r = 0;
this.g = 0;
this.b = 0;
this.a = 0;
} else if (mixedColor.substring(0, 1) === '#') {
try {
this.r = parseInt(mixedColor.substring(1, 3), 16);
this.g = parseInt(mixedColor.substring(3, 5), 16);
this.b = parseInt(mixedColor.substring(5, 7), 16);
} catch (e) {
throw new Error('Invalid Color hex string in constructor');
}
} else if (mixedColor.substring(0, 3) === 'rgb') {
var isRGBA = false;
if (mixedColor.charAt(3) === 'a') {
isRGBA = true;
}
try {
var st = isRGBA ? 5 : 4;
var en = mixedColor.indexOf(',', st);
var tmp = mixedColor.substring(st, en);
this.r = parseInt(tmp);
st = en + 1;
en = mixedColor.indexOf(',', st);
tmp = mixedColor.substring(st, en);
this.g = parseInt(tmp);
st = en + 1;
en = mixedColor.indexOf(isRGBA ? ',' : ')', st);
tmp = mixedColor.substring(st, en);
this.b = parseInt(tmp);
if (isRGBA) {
st = en + 1;
en = mixedColor.indexOf(')', st);
tmp = mixedColor.substring(st, en);
this.a = parseFloat(tmp);
}
} catch (e) {
throw new Error('Invalid Color rgba string in constructor');
}
}
} else if ((typeof mixedColor === 'undefined' ? 'undefined' : _typeof(mixedColor)) === 'object') {
try {
this.r = mixedColor.r;
this.g = mixedColor.g;
this.b = mixedColor.b;
this.a = mixedColor.a;
} catch (e) {
throw new Error('Invalid Color object in constructor');
}
} else {
throw new Error("Couldn't parse color input: '" + mixedColor + "'");
}
// This function darkens the current color by pct
this.darken = function (pct) {
if (pct > 1) {
pct = 1;
}
if (pct < 0) {
return;
}
this.r = Math.round(this.r * (1 - pct));
this.g = Math.round(this.g * (1 - pct));
this.b = Math.round(this.b * (1 - pct));
return this;
};
// This function lightens the current color by pct
this.lighten = function (pct) {
if (pct > 1) {
pct = 1;
}
if (pct < 0) {
return;
}
this.r = Math.round(this.r * (1 + pct));
if (this.r > 255) {
this.r = 255;
}
this.g = Math.round(this.g * (1 + pct));
if (this.g > 255) {
this.g = 255;
}
this.b = Math.round(this.b * (1 + pct));
if (this.b > 255) {
this.b = 255;
}
return this;
};
// fades the color (using the alpha channel)
this.fade = function (pct) {
if (pct > 1) {
pct = 1;
}
if (pct < 0) {
return;
}
this.a = this.a * (1 - pct);
if (this.a < 0) {
this.a = 0;
}
return this;
};
// inverts the color
this.invert = function () {
this.r = 255 - this.r;
this.g = 255 - this.g;
this.b = 255 - this.b;
return this;
};
// This function returns a hex string of the current color
this.getHex = function () {
if (this.a === 0) {
return 'transparent';
}
var rstr = this.r.toString(16);
if (rstr.length < 2) {
rstr = '0' + rstr;
}
var gstr = this.g.toString(16);
if (gstr.length < 2) {
gstr = '0' + gstr;
}
var bstr = this.b.toString(16);
if (bstr.length < 2) {
bstr = '0' + bstr;
}
var str = '#' + rstr + gstr + bstr;
return str;
};
// This function returns an rgba string for the current color
this.getRgba = function () {
if (this.a === 0) {
return 'transparent';
}
return 'rgba(' + this.r.toString() + ',' + this.g.toString() + ',' + this.b.toString() + ',' + this.a.toString() + ')';
};
this.getR = function () {
return Number(this.r);
};
this.getG = function () {
return Number(this.g);
};
this.getB = function () {
return Number(this.b);
};
this.getA = function () {
return Number(this.a);
};
this.toString = function () {
if (this.a === 0) {
return 'transparent';
}
if (this.a === 1) {
return this.getHex();
}
return this.getRgba();
};
this.isTransparent = function () {
return this.a === 0;
};
},
makeColorBetween: function makeColorBetween(startColor, endColor, pct) {
var color1 = new this.Color(startColor);
var color2 = new this.Color(endColor);
if (pct > 1) {
pct = 1;
}
if (pct < 0) {
pct = 0;
}
var r = Math.floor(color1.r + (color2.r - color1.r) * pct);
var g = Math.floor(color1.g + (color2.g - color1.g) * pct);
var b = Math.floor(color1.b + (color2.b - color1.b) * pct);
var a = color1.a + (color2.a - color1.a) * pct;
var finalColor = new this.Color('rgba(' + r + ',' + g + ',' + b + ',' + a + ')');
return finalColor;
}
};