Skip to content

Instantly share code, notes, and snippets.

@kijtra
Created November 29, 2011 03:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kijtra/1403336 to your computer and use it in GitHub Desktop.
Save kijtra/1403336 to your computer and use it in GitHub Desktop.
[PHP] ある色の補色(反対色)を返す関数。計算ではなく単に反対の文字を返すだけなのでRGB値には非対応・・・
<?php
function complementary($hex){
static $_out;
if(!empty($_out[$hex])){
return $_out[$hex];
}
$_out=array();
$in=(strpos($hex,'#')===0) ? substr($hex,1) : $hex;
$in=strtoupper($in);
if(strlen($in)==3){
$r=substr($in,0,1);
$g=substr($in,1,1);
$b=substr($in,2,1);
$in=$r.$r.$g.$g.$b.$b;
}
$a='0123456789ABCDEF';
$b='FEDCBA9876543210';
$out=NULL;
foreach(str_split($in) as $v){
$out.=$b{strpos($a,$v)};
}
return $_out[$hex]=$out;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment