This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Colorsys object (for playing with colors)
- Loading branch information
Showing
1 changed file
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
/** | ||
* @category Taplod | ||
* @package Taplod_Colorsys | ||
* @copyright Copyright (c) 2009, Bellière Ludovic | ||
* @license http://opensource.org/licenses/mit-license.php MIT license | ||
*/ | ||
|
||
class Taplod_Colorsys { | ||
|
||
const HEX = 'hex'; | ||
const RGB = 'rgb'; | ||
|
||
protected $_type; | ||
protected $_color = array( | ||
self::HEX => '', | ||
self::RGB => '' | ||
); | ||
protected $_current_color; | ||
|
||
function __construct($color) { | ||
if (!is_array($color)) { | ||
if ($color == 'random') | ||
$color = self::random('hex'); | ||
$color = str_replace('#','',$color); | ||
$this->_type = self::HEX; | ||
$this->_current_color = $color; | ||
$this->_color = array( | ||
self::RGB => self::hex2rgb(), | ||
self::HEX => $color | ||
); | ||
} else { | ||
$this->_type = self::RGB; | ||
|
||
$this->_current_color = $color; | ||
$this->_color = array( | ||
self::RGB => $color, | ||
self::HEX => $color | ||
); | ||
} | ||
} | ||
|
||
function getRgb() { | ||
return $this->_color[self::RGB]; | ||
} | ||
|
||
function getHex() { | ||
return $this->_color[self::HEX]; | ||
} | ||
|
||
function revert($hex=false) { | ||
if (!$hex) { | ||
if ($this->_type == self::HEX) | ||
$hex = $this->_current_color; | ||
else | ||
return $this->_current_color; | ||
} else { | ||
if ($hex[0] == "#") { | ||
$hex = substr($hex,1); | ||
} | ||
} | ||
|
||
$r = str_pad(dechex(255 - hexdec(substr($hex,0,2))),2,0); | ||
$g = str_pad(dechex(255 - hexdec(substr($hex,2,2))),2,0); | ||
$b = str_pad(dechex(255 - hexdec(substr($hex,-2))),2,0); | ||
|
||
return "#$r$g$b"; | ||
} | ||
|
||
function rgb2hex($rgb=false) { | ||
if (!$rgb) { | ||
if ($this->_type == self::RGB) | ||
$rgb = $this->_current_color; | ||
else | ||
return $this->current_color; | ||
} else { | ||
if (!is_array($rgb)) { | ||
throw new exception('Invalid type given for RGB. An array is expected, '.gettype($rgb).' given.'); | ||
} | ||
} | ||
|
||
$r = dechex(substr($rgb,0,3)); | ||
$g = dechex(substr($rgb,0,3)); | ||
$b = dechex(substr($rgb,-3)); | ||
|
||
return compact('r','g','b'); | ||
} | ||
|
||
function hex2rgb($hex=false) { | ||
if (!$hex) { | ||
if ($this->_type == self::HEX) | ||
$hex = $this->_current_color; | ||
else | ||
return $this->_current_color; | ||
} | ||
|
||
$r = hexdec(substr($hex,0,2)); | ||
$g = hexdec(substr($hex,2,2)); | ||
$b = hexdec(substr($hex,-2)); | ||
|
||
return compact('r', 'g', 'b'); | ||
} | ||
|
||
static function rgb2hsv($r, $g=0, $b=0) { | ||
if (is_array($r) && count($r)==3) { | ||
$b = $r['b']; | ||
$g = $r['g']; | ||
$r = $r['r']; | ||
} | ||
$max = max($r, $g, $b); | ||
$min = min($r, $g, $b); | ||
$delta = $max-$min; | ||
|
||
if ($max == 0) | ||
return array('h'=>0, 's'=>0, 'v'=>0); | ||
|
||
$s = $delta / $max; | ||
$v = $max; | ||
|
||
switch ($max) { | ||
case $g: | ||
if ($delta != 0) { | ||
$h = 2 + ($b - $r) / $delta; | ||
} else { | ||
$s = 0; | ||
$h = 2 + $b - $r; | ||
} | ||
break; | ||
case $b: | ||
if ($delta != 0) { | ||
$h = 4 + ($r - $g) / $delta; | ||
} else { | ||
$s = 0; | ||
$h = 4 + $r - $g; | ||
} | ||
break; | ||
case $r: | ||
if ($delta != 0) { | ||
$h = ($g - $b) / $delta; | ||
} else { | ||
$h = $g - $b; | ||
} | ||
break; | ||
} | ||
|
||
$h*=60; | ||
if ($h<0) | ||
$h+=360; | ||
$h = round($h); | ||
$s = round($s*255); | ||
|
||
return compact('h','s','v'); | ||
} | ||
|
||
static function random($format='dec') { | ||
$color = rand(0, hexdec('ffffff')); | ||
if ($format=='hex') { | ||
return dechex($color); | ||
} else { | ||
return array( | ||
'r' => substr($color,0,2), | ||
'b' => substr($color,2,2), | ||
'r' => substr($color,-2)); | ||
} | ||
} | ||
} |