UNPKG

1.44 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = minifyColor;
7
8var _colord = require("colord");
9
10var _names = _interopRequireDefault(require("colord/plugins/names"));
11
12var _minify = _interopRequireDefault(require("colord/plugins/minify"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16(0, _colord.extend)([_names.default, _minify.default]);
17/**
18 * Performs color value minification
19 *
20 * @param {string} input - CSS value
21 * @param {boolean} options.supportsAlphaHex - Does the browser support 4 & 8 character hex notation
22 * @param {boolean} options.supportsTransparent – Does the browser support "transparent" value properly
23 */
24
25function minifyColor(input, options = {}) {
26 const settings = {
27 supportsAlphaHex: false,
28 supportsTransparent: true,
29 ...options
30 };
31 const instance = (0, _colord.colord)(input);
32
33 if (instance.isValid()) {
34 // Try to shorten the string if it is a valid CSS color value
35 const minified = instance.minify({
36 alphaHex: settings.supportsAlphaHex,
37 transparent: settings.supportsTransparent,
38 name: true
39 }); // Fall back to the original input if it's smaller or has equal length
40
41 return minified.length < input.length ? minified : input.toLowerCase();
42 } else {
43 // Possibly malformed, so pass through
44 return input;
45 }
46}
47
48module.exports = exports.default;
\No newline at end of file