zent
Version:
一套前端设计语言和基于React的实现
67 lines (66 loc) • 2.24 kB
JavaScript
import { rgbToHex, rgbToHsl, rgbToHsv } from './conversion';
import { inputToRGB } from './format-input';
var TinyColor = (function () {
function TinyColor(color, opts) {
if (color === void 0) { color = ''; }
if (opts === void 0) { opts = {}; }
if (color instanceof TinyColor) {
return color;
}
this.originalInput = color;
var rgb = inputToRGB(color);
this.originalInput = color;
this.r = rgb.r;
this.g = rgb.g;
this.b = rgb.b;
this.a = rgb.a;
this.roundA = Math.round(100 * this.a) / 100;
this.format = opts.format || rgb.format;
this.gradientType = opts.gradientType;
if (this.r < 1) {
this.r = Math.round(this.r);
}
if (this.g < 1) {
this.g = Math.round(this.g);
}
if (this.b < 1) {
this.b = Math.round(this.b);
}
this.isValid = rgb.ok;
}
TinyColor.prototype.toHsv = function () {
var hsv = rgbToHsv(this.r, this.g, this.b);
return { h: hsv.h * 360, s: hsv.s, v: hsv.v, a: this.a };
};
TinyColor.prototype.toHsl = function () {
var hsl = rgbToHsl(this.r, this.g, this.b);
return { h: hsl.h * 360, s: hsl.s, l: hsl.l, a: this.a };
};
TinyColor.prototype.toHex = function (allow3Char) {
if (allow3Char === void 0) { allow3Char = false; }
return rgbToHex(this.r, this.g, this.b, allow3Char);
};
TinyColor.prototype.toRgb = function () {
return {
r: Math.round(this.r),
g: Math.round(this.g),
b: Math.round(this.b),
a: this.a,
};
};
TinyColor.prototype.toRgbString = function () {
var r = Math.round(this.r);
var g = Math.round(this.g);
var b = Math.round(this.b);
return this.a === 1
? "rgb(" + r + ", " + g + ", " + b + ")"
: "rgba(" + r + ", " + g + ", " + b + ", " + this.roundA + ")";
};
return TinyColor;
}());
export { TinyColor };
export function tinycolor(color, opts) {
if (color === void 0) { color = ''; }
if (opts === void 0) { opts = {}; }
return new TinyColor(color, opts);
}