wp_tinycolor_bound01( mixed $n, int $max ): float

This function has been deprecated.

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Takes input from [0, n] and returns it as [0, 1].

Description

Direct port of TinyColor’s function, lightly simplified to maintain consistency with TinyColor.

Parameters

$nmixedrequired
Number of unknown type.
$maxintrequired
Upper value of the range to bound to.

Return

float Value in the range [0, 1].

Source

function wp_tinycolor_bound01( $n, $max ) {
	_deprecated_function( __FUNCTION__, '6.3.0' );
	if ( 'string' === gettype( $n ) && str_contains( $n, '.' ) && 1 === (float) $n ) {
		$n = '100%';
	}

	$n = min( $max, max( 0, (float) $n ) );

	// Automatically convert percentage into number.
	if ( 'string' === gettype( $n ) && str_contains( $n, '%' ) ) {
		$n = (int) ( $n * $max ) / 100;
	}

	// Handle floating point rounding errors.
	if ( ( abs( $n - $max ) < 0.000001 ) ) {
		return 1.0;
	}

	// Convert into [0, 1] range if it isn't already.
	return ( $n % $max ) / (float) $max;
}

Changelog

VersionDescription
6.3.0This function has been deprecated.
5.8.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.