Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duotone: Add a posterize option #33673

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* WordPress dependencies
*/
import { Popover, MenuGroup, DuotonePicker } from '@wordpress/components';
import {
Popover,
MenuGroup,
DuotonePicker,
ToggleControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

function DuotonePickerPopover( {
Expand All @@ -12,6 +17,8 @@ function DuotonePickerPopover( {
colorPalette,
disableCustomColors,
disableCustomDuotone,
posterize,
onPosterizeToggle,
} ) {
return (
<Popover
Expand All @@ -28,6 +35,11 @@ function DuotonePickerPopover( {
value={ value }
onChange={ onChange }
/>
<ToggleControl
checked={ posterize }
onChange={ onPosterizeToggle }
label={ __( 'Posterize' ) }
/>
</MenuGroup>
</Popover>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function DuotoneControl( {
disableCustomDuotone,
value,
onChange,
onPosterizeToggle,
posterize,
} ) {
const [ isOpen, setIsOpen ] = useState( false );

Expand Down Expand Up @@ -52,6 +54,8 @@ function DuotoneControl( {
colorPalette={ colorPalette }
disableCustomColors={ disableCustomColors }
disableCustomDuotone={ disableCustomDuotone }
onPosterizeToggle={ onPosterizeToggle }
posterize={ posterize }
/>
) }
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
min-width: 214px;
}

.components-circular-option-picker {
.components-circular-option-picker,
.components-toggle-control {
padding: $grid-unit-15;
}

Expand Down
21 changes: 17 additions & 4 deletions packages/block-editor/src/hooks/duotone.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ export function getValuesFromColors( colors = [] ) {
* @param {string} props.selector Selector to apply the filter to.
* @param {string} props.id Unique id for this duotone filter.
* @param {Values} props.values R, G, and B values to filter with.
* @param {boolean} props.posterize Decides whether to use discrete values or table values for the filter.
*
* @return {WPElement} Duotone element.
*/
function DuotoneFilter( { selector, id, values } ) {
function DuotoneFilter( { selector, id, values, posterize } ) {
const stylesheet = `
${ selector } {
filter: url( #${ id } );
}
`;
const type = posterize ? 'discrete' : 'table';

return (
<>
Expand Down Expand Up @@ -104,15 +106,15 @@ ${ selector } {
colorInterpolationFilters="sRGB"
>
<feFuncR
type="table"
type={ type }
tableValues={ values.r.join( ' ' ) }
/>
<feFuncG
type="table"
type={ type }
tableValues={ values.g.join( ' ' ) }
/>
<feFuncB
type="table"
type={ type }
tableValues={ values.b.join( ' ' ) }
/>
</feComponentTransfer>
Expand All @@ -127,6 +129,7 @@ ${ selector } {
function DuotonePanel( { attributes, setAttributes } ) {
const style = attributes?.style;
const duotone = style?.color?.duotone;
const posterize = style?.posterize;

const duotonePalette = useSetting( 'color.duotone' ) || EMPTY_ARRAY;
const colorPalette = useSetting( 'color.palette' ) || EMPTY_ARRAY;
Expand Down Expand Up @@ -157,6 +160,14 @@ function DuotonePanel( { attributes, setAttributes } ) {
};
setAttributes( { style: newStyle } );
} }
posterize={ posterize }
onPosterizeToggle={ ( newPosterize ) => {
const newStyle = {
...style,
posterize: newPosterize,
};
setAttributes( { style: newStyle } );
} }
/>
</BlockControls>
);
Expand Down Expand Up @@ -227,6 +238,7 @@ const withDuotoneStyles = createHigherOrderComponent(
'color.__experimentalDuotone'
);
const values = props?.attributes?.style?.color?.duotone;
const posterize = props?.attributes?.style?.posterize;

if ( ! duotoneSupport || ! values ) {
return <BlockListBlock { ...props } />;
Expand All @@ -252,6 +264,7 @@ const withDuotoneStyles = createHigherOrderComponent(
selector={ selectorsGroup }
id={ id }
values={ getValuesFromColors( values ) }
posterize={ posterize }
/>,
element
) }
Expand Down