@retriever-ui/system
Version:
63 lines (62 loc) • 2.51 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToHex = exports.getOpacity = void 0;
var getOpacity = function (value) {
if (value > 10 || value < 0) {
throw new Error("The opacity must be greater than 0 or less than 10. You pass: " + value + ".");
}
var isNotInteger = !Number.isInteger(value);
if (isNotInteger) {
throw new Error("The opacity must be a integer value. You pass: " + value + ".");
}
var toHex = function (a) { return a.toString(16); };
var toOpacity = function (a) { return toHex(Math.trunc((a * 255) / 10)); };
var opacity = toOpacity(value);
return opacity;
};
exports.getOpacity = getOpacity;
var convertToHex = function (theme, color) {
var colors = Object.keys(theme.colors);
if (!colors.includes(color)) {
return color;
}
var hex = theme.colors[color];
return hex;
};
exports.convertToHex = convertToHex;
var getColorTheme = function (theme, color) {
if (typeof color !== 'string')
return color;
var onlyColorKey = /(([a-z]+\.\d?\d?\d?)(\/\d?.?\d)?)/g;
var colorSplit = color === null || color === void 0 ? void 0 : color.match(onlyColorKey);
var cssPropertyValue = color;
colorSplit === null || colorSplit === void 0 ? void 0 : colorSplit.forEach(function (item) {
var _a = __read(item.split('/'), 2), key = _a[0], opacity = _a[1];
var hex = (0, exports.convertToHex)(theme, key);
if (opacity) {
var hexOpacity = (0, exports.getOpacity)(Number(opacity));
var hexWithOpacity = hex + hexOpacity;
cssPropertyValue = cssPropertyValue === null || cssPropertyValue === void 0 ? void 0 : cssPropertyValue.replace(item, hexWithOpacity);
return cssPropertyValue;
}
cssPropertyValue = cssPropertyValue === null || cssPropertyValue === void 0 ? void 0 : cssPropertyValue.replace(item, hex);
});
return cssPropertyValue;
};
exports.default = getColorTheme;