Changeset 265 for branches/1.1dev/lib


Ignore:
Timestamp:
Jul 3, 2007 3:35:31 AM (17 years ago)
Author:
quinn
Message:

Improved(?) getTextColor function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.1dev/lib/Utilities.inc.php

    r206 r265  
    173173   
    174174/**
    175  * Generates a hexadecibal html color based on provided word.
     175 * Generates a hexadecimal html color based on provided word.
    176176 *
    177177 * @access public
     
    183183function getTextColor($text, $method=1)
    184184{
    185     $r = substr(md5($text), 0, 1);
    186     $g = substr(md5($text), 1, 1);
    187     $b = substr(md5($text), 2, 1);
     185    $hash = md5($text);
     186    $rgb = array(
     187        substr($hash, 0, 1),
     188        substr($hash, 1, 1),
     189        substr($hash, 2, 1),
     190        substr($hash, 3, 1),
     191        substr($hash, 4, 1),
     192        substr($hash, 5, 1),
     193    );
    188194
    189195    switch ($method) {
     196    case 1 :
     197    default :
     198        // Reduce all hex values slighly to avoid all white.
     199        array_walk($rgb, create_function('&$v', '$v = dechex(round(hexdec($v) * 0.87));'));
     200        break;
    190201    case 2 :
    191         if (hexdec($r) > hexdec('c')) {
    192             $r = dechex(hexdec('f') - hexdec($r));
    193         }
    194         if (hexdec($g) > hexdec('c')) {
    195             $g = dechex(hexdec('f') - hexdec($g));
    196         }
    197         if (hexdec($b) > hexdec('c')) {
    198             $b = dechex(hexdec('f') - hexdec($b));
     202        foreach ($rgb as $i => $v) {
     203            if (hexdec($v) > hexdec('c')) {
     204                $rgb[$i] = dechex(hexdec('f') - hexdec($v));
     205            }
    199206        }
    200207        break;
    201        
    202     case 1 :
    203     default :
    204         $r = dechex(round(hexdec($r) * .8));
    205         $g = dechex(round(hexdec($g) * .8));
    206         $b = dechex(round(hexdec($b) * .6));
    207         break;
    208     }
    209 
    210     return $r . $r . $g . $g . $b . $b;
     208    }
     209
     210    return join('', $rgb);
    211211}
    212212
Note: See TracChangeset for help on using the changeset viewer.