PHP Classes

File: src/helpers.php

Recommend this page to a friend!
  Classes of Robert Devenyi   RGB Primary   src/helpers.php   Download  
File: src/helpers.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: RGB Primary
Change the brightness of a color in hexadecimal
Author: By
Last change:
Date: 1 month ago
Size: 633 bytes
 

Contents

Class file image Download
<?php
if (! function_exists('adjust_brightness')) {
    function
adjust_brightness($hex, $steps)
    {
       
$steps = max(-255, min(255, $steps));

       
$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);
        }

       
$color_parts = str_split($hex, 2);
       
$return = '';

        foreach (
$color_parts as $color) {
           
$color = hexdec($color);
           
$color = max(0,min(255,$color + $steps));
           
$return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT);
        }

    return
$return;
     }
}
?>