@lobehub/charts
Version:
React modern charts components built on recharts
40 lines (39 loc) • 1.81 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import chroma from 'chroma-js';
var isstringScale = function isstringScale(colors, size) {
var invalidstring = colors.find(function (color) {
return !chroma.valid(color);
});
if (invalidstring) {
throw new Error("Invalid color \"".concat(String(invalidstring), "\" passed. All CSS color formats are accepted."));
}
return colors.length === size;
};
var createstringScale = function createstringScale(colors, size) {
return chroma.scale(colors).mode('lch').colors(size);
};
var validateTheme = function validateTheme(input, size) {
if (_typeof(input) !== 'object' || input === undefined) {
throw new Error("The theme object must contain at least 2 or ".concat(size, " colors respectively."));
}
if (input) {
var length = input.length;
if (length !== 2 && length !== size) {
throw new Error("theme must contain exactly 2 or ".concat(size, " colors, ").concat(length, " passed."));
}
}
};
var createDefaultTheme = function createDefaultTheme(size) {
return createstringScale(['hsl(0, 0%, 20%)', 'hsl(0, 0%, 92%)'], size);
};
export function createTheme(input) {
var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
var defaultTheme = createDefaultTheme(size);
if (input) {
validateTheme(input, size);
var theme = input !== null && input !== void 0 ? input : defaultTheme;
// @ts-ignore
return isstringScale(theme, size) ? theme : createstringScale(theme, size);
}
return defaultTheme;
}