@wordpress/block-editor
Version:
60 lines (59 loc) • 2.08 kB
JavaScript
// packages/block-editor/src/components/duotone/utils.js
import { colord } from "colord";
function getValuesFromColors(colors = []) {
const values = { r: [], g: [], b: [], a: [] };
colors.forEach((color) => {
const rgbColor = colord(color).toRgb();
values.r.push(rgbColor.r / 255);
values.g.push(rgbColor.g / 255);
values.b.push(rgbColor.b / 255);
values.a.push(rgbColor.a);
});
return values;
}
function getDuotoneUnsetStylesheet(selector) {
return `${selector}{filter:none}`;
}
function getDuotoneStylesheet(selector, id) {
return `${selector}{filter:url(#${id})}`;
}
function getDuotoneFilter(id, colors) {
const values = getValuesFromColors(colors);
return `
<svg
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 0 0"
width="0"
height="0"
focusable="false"
role="none"
aria-hidden="true"
style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
>
<defs>
<filter id="${id}">
<!--
Use sRGB instead of linearRGB so transparency looks correct.
Use perceptual brightness to convert to grayscale.
-->
<feColorMatrix color-interpolation-filters="sRGB" type="matrix" values=" .299 .587 .114 0 0 .299 .587 .114 0 0 .299 .587 .114 0 0 .299 .587 .114 0 0 "></feColorMatrix>
<!-- Use sRGB instead of linearRGB to be consistent with how CSS gradients work. -->
<feComponentTransfer color-interpolation-filters="sRGB">
<feFuncR type="table" tableValues="${values.r.join(" ")}"></feFuncR>
<feFuncG type="table" tableValues="${values.g.join(" ")}"></feFuncG>
<feFuncB type="table" tableValues="${values.b.join(" ")}"></feFuncB>
<feFuncA type="table" tableValues="${values.a.join(" ")}"></feFuncA>
</feComponentTransfer>
<!-- Re-mask the image with the original transparency since the feColorMatrix above loses that information. -->
<feComposite in2="SourceGraphic" operator="in"></feComposite>
</filter>
</defs>
</svg>`;
}
export {
getDuotoneFilter,
getDuotoneStylesheet,
getDuotoneUnsetStylesheet,
getValuesFromColors
};
//# sourceMappingURL=utils.js.map