Last active
June 6, 2017 07:44
-
-
Save juanparati/48b9c68f10434f6b0fbacdedf7f38fa4 to your computer and use it in GitHub Desktop.
Color palette generator
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
/** | |
* Class Colors. | |
* Pallete Generator Helper. | |
*/ | |
class Colors implements \Iterator, \ArrayAccess | |
{ | |
const COL_MIN_AVG = 64; | |
const COL_MAX_AVG = 192; | |
const COL_STEP = 16; | |
const SEED = 'vvtdljnufvnluvievkdffectltuhvllrccrrtelfvvec'; | |
/** | |
* Sequence pointer. | |
* | |
* @var int | |
*/ | |
protected $sequence_pointer = 0; | |
/** | |
* Generate a color sequence in HEX format | |
* @param $num | |
* @return string color as hexadecimal string | |
*/ | |
public static function getHexColorSequence($num) | |
{ | |
// Append color seed | |
$num .= static::SEED; | |
$range = static::COL_MAX_AVG - static::COL_MIN_AVG; | |
$factor = $range / 256; | |
$offset = static::COL_MIN_AVG; | |
$base_hash = substr(md5($num), 0, 6); | |
$b_R = hexdec(substr($base_hash,0,2)); | |
$b_G = hexdec(substr($base_hash,2,2)); | |
$b_B = hexdec(substr($base_hash,4,2)); | |
$f_R = floor((floor($b_R * $factor) + $offset) / static::COL_STEP) * static::COL_STEP; | |
$f_G = floor((floor($b_G * $factor) + $offset) / static::COL_STEP) * static::COL_STEP; | |
$f_B = floor((floor($b_B * $factor) + $offset) / static::COL_STEP) * static::COL_STEP; | |
return sprintf('%02x%02x%02x', $f_R, $f_G, $f_B); | |
} | |
/** | |
* Return current value | |
* | |
* @return int | |
*/ | |
public function current() | |
{ | |
return static::getHexColorSequence($this->sequence_pointer); | |
} | |
/** | |
* Get next value | |
*/ | |
public function next() | |
{ | |
++$this->sequence_pointer; | |
} | |
/** | |
* Rewind | |
*/ | |
public function rewind() | |
{ | |
$this->sequence_pointer = 0; | |
} | |
/** | |
* Get current key. | |
* | |
* @return int | |
*/ | |
public function key() | |
{ | |
return $this->sequence_pointer; | |
} | |
/** | |
* Check if current index is valid. | |
* | |
* @return bool | |
*/ | |
public function valid() | |
{ | |
return true; | |
} | |
/** | |
* Check if offset exists. | |
* | |
* @param mixed $offset | |
* @return bool | |
*/ | |
public function offsetExists($offset) | |
{ | |
if (is_int($offset)) | |
return true; | |
} | |
/** | |
* Return value by index. | |
* | |
* @param mixed $index | |
* @return string | |
*/ | |
public function offsetGet($index) | |
{ | |
return static::getHexColorSequence($index); | |
} | |
/** | |
* Set a value, however Colors is read-only. | |
* | |
* @param mixed $offset | |
* @param mixed $value | |
*/ | |
public function offsetSet($offset, $value) | |
{ | |
// Nothing to do, Colors is read-only | |
} | |
/** | |
* Remove index, however Colors is read-only. | |
* | |
* @param mixed $index | |
*/ | |
public function offsetUnset($index) | |
{ | |
// Nothing to do, Colors is read-only | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment