su_adjust_lightness

The shortcodes-ultimate su adjust lightness function.

Published Date - February 15, 2021

Description

Helper function that adjusts lightness of a given HEX color value.

su_adjust_lightness( $color, $percent );

Parameters

  1. $color

  2. $percent


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_adjust_lightness' ) ) {
    require_once ABSPATH . PLUGINDIR . 'shortcodes-ultimateshortcodes-ultimate/includes/functions-colors.php';
}

// The input(s). 
$color = null;
$percent = null;

// NOTICE! Understand what this does before running.
$result = su_adjust_lightness( $color, $percent );
            

Defined

The function is defined in the following location(s).

shortcodes-ultimate/includes/functions-colors.php

function su_adjust_lightness( $color, $percent ) {

	if (
		! su_is_valid_hex( $color ) ||
		! is_numeric( $percent )
	) {
		return $color;
	}

	$percent   = max( -100, min( 100, $percent ) );
	$color     = ltrim( $color, '#' );
	$new_color = '#';

	if ( 3 === strlen( $color ) ) {
		$color = su_expand_short_color( $color );
	}

	$color = array_map( 'hexdec', str_split( $color, 2 ) );

	foreach ( $color as $part ) {

		$limit  = $percent < 0 ? $part : 255 - $part;
		$amount = ceil( $limit * $percent / 100 );

		$new_color .= str_pad( dechex( $part + $amount ), 2, '0', STR_PAD_LEFT );

	}

	return $new_color;

}