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

Port colord to PHP #49700

Merged
merged 4 commits into from
Apr 18, 2023
Merged
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
98 changes: 28 additions & 70 deletions lib/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
*
* @see https://github.com/bgrins/TinyColor
*
* @deprecated 6.3.0
*
* @param mixed $n Number of unknown type.
* @param int $max Upper value of the range to bound to.
* @return float Value in the range [0,1].
*/
function gutenberg_tinycolor_bound01( $n, $max ) {
_deprecated_function( __FUNCTION__, '6.3.0' );

if ( 'string' === gettype( $n ) && str_contains( $n, '.' ) && 1 === (float) $n ) {
$n = '100%';
}
Expand All @@ -42,10 +46,14 @@ function gutenberg_tinycolor_bound01( $n, $max ) {
*
* @see https://github.com/bgrins/TinyColor
*
* @deprecated 6.3.0
*
* @param mixed $n Number of unknown type.
* @return float Value in the range [0,1].
*/
function gutenberg_tinycolor_bound_alpha( $n ) {
_deprecated_function( __FUNCTION__, '6.3.0' );

if ( is_numeric( $n ) ) {
$n = (float) $n;
if ( $n >= 0 && $n <= 1 ) {
Expand All @@ -60,10 +68,14 @@ function gutenberg_tinycolor_bound_alpha( $n ) {
*
* @see https://github.com/bgrins/TinyColor
*
* @deprecated 6.3.0
*
* @param array $rgb_color RGB object.
* @return array Rounded and converted RGB object.
*/
function gutenberg_tinycolor_rgb_to_rgb( $rgb_color ) {
_deprecated_function( __FUNCTION__, '6.3.0' );

return array(
'r' => gutenberg_tinycolor_bound01( $rgb_color['r'], 255 ) * 255,
'g' => gutenberg_tinycolor_bound01( $rgb_color['g'], 255 ) * 255,
Expand All @@ -76,12 +88,16 @@ function gutenberg_tinycolor_rgb_to_rgb( $rgb_color ) {
*
* @see https://github.com/bgrins/TinyColor
*
* @deprecated 6.3.0
*
* @param float $p first component.
* @param float $q second component.
* @param float $t third component.
* @return float R, G, or B component.
*/
function gutenberg_tinycolor_hue_to_rgb( $p, $q, $t ) {
_deprecated_function( __FUNCTION__, '6.3.0' );

if ( $t < 0 ) {
++$t;
}
Expand All @@ -105,10 +121,14 @@ function gutenberg_tinycolor_hue_to_rgb( $p, $q, $t ) {
*
* @see https://github.com/bgrins/TinyColor
*
* @deprecated 6.3.0
*
* @param array $hsl_color HSL object.
* @return array Rounded and converted RGB object.
*/
function gutenberg_tinycolor_hsl_to_rgb( $hsl_color ) {
_deprecated_function( __FUNCTION__, '6.3.0' );

$h = gutenberg_tinycolor_bound01( $hsl_color['h'], 360 );
$s = gutenberg_tinycolor_bound01( $hsl_color['s'], 100 );
$l = gutenberg_tinycolor_bound01( $hsl_color['l'], 100 );
Expand Down Expand Up @@ -140,10 +160,14 @@ function gutenberg_tinycolor_hsl_to_rgb( $hsl_color ) {
* @see https://github.com/bgrins/TinyColor
* @see https://github.com/casesandberg/react-color/
*
* @deprecated 6.3.0
*
* @param string $color_str CSS color string.
* @return array RGB object.
*/
function gutenberg_tinycolor_string_to_rgb( $color_str ) {
_deprecated_function( __FUNCTION__, '6.3.0' );

$color_str = strtolower( trim( $color_str ) );

$css_integer = '[-\\+]?\\d+%?';
Expand Down Expand Up @@ -321,80 +345,14 @@ function gutenberg_get_duotone_filter_property( $preset ) {
/**
* Returns the duotone filter SVG string for the preset.
*
* @deprecated 6.3.0
*
* @param array $preset Duotone preset value as seen in theme.json.
* @return string Duotone SVG filter.
*/
function gutenberg_get_duotone_filter_svg( $preset ) {
$filter_id = gutenberg_get_duotone_filter_id( $preset );

$duotone_values = array(
'r' => array(),
'g' => array(),
'b' => array(),
'a' => array(),
);

if ( ! isset( $preset['colors'] ) || ! is_array( $preset['colors'] ) ) {
$preset['colors'] = array();
}

foreach ( $preset['colors'] as $color_str ) {
$color = gutenberg_tinycolor_string_to_rgb( $color_str );

$duotone_values['r'][] = $color['r'] / 255;
$duotone_values['g'][] = $color['g'] / 255;
$duotone_values['b'][] = $color['b'] / 255;
$duotone_values['a'][] = $color['a'];
}

ob_start();

?>

<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 0 0"
width="0"
height="0"
focusable="false"
role="none"
style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
>
<defs>
<filter id="<?php echo esc_attr( $filter_id ); ?>">
<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
"
/>
<feComponentTransfer color-interpolation-filters="sRGB" >
<feFuncR type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['r'] ) ); ?>" />
<feFuncG type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['g'] ) ); ?>" />
<feFuncB type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['b'] ) ); ?>" />
<feFuncA type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['a'] ) ); ?>" />
</feComponentTransfer>
<feComposite in2="SourceGraphic" operator="in" />
</filter>
</defs>
</svg>

<?php

$svg = ob_get_clean();

if ( ! SCRIPT_DEBUG ) {
// Clean up the whitespace.
$svg = preg_replace( "/[\r\n\t ]+/", ' ', $svg );
$svg = str_replace( '> <', '><', $svg );
$svg = trim( $svg );
}

return $svg;
_deprecated_function( __FUNCTION__, '6.3.0' );
return WP_Duotone_Gutenberg::get_filter_svg_from_preset( $preset );
}

/**
Expand Down
Loading