MP_Settings::adjust_brightness( string $hex, int $steps )

Adjust Brightness.


Parameters Parameters

$hex

(string) (Required) color hex

$steps

(int) (Required) steps should be between -255 and 255. Negative = darker, positive = lighter


Top ↑

Return Return

(string)


Top ↑

Source Source

File: mortgage-platform/Inc/Base/MP_Settings.php

	public static function adjust_brightness( $hex, $steps ) {
		// Steps should be between -255 and 255. Negative = darker, positive = lighter
		$steps = max( - 255, min( 255, $steps ) );

		// Normalize into a six character long hex string
		$hex = str_replace( '#', '', $hex );
		if ( strlen( $hex ) === 3 ) {
			$hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 );
		}

		// Split into three parts: R, G and B
		$color_parts = str_split( $hex, 2 );
		$return      = '#';

		foreach ( $color_parts as $color ) {
			$color   = hexdec( $color ); // Convert to decimal
			$color   = max( 0, min( 255, $color + $steps ) ); // Adjust color
			$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
		}

		return $return;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.