Location: PHPKode > scripts > myColors > vylson-silwr-myColors-2550f71/core/toHex.class.php
<?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
	 */

	// -----------------toHex.class.php-------------------- //

	/**
	 *converts various input color format to valid hex code
	 *
	 *this class uses convert class as a parent and converts from name, rgb, hsl,
	 * hsv, 3 character hex to valid 6 character hex color
	 */

	class toHex Extends convert
	{

		/**
		 * default color
		 *
		 * @var string
		 */

		private $_default_color = 'ffffff';

		/**
		 * detects the given mode
		 *
		 * uses the parent converting methods and outputs as hex color. if the given
		 * color is invalid, it uses default color
		 *
		 * @return string
		 */
		public function detect($mode)
		{

			if (strstr(($mode), 'hsl'))
			{

				$colr = str_replace(array(
					'hsl(',
					')'
				), ' ', $mode);
				$cols = explode(',', $colr);
				$h = ($cols[0] / 360);
				$s = $cols[1] / 100;
				$l = $cols[2] / 100;
				$rgb_col = $this -> hsl_to_rgb($h, $s, $l);
				return $this -> rgb_to_hex($rgb_col[0], $rgb_col[1], $rgb_col[2]);
			}

			elseif (strstr(($mode), 'hsv'))
			{

				$colr = str_replace(array(
					'hsv(',
					')'
				), ' ', $mode);
				$cols = explode(',', $colr);
				$h = $cols[0] / 360;
				$s = $cols[1] / 100;
				$v = $cols[2] / 100;
				$rgb_col = $this -> hsv_to_rgb($h, $s, $v);
				return $this -> rgb_to_hex($rgb_col[0], $rgb_col[1], $rgb_col[2]);
			}

			elseif (strstr(($mode), 'rgb'))
			{

				$colr = str_replace(array(
					'rgb(',
					')'
				), ' ', $mode);
				$cols = explode(',', $colr);
				return $this -> rgb_to_hex($cols[0], $cols[1], $cols[2]);
			}

			elseif (strstr(($mode), 'name'))
			{

				$colr = trim(str_replace(array(
					'name(',
					')'
				), ' ', $mode));

				return $this -> name_to_hex($colr);
			}

			elseif (strstr(($mode), '#'))
			{

				$colr = trim($mode, '#');
				if (strlen($colr) == 3)
					return $this -> char_hex_to_hex($colr);
				elseif (strlen($colr) == 6)
					return $colr;
				else
					return $this -> _default_color;
			}
			else
				return $this -> _default_color;

		}

	}

	//--------------class ends---------------//
?>
Return current item: myColors