Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 PHP Code: Invert Hex Color Jump to:  
Category: >> PHP Code >> Invert Hex Color Bookmark and Share

<< lastnext >>

Snippet Name: Invert Hex Color

Description: Inverts a hexadecimal color value.

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: February 27th, 2009

<?PHP
 
FUNCTION inverseHex( $color )
{
     $color       = TRIM($color);
     $prependHash = FALSE;
 
     IF(STRPOS($color,'#')!==FALSE) {
          $prependHash = TRUE;
          $color       = STR_REPLACE('#',NULL,$color);
     }
 
     SWITCH($len=STRLEN($color)) {
          CASE 3:
               $color=PREG_REPLACE("/(.)(.)(.)/","\\1\\1\\2\\2\\3\\3",$color);
          CASE 6:
               BREAK;
          DEFAULT:
               TRIGGER_ERROR("Invalid hex length ($len). Must be (3) or (6)", E_USER_ERROR);
     }
 
     IF(!PREG_MATCH('/[a-f0-9]{6}/i',$color)) {
          $color = HTMLENTITIES($color);
          TRIGGER_ERROR( "Invalid hex string #$color", E_USER_ERROR );
     }
 
     $r = DECHEX(255-HEXDEC(SUBSTR($color,0,2)));
     $r = (STRLEN($r)>1)?$r:'0'.$r;
     $g = DECHEX(255-HEXDEC(SUBSTR($color,2,2)));
     $g = (STRLEN($g)>1)?$g:'0'.$g;
     $b = DECHEX(255-HEXDEC(SUBSTR($color,4,2)));
     $b = (STRLEN($b)>1)?$b:'0'.$b;
 
     RETURN ($prependHash?'#':NULL).$r.$g.$b;
}
 
// Demo
// echo inverseHex('#000000'); // #ffffff
?>


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 164 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?