<?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
*/
// -----------------convert.class.php-------------------- //
require_once '../functions/hexColors.func.php';
/**
* converts from on mode to another
*
*/
class convert
{
/**
* default color if color is not mentioned/ invalid hex
*
* @var string
*/
private $_default_color = 'ffffff';
/**
* stores array of decimal values of hex colors
*
* @var array
*/
private $_colors = array();
/**
* stores hex colors of decimal values
*
* @var array
*/
private $_get_hex_from_number = array();
/**
* stores array of color names and hex values
*
* @var array
*/
protected $_color_names = array();
/**
* constructor
*
* stores values
*
* @return object
*/
public function __construct()
{
$this -> _colors = colors(FALSE);
$this -> _get_hex_from_number = colors(TRUE);
$this -> _color_names = $this -> _load_colornames();
return $this;
}
/**
* converts from hsl to rgb value
*
* @return array
*/
public function hsl_to_rgb($hue = 360, $saturation = 45, $lightness = 50)
{
if ($saturation == 0)//HSL from 0 to 1
{
$red = $lightness * 255;
//RGB results from 0 to 255
$green = $lightness * 255;
$blue = $lightness * 255;
}
else
{
if ($lightness < 0.5)
$var_2 = $lightness * (1 + $saturation);
else
$var_2 = ($lightness + $saturation) - ($saturation * $lightness);
$var_1 = (2 * $lightness) - $var_2;
$r = floor(255 * $this -> hue_to_rgb($var_1, $var_2, $hue + (1 / 3)));
$g = floor(255 * $this -> hue_to_rgb($var_1, $var_2, $hue));
$b = floor(255 * $this -> hue_to_rgb($var_1, $var_2, $hue - (1 / 3)));
return array(
$r,
$g,
$b
);
}
}
/**
* converts from hue to rgb
*
* set values
* @return int
*/
private function hue_to_rgb($v1, $v2, $hue_value)
{
if ($hue_value < 0)
$hue_value += 1;
if ($hue_value > 1)
$hue_value -= 1;
if ((6 * $hue_value) < 1)
return ($v1 + ($v2 - $v1) * 6 * $hue_value);
if ((2 * $hue_value) < 1)
return ($v2);
if ((3 * $hue_value) < 2)
return ($v1 + ($v2 - $v1) * ((2 / 3) - $hue_value) * 6);
return ($v1);
}
/**
* converts from hsv to rgb
*
* @return array
*/
protected function hsv_to_rgb($hue = 360, $saturation = 45, $value = 60)
{
if ($saturation == 0)//$hue$saturation$value from 0 to 1
{
$red = round($value * 255);
$green = round($value * 255);
$blue = round($value * 255);
}
else
{
$hue_value = $hue * 6;
if ($hue_value == 6)
$hue_value = 0;
//$hue must be < 1
$var_i = floor($hue_value);
//Or ... var_i = floor( $hue_value )
$var_1 = $value * (1 - $saturation);
$var_2 = $value * (1 - $saturation * ($hue_value - $var_i));
$var_3 = $value * (1 - $saturation * (1 - ($hue_value - $var_i)));
if ($var_i == 0)
{
$red_value = $value;
$green_value = $var_3;
$blue_value = $var_1;
}
else
if ($var_i == 1)
{
$red_value = $var_2;
$green_value = $value;
$blue_value = $var_1;
}
else
if ($var_i == 2)
{
$red_value = $var_1;
$green_value = $value;
$blue_value = $var_3;
}
else
if ($var_i == 3)
{
$red_value = $var_1;
$green_value = $var_2;
$blue_value = $value;
}
else
if ($var_i == 4)
{
$red_value = $var_3;
$green_value = $var_1;
$blue_value = $value;
}
else
{
$red_value = $value;
$green_value = $var_1;
$blue_value = $var_2;
}
$red = round($red_value * 255);
$green = round($green_value * 255);
$blue = round($blue_value * 255);
}
return array(
$red,
$green,
$blue
);
}
/**
* converts from rgb to hex
*
* @return array
*/
public function rgb_to_hex($r = 224, $g = 78, $b = 90)
{
$r = $r + 0;
$g = $g + 0;
$b = $b + 0;
return $this -> _get_hex_from_number[$r] . $this -> _get_hex_from_number[$g] . $this -> _get_hex_from_number[$b];
}
/**
* converts from hex to rgb
*
* @return array
*/
public function hex_to_rgb($hex_code)
{
$hex_code = trim($hex_code, '#');
$hex_colors = str_split($hex_code, 2);
foreach ($hex_colors as $hex_color)
{
$rgb_colors[] = $this -> _colors[$hex_color] + 0;
}
return $rgb_colors;
}
/**
* converts from rgb to hsl
*
* @return array
*/
public function rgb_to_hsl(array $rgb)
{
$rgb = $rgb;
$red_value = ($rgb[0] / 255);
//RGB from 0 to 255
$green_value = ($rgb[1] / 255);
$blue_value = ($rgb[2] / 255);
$min_value = min($red_value, $green_value, $blue_value);
//Min. value of RGB
$max_value = max($red_value, $green_value, $blue_value);
//Max. value of RGB
$diff_value = $max_value - $min_value;
//Delta RGB value
$lightness = ($max_value + $min_value) / 2;
if ($diff_value == 0)//This is a gray, no chroma...
{
$hue = 0;
//HSL results from 0 to 1
$saturation = 0;
}
else//Chromatic data...
{
if ($lightness < 0.5)
$saturation = $diff_value / ($max_value + $min_value);
else
$saturation = $diff_value / (2 - $max_value - $min_value);
$red_point = ((($max_value - $red_value) / 6) + ($diff_value / 2)) / $diff_value;
$green_point = ((($max_value - $green_value) / 6) + ($diff_value / 2)) / $diff_value;
$blue_point = ((($max_value - $blue_value) / 6) + ($diff_value / 2)) / $diff_value;
if ($red_value == $max_value)
$hue = $blue_point - $green_point;
else
if ($green_value == $max_value)
$hue = (1 / 3) + $red_point - $blue_point;
else
if ($blue_value == $max_value)
$hue = (2 / 3) + $green_point - $red_point;
if ($hue < 0)
$hue += 1;
if ($hue > 1)
$hue -= 1;
}
return array(
'hue' => $hue,
'saturation' => $saturation,
'lightness' => $lightness
);
}
/**
* array of color names with hex code
*
* @return array
*/
private function _load_colornames()
{
return array(
'AliceBlue' => '#f0f8ff',
'AntiqueWhite' => '#faebd7',
'Aqua' => '#00ffff',
'Aquamarine' => '#7fffd4',
'Azure' => '#f0ffff',
'Beige' => '#f5f5dc',
'Bisque' => '#ffe4c4',
'Black' => '#000000',
'BlanchedAlmond' => '#ffebcd',
'Blue' => '#0000ff',
'BlueViolet' => '#8a2be2',
'Brown' => '#a52a2a',
'Burlywood' => '#deb887',
'CadetBlue' => '#5f9ea0',
'Chartreuse' => '#7fff00',
'Chocolate' => '#d2691e',
'Coral' => '#ff7f50',
'CornflowerBlue' => '#6495ed',
'Cornsilk' => '#fff8dc',
'Crimson' => '#dc143c',
'Cyan' => '#00ffff',
'DarkBlue' => '#00008b',
'DarkCyan' => '#008b8b',
'DarkGoldenrod' => '#b8860b',
'DarkGray' => '#a9a9a9',
'DarkGrey' => '#a9a9a9',
'DarkGreen' => '#006400',
'DarkKhaki' => '#bdb76b',
'DarkMagenta' => '#8b008b',
'DarkOliveGreen' => '#556b2f',
'DarkOrange' => '#ff8c00',
'DarkOrchid' => '#9932cc',
'DarkRed' => '#8b0000',
'DarkSalmon' => '#e9967a',
'DarkSeaGreen' => '#8fbc8f',
'DarkSlateBlue' => '#483d8b',
'DarkSlateGray' => '#2f4f4f',
'DarkSlateGrey' => '#2f4f4f',
'DarkTurquoise' => '#00ced1',
'DarkViolet' => '#9400d3',
'DeepPink' => '#ff1493',
'DeepSkyBlue' => '#00bfff',
'DimGray' => '#696969',
'DimGrey' => '#696969',
'DodgerBlue' => '#1e90ff',
'FireBrick' => '#b22222',
'FloralWhite' => '#fffaf0',
'ForestGreen' => '#228b22',
'Fuchsia' => '#ff00ff',
'Gainsboro' => '#dcdcdc',
'GhostWhite' => '#f8f8ff',
'Gold' => '#ffd700',
'Goldenrod' => '#daa520',
'Gray' => '#808080',
'Grey' => '#808080',
'Green' => '#008000',
'GreenYellow' => '#adff2f',
'Honeydew' => '#f0fff0',
'HotPink' => '#ff69b4',
'IndianRed' => '#cd5c5c',
'Indigo' => '#4b0082',
'Ivory' => '#fffff0',
'Khaki' => '#f0e68c',
'Lavender' => '#e6e6fa',
'LavenderBlush' => '#fff0f5',
'LawnGreen' => '#7cfc00',
'LemonChiffon' => '#fffacd',
'LightBlue' => '#add8e6',
'LightCoral' => '#f08080',
'LightCyan' => '#e0ffff',
'LightGoldenrodYellow' => '#fafad2',
'LightGray' => '#d3d3d3',
'LightGrey' => '#d3d3d3',
'LightGreen' => '#90ee90',
'LightPink' => '#ffb6c1',
'LightSalmon' => '#ffa07a',
'LightSeaGreen' => '#20b2aa',
'LightSkyBlue' => '#87cefa',
'LightSlateGray' => '#778899',
'LightSlateGrey' => '#778899',
'LightSteelBlue' => '#b0c4de',
'LightYellow' => '#ffffe0',
'Lime' => '#00ff00',
'Limegreen' => '#32cd32',
'Linen' => '#faf0e6',
'Magenta' => '#ff00ff',
'Maroon' => '#800000',
'MediumAquamarine' => '#66cdaa',
'MediumBlue' => '#0000cd',
'MediumOrchid' => '#ba55d3',
'MediumPurple' => '#9370d8',
'MediumSeaGreen' => '#3cb371',
'MediumSlateBlue' => '#7b68ee',
'MediumSpringGreen' => '#00fa9a',
'MediumTurquoise' => '#48d1cc',
'MediumVioletRed' => '#c71585',
'MidnightBlue' => '#191970',
'MintCream' => '#f5fffa',
'MistyRose' => '#ffe4e1',
'Moccasin' => '#ffe4b5',
'NavajoWhite' => '#ffdead',
'Navy' => '#000080',
'OldLace' => '#fdf5e6',
'Olive' => '#808000',
'OliveDrab' => '#6b8e23',
'Orange' => '#ffa500',
'OrangeRed' => '#ff4500',
'Orchid' => '#da70d6',
'PaleGoldenrod' => '#eee8aa',
'PaleGreen' => '#98fb98',
'PaleTurquoise' => '#afeeee',
'PaleVioletRed' => '#d87093',
'PapayaWhip' => '#ffefd5',
'PeachPuff' => '#ffdab9',
'Peru' => '#cd853f',
'Pink' => '#ffc0cb',
'Plum' => '#dda0dd',
'PowderBlue' => '#b0e0e6',
'Purple' => '#800080',
'Red' => '#ff0000',
'RosyBrown' => '#bc8f8f',
'RoyalBlue' => '#4169e1',
'SaddleBrown' => '#8b4513',
'Salmon' => '#fa8072',
'SandyBrown' => '#f4a460',
'SeaGreen' => '#2e8b57',
'Seashell' => '#fff5ee',
'Sienna' => '#a0522d',
'Silver' => '#c0c0c0',
'SkyBlue' => '#87ceeb',
'SlateBlue' => '#6a5acd',
'SlateGray' => '#708090',
'SlateGrey' => '#708090',
'Snow' => '#fffafa',
'SpringGreen' => '#00ff7f',
'SteelBlue' => '#4682b4',
'Tan' => '#d2b48c',
'Teal' => '#008080',
'Thistle' => '#d8bfd8',
'Tomato' => '#ff6347',
'Turquoise' => '#40e0d0',
'Violet' => '#ee82ee',
'Wheat' => '#f5deb3',
'White' => '#ffffff',
'WhiteSmoke' => '#f5f5f5',
'Yellow' => '#ffff00',
'YellowGreen' => '#9acd32'
);
}
/**
* finds hex code for name
*
* @return string
*/
protected function name_to_hex($color)
{
$color = strToLower($color);
$colorname = array_change_key_case($this -> _load_colornames());
if (array_key_exists($color, $colorname))
return trim($colorname[$color], '#');
else
return $this -> _default_color;
}
/**
* converts 3 character hex to 6 character hex
*
* @return string
*/
protected function char_hex_to_hex($color)
{
return $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
}
//--------------class ends---------------//
}
?>