forked from cssnano/cssnano
/
minifyColor.js
31 lines (28 loc) · 942 Bytes
/
minifyColor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { process } from './lib/color';
import getShortestString from './lib/getShortestString';
/**
* Performs color value minification
*
* @param {string} input - CSS value
* @param {boolean} options.supportsAlphaHex - Does the browser support 4 & 8 character hex notation
* @param {boolean} options.supportsTransparent – Does the browser support "transparent" value properly
*/
export default function minifyColor(input, options = {}) {
const settings = {
supportsAlphaHex: false,
supportsTransparent: true,
...options,
};
const instance = process(input);
if (instance.isValid()) {
// Try to shorten the string if it is a valid CSS color value.
// Fall back to the original input if it's smaller or has equal length/
return getShortestString([
input.toLowerCase(),
instance.toShortString(settings),
]);
} else {
// Possibly malformed, so pass through
return input;
}
}