1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | Object.defineProperty(exports, "process", {
|
---|
7 | enumerable: true,
|
---|
8 | get: function () {
|
---|
9 | return _colord.colord;
|
---|
10 | }
|
---|
11 | });
|
---|
12 | Object.defineProperty(exports, "getFormat", {
|
---|
13 | enumerable: true,
|
---|
14 | get: function () {
|
---|
15 | return _colord.getFormat;
|
---|
16 | }
|
---|
17 | });
|
---|
18 |
|
---|
19 | var _colord = require("colord");
|
---|
20 |
|
---|
21 | var _names = _interopRequireDefault(require("colord/plugins/names"));
|
---|
22 |
|
---|
23 | var _getShortestString = _interopRequireDefault(require("./getShortestString"));
|
---|
24 |
|
---|
25 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
26 |
|
---|
27 | let minifierPlugin = Colord => {
|
---|
28 | /**
|
---|
29 | * Shortens a color to 3 or 4 digit hexadecimal string if it's possible.
|
---|
30 | * Returns the original (6 or 8 digit) hex if the it can't be shortened.
|
---|
31 | */
|
---|
32 | Colord.prototype.toShortHex = function ({
|
---|
33 | formatAlpha
|
---|
34 | }) {
|
---|
35 | let hex = this.toHex();
|
---|
36 | let [, r1, r2, g1, g2, b1, b2, a1, a2] = hex.split(''); // Check if the string can be shorten
|
---|
37 |
|
---|
38 | if (r1 === r2 && g1 === g2 && b1 === b2) {
|
---|
39 | if (this.alpha() === 1) {
|
---|
40 | // Express as 3 digit hexadecimal string if the color doesn't have an alpha channel
|
---|
41 | return '#' + r1 + g1 + b1;
|
---|
42 | } else if (formatAlpha && a1 === a2) {
|
---|
43 | // Format 4 digit hex
|
---|
44 | return '#' + r1 + g1 + b1 + a1;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | return hex;
|
---|
49 | };
|
---|
50 | /**
|
---|
51 | * Returns the shortest representation of a color.
|
---|
52 | */
|
---|
53 |
|
---|
54 |
|
---|
55 | Colord.prototype.toShortString = function ({
|
---|
56 | supportsTransparent,
|
---|
57 | supportsAlphaHex
|
---|
58 | }) {
|
---|
59 | let {
|
---|
60 | r,
|
---|
61 | g,
|
---|
62 | b
|
---|
63 | } = this.toRgb();
|
---|
64 | let a = this.alpha(); // RGB[A] and HSL[A] functional notations
|
---|
65 |
|
---|
66 | let options = [this.toRgbString(), // e.g. "rgb(128, 128, 128)" or "rgba(128, 128, 128, 0.5)"
|
---|
67 | this.toHslString() // e.g. "hsl(180, 50%, 50%)" or "hsla(180, 50%, 50%, 0.5)"
|
---|
68 | ]; // Hexadecimal notations
|
---|
69 |
|
---|
70 | if (supportsAlphaHex && a < 1) {
|
---|
71 | let alphaHex = this.toShortHex({
|
---|
72 | formatAlpha: true
|
---|
73 | }); // e.g. "#7777" or "#80808080"
|
---|
74 | // Output 4 or 8 digit hex only if the color conversion is lossless
|
---|
75 |
|
---|
76 | if ((0, _colord.colord)(alphaHex).alpha() === a) {
|
---|
77 | options.push(alphaHex);
|
---|
78 | }
|
---|
79 | } else if (a === 1) {
|
---|
80 | options.push(this.toShortHex({
|
---|
81 | formatAlpha: false
|
---|
82 | })); // e.g. "#777" or "#808080"
|
---|
83 | } // CSS keyword
|
---|
84 |
|
---|
85 |
|
---|
86 | if (supportsTransparent && r === 0 && g === 0 && b === 0 && a === 0) {
|
---|
87 | options.push('transparent');
|
---|
88 | } else if (a === 1) {
|
---|
89 | let name = this.toName(); // e.g. "gray"
|
---|
90 |
|
---|
91 | if (name) {
|
---|
92 | options.push(name);
|
---|
93 | }
|
---|
94 | } // Find the shortest option available
|
---|
95 |
|
---|
96 |
|
---|
97 | return (0, _getShortestString.default)(options);
|
---|
98 | };
|
---|
99 | };
|
---|
100 |
|
---|
101 | (0, _colord.extend)([_names.default, minifierPlugin]); |
---|