antd
Version:
An enterprise-class UI design language and React components implementation
122 lines (121 loc) • 3.78 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toHexFormat = exports.getHex = exports.AggregationColor = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _colorPicker = require("@rc-component/color-picker");
const toHexFormat = (value, alpha) => (value === null || value === void 0 ? void 0 : value.replace(/[^\w/]/g, '').slice(0, alpha ? 8 : 6)) || '';
exports.toHexFormat = toHexFormat;
const getHex = (value, alpha) => value ? toHexFormat(value, alpha) : '';
exports.getHex = getHex;
let AggregationColor = exports.AggregationColor = /*#__PURE__*/function () {
function AggregationColor(color) {
(0, _classCallCheck2.default)(this, AggregationColor);
var _a;
this.cleared = false;
// Clone from another AggregationColor
if (color instanceof AggregationColor) {
this.metaColor = color.metaColor.clone();
this.colors = (_a = color.colors) === null || _a === void 0 ? void 0 : _a.map(info => ({
color: new AggregationColor(info.color),
percent: info.percent
}));
this.cleared = color.cleared;
return;
}
const isArray = Array.isArray(color);
if (isArray && color.length) {
this.colors = color.map(_ref => {
let {
color: c,
percent
} = _ref;
return {
color: new AggregationColor(c),
percent
};
});
this.metaColor = new _colorPicker.Color(this.colors[0].color.metaColor);
} else {
this.metaColor = new _colorPicker.Color(isArray ? '' : color);
}
if (!color || isArray && !this.colors) {
this.metaColor = this.metaColor.setA(0);
this.cleared = true;
}
}
return (0, _createClass2.default)(AggregationColor, [{
key: "toHsb",
value: function toHsb() {
return this.metaColor.toHsb();
}
}, {
key: "toHsbString",
value: function toHsbString() {
return this.metaColor.toHsbString();
}
}, {
key: "toHex",
value: function toHex() {
return getHex(this.toHexString(), this.metaColor.a < 1);
}
}, {
key: "toHexString",
value: function toHexString() {
return this.metaColor.toHexString();
}
}, {
key: "toRgb",
value: function toRgb() {
return this.metaColor.toRgb();
}
}, {
key: "toRgbString",
value: function toRgbString() {
return this.metaColor.toRgbString();
}
}, {
key: "isGradient",
value: function isGradient() {
return !!this.colors && !this.cleared;
}
}, {
key: "getColors",
value: function getColors() {
return this.colors || [{
color: this,
percent: 0
}];
}
}, {
key: "toCssString",
value: function toCssString() {
const {
colors
} = this;
// CSS line-gradient
if (colors) {
const colorsStr = colors.map(c => `${c.color.toRgbString()} ${c.percent}%`).join(', ');
return `linear-gradient(90deg, ${colorsStr})`;
}
return this.metaColor.toRgbString();
}
}, {
key: "equals",
value: function equals(color) {
if (!color || this.isGradient() !== color.isGradient()) {
return false;
}
if (!this.isGradient()) {
return this.toHexString() === color.toHexString();
}
return this.colors.length === color.colors.length && this.colors.every((c, i) => {
const target = color.colors[i];
return c.percent === target.percent && c.color.equals(target.color);
});
}
}]);
}();