su_adjust_brightness

The shortcodes-ultimate su adjust brightness function.

Published Date - February 15, 2021

Description

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

su_adjust_brightness( $color, $percent );

Parameters

  1. $color

  2. $percent


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_adjust_brightness' ) ) {
    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_brightness( $color, $percent );
            

Defined

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

shortcodes-ultimate/includes/functions-colors.php

function su_adjust_brightness( $color, $percent ) {

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

	$percent = max( -100, min( 100, $percent ) );
	$steps   = round( $percent * 2.55 );
	$color   = ltrim( $color, '#' );

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

	$color_parts = str_split( $color, 2 );
	$new_color   = '#';

	foreach ( $color_parts as $color_part ) {

		$color_part = hexdec( $color_part );
		$color_part = max( 0, min( 255, $color_part + $steps ) );

		$new_color .= str_pad( dechex( $color_part ), 2, '0', STR_PAD_LEFT );

	}

	return $new_color;

}