@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
57 lines (54 loc) • 2.34 kB
JavaScript
;
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.normalizeHexColor = normalizeHexColor;
var namedColors = _interopRequireWildcard(require("css-color-names"));
var _isHex = require("./is-hex");
var _isRgb = require("./is-rgb");
var _rgbToHex = require("./rgb-to-hex");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
// eslint-disable-line import/no-namespace
/**
* @returns String with HEX-coded color.
*/
function normalizeHexColor(color, defaultColor) {
if (!color) {
return null;
}
// Normalize to hex
color = color.trim().toLowerCase();
if ((0, _isHex.isHex)(color)) {
// Normalize 3-hex to 6-hex colours
if (color.length === 4) {
color = color.split('').map(function (c) {
return c === '#' ? '#' : "".concat(c).concat(c);
}).join('');
}
} else if ((0, _isRgb.isRgb)(color)) {
return (0, _rgbToHex.rgbToHex)(color);
} else {
// http://dev.w3.org/csswg/css-color/#named-colors
if (color === 'default') {
return null;
} else if (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
namedColors.default &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
namedColors.default[color]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
color = namedColors.default[color];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} else if (namedColors && namedColors[color]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
color = namedColors[color];
} else {
return null;
}
}
if (color === defaultColor) {
return null;
}
return color;
}