Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(postcss-colormin): Output 4 and 8 digit hex colors (#1109)
* Output 4 and 8 digit hex colors * Check browser support by caniuse-api * Update specs * Update postcss-colormin specs * Tweak integration tests * Rename colours to minifyColor * Deduplicate packages * Fix `minifyColor` import * Rebase * Fix lock file * Fix linter error * Improve resulting alpha precition * Lossless conversion
- Loading branch information
Vlad Shilov
committed
May 28, 2021
1 parent
f2a8a1a
commit 3168399
Showing
15 changed files
with
215 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,9 @@ | |
"chrome58": [ | ||
"Chrome 58" | ||
], | ||
"chrome62": [ | ||
"Chrome 62" | ||
], | ||
"edge15": [ | ||
"Edge 15" | ||
], | ||
|
2 changes: 1 addition & 1 deletion
2
packages/cssnano-preset-advanced/src/__tests__/integrations/bootstrap-v3.4.0.css
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/cssnano-preset-advanced/src/__tests__/integrations/bootstrap-v4.2.1.css
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/cssnano-preset-default/src/__tests__/integrations/bootstrap-v3.4.0.css
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/cssnano-preset-default/src/__tests__/integrations/bootstrap-v4.2.1.css
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { colord, extend, getFormat } from 'colord'; | ||
import namesPlugin from 'colord/plugins/names'; | ||
import getShortestString from './getShortestString'; | ||
|
||
let minifierPlugin = (Colord) => { | ||
/** | ||
* Shortens a color to 3 or 4 digit hexadecimal string if it's possible. | ||
* Returns the original (6 or 8 digit) hex if the it can't be shortened. | ||
*/ | ||
Colord.prototype.toShortHex = function ({ formatAlpha }) { | ||
let hex = this.toHex(); | ||
let [, r1, r2, g1, g2, b1, b2, a1, a2] = hex.split(''); | ||
|
||
// Check if the string can be shorten | ||
if (r1 === r2 && g1 === g2 && b1 === b2) { | ||
if (this.alpha() === 1) { | ||
// Express as 3 digit hexadecimal string if the color doesn't have an alpha channel | ||
return '#' + r1 + g1 + b1; | ||
} else if (formatAlpha && a1 === a2) { | ||
// Format 4 digit hex | ||
return '#' + r1 + g1 + b1 + a1; | ||
} | ||
} | ||
|
||
return hex; | ||
}; | ||
|
||
/** | ||
* Returns the shortest representation of a color. | ||
*/ | ||
Colord.prototype.toShortString = function ({ | ||
supportsTransparent, | ||
supportsAlphaHex, | ||
}) { | ||
let { r, g, b } = this.toRgb(); | ||
let a = this.alpha(); | ||
|
||
// RGB[A] and HSL[A] functional notations | ||
let options = [ | ||
this.toRgbString(), // e.g. "rgb(128, 128, 128)" or "rgba(128, 128, 128, 0.5)" | ||
this.toHslString(), // e.g. "hsl(180, 50%, 50%)" or "hsla(180, 50%, 50%, 0.5)" | ||
]; | ||
|
||
// Hexadecimal notations | ||
if (supportsAlphaHex && a < 1) { | ||
let alphaHex = this.toShortHex({ formatAlpha: true }); // e.g. "#7777" or "#80808080" | ||
|
||
// Output 4 or 8 digit hex only if the color conversion is lossless | ||
if (colord(alphaHex).alpha() === a) { | ||
options.push(alphaHex); | ||
} | ||
} else if (a === 1) { | ||
options.push(this.toShortHex({ formatAlpha: false })); // e.g. "#777" or "#808080" | ||
} | ||
|
||
// CSS keyword | ||
if (supportsTransparent && r === 0 && g === 0 && b === 0 && a === 0) { | ||
options.push('transparent'); | ||
} else if (a === 1) { | ||
let name = this.toName(); // e.g. "gray" | ||
if (name) { | ||
options.push(name); | ||
} | ||
} | ||
|
||
// Find the shortest option available | ||
return getShortestString(options); | ||
}; | ||
}; | ||
|
||
extend([namesPlugin, minifierPlugin]); | ||
|
||
export { colord as process, getFormat }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Returns the shortest string in array | ||
*/ | ||
const getShortestString = (strings) => { | ||
let shortest = null; | ||
|
||
for (let string of strings) { | ||
if (shortest === null || string.length < shortest.length) { | ||
shortest = string; | ||
} | ||
} | ||
|
||
return shortest; | ||
}; | ||
|
||
export default getShortestString; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
Oops, something went wrong.