a php developer weblog

blog Closed!
calin view of the web development world

2005/4/20

a PHP function to obtain a darker HTML colour code

@ 01:43 PM (39 months, 8 hours ago)

I needed a PHP function to return me a darker HTML colour code. I couldn't find one, therefore I wrote one. So here's a PHP code that randomly creates some HTML colour codes and applies the darkifier function. Here comes the source:

<?php
#    
#    filename:    darkifier.php
#    descr:        randomly generate 100 html color codes, then apply the darkerHtmlColor function
#    author:        calin uioreanu http://php9.bloghi.com
#


# generate 100 random colors
for ($count=0; $count<30; $count++) {
    
$PlotColors[] = '#'. sprintf("%06X", rand(0,pow(16,6)));
}

echo
    
'<table border="1">';
foreach (
$PlotColors as $htmlColor) {
    
$darkerColor = darkerHtmlColor($htmlColor);
    echo
        
'<tr>',
            
'<td>darkerHtmlColor(</td>',
            
'<td style="background-color:'. $htmlColor .'">', "n",
            
$htmlColor,
            
'</td>',
            
'<td>)=</td>',
            
'<td style="background-color:'. $darkerColor .'">', "n",
            
$darkerColor,
            
'</td>',
        
'</tr>',
        
'';
}
echo
    
'</table>';

/**
    html-color-code darkerHtmlColor(html-color-code)

    returns a "darker" version of the HTML color code provided.
*/
function darkerHtmlColor($htmlColor)
{
    
# input patch
    
if (substr($htmlColor, 0, 1) == '#') {
        
$htmlColor = substr($htmlColor, 1);
    }
    
# input check
    
if (strlen($htmlColor)!=6) {
        die (print
'darkerHtmlColor:: invalid parameter: '. $htmlColor);
    }
    
# str_split (php5)
    
$pieces = explode(' ', rtrim(chunk_split($htmlColor, 2, ' ')));
    foreach (
$pieces as $piece) {
        
# convert from base16 to base10, reduce the brightness then come back to base16
        
$darkpiece = (int) (base_convert($piece, 16, 10) / 2);
        
$darkerColor .= sprintf("%02x", $darkpiece);
    }
    return
'#'. $darkerColor;
}
// end func


?>

 

And here's some output:

darkerHtmlColor( #13B540 )= #095a20
darkerHtmlColor( #DF1AE8 )= #6f0d74
darkerHtmlColor( #294287 )= #142143
darkerHtmlColor( #33E807 )= #197403
darkerHtmlColor( #96E440 )= #4b7220
darkerHtmlColor( #0E6DB2 )= #073659
darkerHtmlColor( #C0350D )= #601a06
darkerHtmlColor( #1B9C73 )= #0d4e39
darkerHtmlColor( #DB3AC7 )= #6d1d63
darkerHtmlColor( #5288D7 )= #29446b
darkerHtmlColor( #54528F )= #2a2947
darkerHtmlColor( #E34C05 )= #712602
darkerHtmlColor( #A60BA1 )= #530550
darkerHtmlColor( #B5B483 )= #5a5a41
darkerHtmlColor( #FCAC1F )= #7e560f
darkerHtmlColor( #93B616 )= #495b0b
darkerHtmlColor( #53E9DD )= #29746e
darkerHtmlColor( #1234D4 )= #091a6a
darkerHtmlColor( #6DE2A5 )= #367152
darkerHtmlColor( #39BBE0 )= #1c5d70
darkerHtmlColor( #316EA2 )= #183751
darkerHtmlColor( #8C847E )= #46423f
darkerHtmlColor( #63E4AC )= #317256
darkerHtmlColor( #AEEA2D )= #577516
darkerHtmlColor( #90BB41 )= #485d20
darkerHtmlColor( #91C9BA )= #48645d
darkerHtmlColor( #DF4F12 )= #6f2709
darkerHtmlColor( #27CCFE )= #13667f
darkerHtmlColor( #5744A1 )= #2b2250
darkerHtmlColor( #5FF19F )= #2f784f

I'd like to know you found this useful.