@atlaskit/atlassian-navigation
Version:
A horizontal navigation component for Atlassian products.
73 lines (71 loc) • 3.66 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hexToRGBA = exports.getContrastColor = exports.getBoxShadow = exports.generateTextColor = exports.convertHexShorthand = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _chromatism = _interopRequireDefault(require("chromatism"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var hexToRGBA = function hexToRGBA(hex) {
var opacity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var rgba = _objectSpread(_objectSpread({}, _chromatism.default.convert(hex).rgb), {
a: opacity
});
return "rgba(".concat(Object.values(rgba).join(', '), ")");
};
exports.hexToRGBA = hexToRGBA;
var getBoxShadow = function getBoxShadow(color) {
return "0 0 0 2px ".concat(color);
};
exports.getBoxShadow = getBoxShadow;
var hex = '[a-z0-9]';
var shortHandHexColorPattern = new RegExp("#(".concat(hex, ")(").concat(hex, ")(").concat(hex, ")"), 'i');
var repeat = function repeat(str, times) {
return (0, _toConsumableArray2.default)(Array(times)).map(function () {
return str;
}).join('');
};
var isShortHexColor = function isShortHexColor(color) {
return color && color.length === 4;
};
var completeHexColor = function completeHexColor(colors) {
var hex = colors.map(function (color) {
return repeat(color, 2);
}).join('');
return "#".concat(hex);
};
var completeTriplet = function completeTriplet(colors) {
return "#".concat(repeat(colors[1], 6));
};
var isTriplet = function isTriplet(colors) {
return colors[0] === colors[1] && colors[1] === colors[2];
};
var convertHexShorthand = function convertHexShorthand(color) {
if (isShortHexColor(color)) {
// when color = '#ccc', matches is structured as ['#ccc', 'c', 'c', 'c', ...]
var matches = color.match(shortHandHexColorPattern);
if (matches) {
var colors = matches.slice(1, 4);
if (isTriplet(colors)) {
return completeTriplet(colors);
} else {
return completeHexColor(colors);
}
}
}
// return the color as is when it's not hex color shorthand
return color;
};
exports.convertHexShorthand = convertHexShorthand;
var generateTextColor = function generateTextColor(color) {
var converted = convertHexShorthand(color);
return _chromatism.default.contrastRatio(converted).hex;
};
exports.generateTextColor = generateTextColor;
var getContrastColor = function getContrastColor(contrastValue, opacityValue, color) {
return hexToRGBA(_chromatism.default.contrast(contrastValue, color).hex, opacityValue);
};
exports.getContrastColor = getContrastColor;