How to generate lighter/darker color with PHP?

前端 未结 7 1302
北恋
北恋 2020-12-12 12:31

I have a hex value of some color, for example #202010.

How to generate a new color which is either lighter or darker given in percent (ie. 20% dar

7条回答
  •  无人及你
    2020-12-12 13:28

    I was interested in this, but my question was how do I add a opacity to a colour?

    I wanted a colour to fade, not made lighter. i found this: http://www.gidnetwork.com/b-135.html and it worked great- code posted from original site for SO readers.

    function color_blend_by_opacity( $foreground, $opacity, $background=null )
    {
        static $colors_rgb=array(); // stores colour values already passed through the hexdec() functions below.
        $foreground = str_replace('#','',$foreground);
        if( is_null($background) )
            $background = 'FFFFFF'; // default background.
    
        $pattern = '~^[a-f0-9]{6,6}$~i'; // accept only valid hexadecimal colour values.
        if( !@preg_match($pattern, $foreground)  or  !@preg_match($pattern, $background) )
        {
            trigger_error( "Invalid hexadecimal colour value(s) found", E_USER_WARNING );
            return false;
        }
    
        $opacity = intval( $opacity ); // validate opacity data/number.
        if( $opacity>100  || $opacity<0 )
        {
            trigger_error( "Opacity percentage error, valid numbers are between 0 - 100", E_USER_WARNING );
            return false;
        }
    
        if( $opacity==100 )    // $transparency == 0
            return strtoupper( $foreground );
        if( $opacity==0 )    // $transparency == 100
            return strtoupper( $background );
        // calculate $transparency value.
        $transparency = 100-$opacity;
    
        if( !isset($colors_rgb[$foreground]) )
        { // do this only ONCE per script, for each unique colour.
            $f = array(  'r'=>hexdec($foreground[0].$foreground[1]),
                         'g'=>hexdec($foreground[2].$foreground[3]),
                         'b'=>hexdec($foreground[4].$foreground[5])    );
            $colors_rgb[$foreground] = $f;
        }
        else
        { // if this function is used 100 times in a script, this block is run 99 times.  Efficient.
            $f = $colors_rgb[$foreground];
        }
    
        if( !isset($colors_rgb[$background]) )
        { // do this only ONCE per script, for each unique colour.
            $b = array(  'r'=>hexdec($background[0].$background[1]),
                         'g'=>hexdec($background[2].$background[3]),
                         'b'=>hexdec($background[4].$background[5])    );
            $colors_rgb[$background] = $b;
        }
        else
        { // if this FUNCTION is used 100 times in a SCRIPT, this block will run 99 times.  Efficient.
            $b = $colors_rgb[$background];
        }
    
        $add = array(    'r'=>( $b['r']-$f['r'] ) / 100,
                         'g'=>( $b['g']-$f['g'] ) / 100,
                         'b'=>( $b['b']-$f['b'] ) / 100    );
    
        $f['r'] += intval( $add['r'] * $transparency );
        $f['g'] += intval( $add['g'] * $transparency );
        $f['b'] += intval( $add['b'] * $transparency );
    
        return sprintf( '%02X%02X%02X', $f['r'], $f['g'], $f['b'] );
    }
    

提交回复
热议问题