WordPress at Your Fingertips
function is not described

wp_tinycolor_bound01() WP 5.8.0

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

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

No Hooks.

Return

float. Value in the range [0, 1].

Usage

wp_tinycolor_bound01( $n, $max );
$n(mixed) (required)
Number of unknown type.
$max(int) (required)
Upper value of the range to bound to.

Notes

Changelog

Since 5.8.0 Introduced.

Code of wp_tinycolor_bound01() WP 5.8.1

function wp_tinycolor_bound01( $n, $max ) {
	if ( 'string' === gettype( $n ) && false !== strpos( $n, '.' ) && 1 === (float) $n ) {
		$n = '100%';
	}

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

	// Automatically convert percentage into number.
	if ( 'string' === gettype( $n ) && false !== strpos( $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;
}