<?php
/**
* myColors
*
* Discover Your Colors!
*
* @author vylson silwr <hide@address.com>
* @version 1.0
* @copyright (C)2007 vylson silwr- GNU GPL
* @license http://www.gnu.org/licenses/gpl.html GNU General public License
* @package myColors
*/
// -----------------myColorsClass.php-------------------- //
require_once 'convert.class.php';
require_once 'info.class.php';
require_once 'toHex.class.php';
function __autoload($formula_name)
{
require_once '../lab/' . $formula_name . '_formula.php';
}
/*
* core class
*
* basic idea of this class is to
* step 1- receive color
* step 2-collect info about color
* step 3- loop through formula class till it reaches limit
* step 4- collect all colors and put into an array. manipulate.
* step 5- return
* */
class myColors
{
/**
*
* stores informations of an color to feed into formula class
*
* @var array
*/
private $_info = array();
/**
* default formula
*
* @var string
*/
private $_default_formula = 'spread';
/**
* default degree value
*
* @var int
*/
private $_degree = 5;
/**
* total number of colors to be produced
*
* @var int
*/
private $_limit = 8;
/**
* defining a static variable to count number of colors produced
*
* @var int
*/
private static $add = 0;
/**
* array containing hex chars => values
* located in separate file
* @file '/hexColors.php'
*
* @var array
*/
private $_colors = array();
/**
* array containing rgb hex values
*
* @var array
*/
private $_show_info;
/**
* array of hex code for rgb values
*
* @var array
*/
private $_hex = array();
/**
* variable to store formula class name to initiate
*
* @var string
*/
private $_formula;
/**
* instance of toHex class
*
* @var object
*/
private $_uc;
/**
* constructor
*
* set values
* @return void
*/
public function __construct($user_color, $info = FALSE)
{
$this -> _colors = colors(FALSE);
$this -> _uc = new toHex();
$this -> _user_color = $this -> _uc -> detect($user_color);
$this -> _hex = $this -> _divide_hex($this -> _user_color);
$this -> _rgb = $this -> _divide_rgb($this -> _hex);
$this -> _show_info = $info;
}
/**
* divide hex color to hex values of r g b
*
* @return array
*/
private function _divide_hex($hex_code)
{
$trim = trim($hex_code, '#');
return str_split($trim, 2);
}
/**
* convert hex values to r g b
*
* @return array
*/
private function _divide_rgb(array $hex_colors)
{
foreach ($hex_colors as $hex_color)
{
$rgb_colors[] = $this -> _colors[$hex_color];
}
return $rgb_colors;
}
/**
* loops over the color till the limit value
*
* @return array
*/
private function _color_factory($formula_class)
{
$source = $this -> _info = array(
'red_number' => $this -> _rgb[0],
'green_number' => $this -> _rgb[1],
'blue_number' => $this -> _rgb[2],
'hex' => $this -> _user_color,
'hex_red' => $this -> _hex[0],
'hex_green' => $this -> _hex[1],
'hex_blue' => $this -> _hex[2],
'degree' => $this -> _degree
);
$collect = array();
do
{
$this -> _formula = new $formula_class($source);
$collect[] = $col = $this -> _formula -> output();
$source = $this -> _feedback($col);
self::$add++;
}
while(self::$add<=$this->_limit);
return $collect;
}
/**
* setting custom formula, limit, degree to the class
*
* @return array
*/
public function discover($formula = '', $limit = '', $degree = '')
{
$this -> _limit = $limit - 1;
$this -> _degree = $degree;
$colors = array_unique($this -> _color_factory($formula));
if ($this -> _show_info)
{
foreach ($colors as $col)
{
$info = new info($col);
$collect_info[] = $info -> infos();
}
return array(
$colors,
$collect_info
);
}
return $colors;
}
/**
* returns information about colors
*
* @return array
*/
private function _feedback($hex)
{
$_hex = $this -> _divide_hex($hex);
$_rgb = $this -> _divide_rgb($_hex);
return array(
'red_number' => $_rgb[0],
'green_number' => $_rgb[1],
'blue_number' => $_rgb[2],
'hex' => $hex,
'hex_red' => $_hex[0],
'hex_green' => $_hex[1],
'hex_blue' => $_hex[2],
'degree' => $this -> _degree
);
}
/**
* information about the color
*
* @return string
*/
public function displayInfo()
{
$info = new info($this -> _user_color);
$collect_info[] = $info -> infos();
return $collect_info;
}
/**
* outputs some info about the input color
*
* @return string
*/
public function __toString()
{
$r = $this -> _divide_rgb($this -> _divide_hex($this -> _user_color));
$rr = round(($r[0] / 255) * 100);
$gg = round(($r[1] / 255) * 100);
$bb = round(($r[2] / 255) * 100);
$tot = ($rr + $gg + $bb) * 100;
return "red contains $rr% and green contains $gg% and blue contains $bb%";
}
}
//--------------class ends---------------//
?>