diff --git a/blocks/duotone-filter/duotone.php b/blocks/duotone-filter/duotone.php
index 68390c77..f04f8331 100644
--- a/blocks/duotone-filter/duotone.php
+++ b/blocks/duotone-filter/duotone.php
@@ -1,35 +1,22 @@
$r / 0xff,
- 'g' => $g / 0xff,
- 'b' => $b / 0xff,
- );
- }
-}
+include_once 'utils.php';
// phpcs:disable
$duotone_id = $block['attrs']['duotoneId'];
+$duotone_selector = '.wp-block-image.' . $duotone_id . ' img';
$duotone_dark = hex2rgb( $block['attrs']['duotoneDark'] );
$duotone_light = hex2rgb( $block['attrs']['duotoneLight'] );
// phpcs:enable
?>
+
+
+
+ >
);
}
diff --git a/blocks/duotone-filter/src/index.js b/blocks/duotone-filter/src/index.js
index b0b432e5..d3001104 100644
--- a/blocks/duotone-filter/src/index.js
+++ b/blocks/duotone-filter/src/index.js
@@ -1,8 +1,14 @@
+/**
+ * External dependencies
+ */
+import classnames from 'classnames';
+
/**
* WordPress dependencies
*/
import { InspectorControls, PanelColorSettings } from '@wordpress/block-editor';
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
+import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
@@ -11,8 +17,12 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import Duotone from './duotone';
+import { hex2rgb } from './utils';
-const SUPPORTED_BLOCKS = [ 'core/image', 'core/cover' ];
+const FALLBACK_DARK_COLOR = '#000';
+const FALLBACK_LIGHT_COLOR = '#fff';
+
+const SUPPORTED_BLOCKS = [ 'core/image' ];
export const isSupportedBlock = ( blockName ) =>
SUPPORTED_BLOCKS.includes( blockName );
@@ -34,9 +44,20 @@ const withDuotoneAttributes = ( settings, blockName ) => {
return settings;
};
+/**
+ * Convert a hex color to perceived brightness.
+ *
+ * @param {string} color Hex color
+ * @return {number} Perceived brightness of the color
+ */
+const toBrightness = ( color ) => {
+ const { r, g, b } = hex2rgb( color );
+ return r * 0.299 + g * 0.587 + b * 0.114;
+};
+
const withDuotoneEditorControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
- const { name: blockName, attributes, setAttributes } = props;
+ const { name: blockName, attributes, setAttributes, clientId } = props;
if ( ! isSupportedBlock( blockName ) ) {
return ;
@@ -50,6 +71,29 @@ const withDuotoneEditorControls = createHigherOrderComponent(
} );
}, [ instanceId ] );
+ const [
+ defaultDarkColor = FALLBACK_DARK_COLOR,
+ defaultLightColor = FALLBACK_LIGHT_COLOR,
+ ] = useSelect( ( select ) => {
+ const { colors } = select( 'core/block-editor' ).getSettings();
+ return colors
+ .map( ( { color } ) => ( {
+ color,
+ brightness: toBrightness( color ),
+ } ) )
+ .reduce( ( [ min, max ], current ) => {
+ return [
+ ! min || current.brightness < min.brightness
+ ? current
+ : min,
+ ! max || current.brightness > max.brightness
+ ? current
+ : max,
+ ];
+ }, [] )
+ .map( ( { color } ) => color );
+ }, [] );
+
return (
<>
@@ -60,38 +104,55 @@ const withDuotoneEditorControls = createHigherOrderComponent(
{
label: __( 'Dark Color', 'block-experiments' ),
value: attributes.duotoneDark,
- onChange: ( duotoneDark ) =>
- setAttributes( { duotoneDark } ),
+ onChange: ( duotoneDark ) => {
+ if (
+ duotoneDark &&
+ ! attributes.duotoneLight
+ ) {
+ setAttributes( {
+ duotoneLight:
+ duotoneDark !==
+ defaultLightColor
+ ? defaultLightColor
+ : FALLBACK_LIGHT_COLOR,
+ } );
+ }
+ setAttributes( { duotoneDark } );
+ },
},
{
label: __( 'Light Color', 'block-experiments' ),
value: attributes.duotoneLight,
- onChange: ( duotoneLight ) =>
- setAttributes( { duotoneLight } ),
+ onChange: ( duotoneLight ) => {
+ if (
+ duotoneLight &&
+ ! attributes.duotoneDark
+ ) {
+ setAttributes( {
+ duotoneDark:
+ duotoneLight !==
+ defaultDarkColor
+ ? defaultDarkColor
+ : FALLBACK_DARK_COLOR,
+ } );
+ }
+ setAttributes( { duotoneLight } );
+ },
},
] }
/>
+
{ attributes.duotoneDark &&
attributes.duotoneLight &&
attributes.duotoneId ? (
- <>
-
-
-
-
- >
- ) : (
-
- ) }
+
+ ) : null }
>
);
},
@@ -108,9 +169,7 @@ function addDuotoneFilterStyle( props, block, attributes ) {
return props;
}
- const { style = {} } = props;
-
- return { style: { ...style, filter: `url( #${ attributes.duotoneId } )` } };
+ return { className: classnames( props.className, attributes.duotoneId ) };
}
export function registerBlock() {
diff --git a/blocks/duotone-filter/src/utils.js b/blocks/duotone-filter/src/utils.js
new file mode 100644
index 00000000..192e2d63
--- /dev/null
+++ b/blocks/duotone-filter/src/utils.js
@@ -0,0 +1,23 @@
+export const hex2rgb = ( color = '' ) => {
+ let r = '0';
+ let g = '0';
+ let b = '0';
+
+ if ( color.length === 4 ) {
+ r = '0x' + color[ 1 ] + color[ 1 ];
+ g = '0x' + color[ 2 ] + color[ 2 ];
+ b = '0x' + color[ 3 ] + color[ 3 ];
+ } else if ( color.length === 7 ) {
+ r = '0x' + color[ 1 ] + color[ 2 ];
+ g = '0x' + color[ 3 ] + color[ 4 ];
+ b = '0x' + color[ 5 ] + color[ 6 ];
+ } else {
+ return {};
+ }
+
+ return {
+ r: r / 0xff,
+ g: g / 0xff,
+ b: b / 0xff,
+ };
+};
diff --git a/blocks/duotone-filter/utils.php b/blocks/duotone-filter/utils.php
new file mode 100644
index 00000000..a2eed832
--- /dev/null
+++ b/blocks/duotone-filter/utils.php
@@ -0,0 +1,23 @@
+ $r / 0xff,
+ 'g' => $g / 0xff,
+ 'b' => $b / 0xff,
+ );
+ }
+}