Login   Register  
PHP Classes
elePHPant
Icontem

File: WebSafeColors.inc.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Er. Rochak Chauhan  >  WebSafeColors.inc.php  >  WebSafeColors.inc.php  >  Download  
File: WebSafeColors.inc.php
Role: Class source
Content type: text/plain
Description: This class gives complete information about 'Web Safe Colors'
Class: WebSafeColors.inc.php
Lookup and retrieve colors of the Web Safe palette
Author: By
Last change: Optimized the class and added a new function to return the closest webSafeColor name. (as Suggested by Phil)
Date: 9 years ago
Size: 19,737 bytes
 

Contents

Class file image Download
<?php

/**
 * This class gives complete information about 'Web Safe Colors'
 *
 * It can  checks if a color if web safe or not.
 * It can also return any of RGB, Hexadecimal or name of the web safe color....
 * ... by entring any one of above mentioned three parameters.   Pretty neat eh ?  :)
 *
 *
 *
 * @author  Rochak Chauhan
 * 
 */

class WebSafeColors {
	// initialize the database of 140 web safe colors
	private $isExactMatch = -1;
	private $webSafeColorDatabase = Array(  
											0=>Array('colorName'=>'aliceblue', 'hexValue'=>'#F0F8FF'),
											1=>Array('colorName'=>'darkslategray', 'hexValue'=>'#2F4F4F'),
											2=>Array('colorName'=>'lightsalmon', 'hexValue'=>'#FFA07A'),
											3=>Array('colorName'=>'palevioletred', 'hexValue'=>'#DB7093'),
											4=>Array('colorName'=>'antiquewhite', 'hexValue'=>'#FAEBD7'),
											5=>Array('colorName'=>'darkturquoise', 'hexValue'=>'#00CED1'),
											6=>Array('colorName'=>'lightseagreen', 'hexValue'=>'#20B2AA'),
											7=>Array('colorName'=>'papayawhip', 'hexValue'=>'#FFEFD5'),
											8=>Array('colorName'=>'aqua', 'hexValue'=>'#00FFFF'),
											9=>Array('colorName'=>'darkviolet', 'hexValue'=>'#9400D3'),
											10=>Array('colorName'=>'lightskyblue', 'hexValue'=>'#87CEFA'),
											11=>Array('colorName'=>'peachpuff', 'hexValue'=>'#FFDAB9'),
											12=>Array('colorName'=>'aquamarine', 'hexValue'=>'#7FFFD4'),
											13=>Array('colorName'=>'deeppink', 'hexValue'=>'#FF1493'),
											14=>Array('colorName'=>'lightslategray', 'hexValue'=>'#778899'),
											15=>Array('colorName'=>'peru', 'hexValue'=>'#CD853F'),
											16=>Array('colorName'=>'azure', 'hexValue'=>'#F0FFFF'),
											17=>Array('colorName'=>'deepskyblue', 'hexValue'=>'#00BFFF'),
											18=>Array('colorName'=>'lightsteelblue', 'hexValue'=>'#B0C4DE'),
											19=>Array('colorName'=>'pink', 'hexValue'=>'#FFC0CB'),
											20=>Array('colorName'=>'beige', 'hexValue'=>'#F5F5DC'),
											21=>Array('colorName'=>'dimgray', 'hexValue'=>'#696969'),
											22=>Array('colorName'=>'lightyellow', 'hexValue'=>'#FFFFE0'),
											23=>Array('colorName'=>'plum', 'hexValue'=>'#DDA0DD'),
											24=>Array('colorName'=>'bisque', 'hexValue'=>'#FFE4C4'),
											25=>Array('colorName'=>'dodgerblue', 'hexValue'=>'#1E90FF'),
											26=>Array('colorName'=>'lime', 'hexValue'=>'#00FF00'),
											27=>Array('colorName'=>'powderblue', 'hexValue'=>'#B0E0E6'),
											28=>Array('colorName'=>'black', 'hexValue'=>'#000000'),
											29=>Array('colorName'=>'firebrick', 'hexValue'=>'#B22222'),
											30=>Array('colorName'=>'limegreen', 'hexValue'=>'#32CD32'),
											31=>Array('colorName'=>'purple', 'hexValue'=>'#800080'),
											32=>Array('colorName'=>'blanchedalmond', 'hexValue'=>'#FFEBCD'),
											33=>Array('colorName'=>'floralwhite', 'hexValue'=>'#FFFAF0'),
											34=>Array('colorName'=>'linen', 'hexValue'=>'#FAF0E6'),
											35=>Array('colorName'=>'red', 'hexValue'=>'#FF0000'),
											36=>Array('colorName'=>'blue', 'hexValue'=>'#0000FF'),
											37=>Array('colorName'=>'forestgreen', 'hexValue'=>'#228B22'),
											38=>Array('colorName'=>'magenta', 'hexValue'=>'#FF00FF'),
											39=>Array('colorName'=>'rosybrown', 'hexValue'=>'#BC8F8F'),
											40=>Array('colorName'=>'blueviolet', 'hexValue'=>'#8A2BE2'),
											41=>Array('colorName'=>'fuchsia', 'hexValue'=>'#FF00FF'),
											42=>Array('colorName'=>'maroon', 'hexValue'=>'#800000'),
											43=>Array('colorName'=>'royalblue', 'hexValue'=>'#4169E1'),
											44=>Array('colorName'=>'brown', 'hexValue'=>'#A52A2A'),
											45=>Array('colorName'=>'gainsboro', 'hexValue'=>'#DCDCDC'),
											46=>Array('colorName'=>'mediumaquamarine', 'hexValue'=>'#66CDAA'),
											47=>Array('colorName'=>'saddlebrown', 'hexValue'=>'#8B4513'),
											48=>Array('colorName'=>'burlywood', 'hexValue'=>'#DEB887'),
											49=>Array('colorName'=>'ghostwhite', 'hexValue'=>'#F8F8FF'),
											50=>Array('colorName'=>'mediumblue', 'hexValue'=>'#0000CD'),
											51=>Array('colorName'=>'salmon', 'hexValue'=>'#FA8072'),
											52=>Array('colorName'=>'cadetblue', 'hexValue'=>'#5F9EA0'),
											53=>Array('colorName'=>'gold', 'hexValue'=>'#FFD700'),
											54=>Array('colorName'=>'mediumorchid', 'hexValue'=>'#BA55D3'),
											55=>Array('colorName'=>'sandybrown', 'hexValue'=>'#F4A460'),
											56=>Array('colorName'=>'chartreuse', 'hexValue'=>'#7FFF00'),
											57=>Array('colorName'=>'goldenrod', 'hexValue'=>'#DAA520'),
											58=>Array('colorName'=>'mediumpurple', 'hexValue'=>'#9370DB'),
											59=>Array('colorName'=>'seagreen', 'hexValue'=>'#2E8B57'),
											60=>Array('colorName'=>'chocolate', 'hexValue'=>'#D2691E'),
											61=>Array('colorName'=>'gray', 'hexValue'=>'#808080'),
											62=>Array('colorName'=>'mediumseagreen', 'hexValue'=>'#3CB371'),
											63=>Array('colorName'=>'seashell', 'hexValue'=>'#FFF5EE'),
											64=>Array('colorName'=>'coral', 'hexValue'=>'#FF7F50'),
											65=>Array('colorName'=>'green', 'hexValue'=>'#008000'),
											66=>Array('colorName'=>'mediumslateblue', 'hexValue'=>'#7B68EE'),
											67=>Array('colorName'=>'sienna', 'hexValue'=>'#A0522D'),
											68=>Array('colorName'=>'cornflowerblue', 'hexValue'=>'#6495ED'),
											69=>Array('colorName'=>'greenyellow', 'hexValue'=>'#ADFF2F'),
											70=>Array('colorName'=>'mediumspringgreen', 'hexValue'=>'#00FA9A'),
											71=>Array('colorName'=>'silver', 'hexValue'=>'#C0C0C0'),
											72=>Array('colorName'=>'cornsilk', 'hexValue'=>'#FFF8DC'),
											73=>Array('colorName'=>'honeydew', 'hexValue'=>'#F0FFF0'),
											74=>Array('colorName'=>'mediumturquoise', 'hexValue'=>'#48D1CC'),
											75=>Array('colorName'=>'skyblue', 'hexValue'=>'#87CEEB'),
											76=>Array('colorName'=>'crimson', 'hexValue'=>'#DC143C'),
											77=>Array('colorName'=>'hotpink', 'hexValue'=>'#FF69B4'),
											78=>Array('colorName'=>'mediumvioletred', 'hexValue'=>'#C71585'),
											79=>Array('colorName'=>'slateblue', 'hexValue'=>'#6A5ACD'),
											80=>Array('colorName'=>'cyan', 'hexValue'=>'#00FFFF'),
											81=>Array('colorName'=>'indianred', 'hexValue'=>'#CD5C5C'),
											82=>Array('colorName'=>'midnightblue', 'hexValue'=>'#191970'),
											83=>Array('colorName'=>'slategray', 'hexValue'=>'#708090'),
											84=>Array('colorName'=>'darkblue', 'hexValue'=>'#00008B'),
											85=>Array('colorName'=>'indigo', 'hexValue'=>'#4B0082'),
											86=>Array('colorName'=>'mintcream', 'hexValue'=>'#F5FFFA'),
											87=>Array('colorName'=>'snow', 'hexValue'=>'#FFFAFA'),
											88=>Array('colorName'=>'darkcyan', 'hexValue'=>'#008B8B'),
											89=>Array('colorName'=>'ivory', 'hexValue'=>'#FFFFF0'),
											90=>Array('colorName'=>'mistyrose', 'hexValue'=>'#FFE4E1'),
											91=>Array('colorName'=>'springgreen', 'hexValue'=>'#00FF7F'),
											92=>Array('colorName'=>'darkgoldenrod', 'hexValue'=>'#B8860B'),
											93=>Array('colorName'=>'khaki', 'hexValue'=>'#F0E68C'),
											94=>Array('colorName'=>'moccasin', 'hexValue'=>'#FFE4B5'),
											95=>Array('colorName'=>'steelblue', 'hexValue'=>'#4682B4'),
											96=>Array('colorName'=>'darkgray', 'hexValue'=>'#A9A9A9'),
											97=>Array('colorName'=>'lavender', 'hexValue'=>'#E6E6FA'),
											98=>Array('colorName'=>'navajowhite', 'hexValue'=>'#FFDEAD'),
											99=>Array('colorName'=>'tan', 'hexValue'=>'#D2B48C'),
											100=>Array('colorName'=>'darkgreen', 'hexValue'=>'#006400'),
											101=>Array('colorName'=>'lavenderblush', 'hexValue'=>'#FFF0F5'),
											102=>Array('colorName'=>'navy', 'hexValue'=>'#000080'),
											103=>Array('colorName'=>'teal', 'hexValue'=>'#008080'),
											104=>Array('colorName'=>'darkkhaki', 'hexValue'=>'#BDB76B'),
											105=>Array('colorName'=>'lawngreen', 'hexValue'=>'#7CFC00'),
											106=>Array('colorName'=>'oldlace', 'hexValue'=>'#FDF5E6'),
											107=>Array('colorName'=>'thistle', 'hexValue'=>'#D8BFD8'),
											108=>Array('colorName'=>'darkmagenta', 'hexValue'=>'#8B008B'),
											109=>Array('colorName'=>'lemonchiffon', 'hexValue'=>'#FFFACD'),
											110=>Array('colorName'=>'olive', 'hexValue'=>'#808000'),
											111=>Array('colorName'=>'tomato', 'hexValue'=>'#FD6347'),
											112=>Array('colorName'=>'darkolivegreen', 'hexValue'=>'#556B2F'),
											113=>Array('colorName'=>'lightblue', 'hexValue'=>'#ADD8E6'),
											114=>Array('colorName'=>'olivedrab', 'hexValue'=>'#6B8E23'),
											115=>Array('colorName'=>'turquoise', 'hexValue'=>'#40E0D0'),
											116=>Array('colorName'=>'darkorange', 'hexValue'=>'#FF8C00'),
											117=>Array('colorName'=>'lightcoral', 'hexValue'=>'#F08080'),
											118=>Array('colorName'=>'orange', 'hexValue'=>'#FFA500'),
											119=>Array('colorName'=>'violet', 'hexValue'=>'#EE82EE'),
											120=>Array('colorName'=>'darkorchid', 'hexValue'=>'#9932CC'),
											121=>Array('colorName'=>'lightcyan', 'hexValue'=>'#E0FFFF'),
											122=>Array('colorName'=>'orangered', 'hexValue'=>'#FF4500'),
											123=>Array('colorName'=>'wheat', 'hexValue'=>'#F5DEB3'),
											124=>Array('colorName'=>'darkred', 'hexValue'=>'#8B0000'),
											125=>Array('colorName'=>'lightgoldenrodyellow', 'hexValue'=>'#FAFAD2'),
											126=>Array('colorName'=>'orchid', 'hexValue'=>'#DA70D6'),
											127=>Array('colorName'=>'white', 'hexValue'=>'#FFFFFF'),
											128=>Array('colorName'=>'darksalmon', 'hexValue'=>'#E9967A'),
											129=>Array('colorName'=>'lightgreen', 'hexValue'=>'#90EE90'),
											130=>Array('colorName'=>'palegoldenrod', 'hexValue'=>'#EEE8AA'),
											131=>Array('colorName'=>'whitesmoke', 'hexValue'=>'#F5F5F5'),
											132=>Array('colorName'=>'darkseagreen', 'hexValue'=>'#8FBC8F'),
											133=>Array('colorName'=>'lightgrey', 'hexValue'=>'#D3D3D3'),
											134=>Array('colorName'=>'palegreen', 'hexValue'=>'#98FB98'),
											135=>Array('colorName'=>'yellow', 'hexValue'=>'#FFFF00'),
											136=>Array('colorName'=>'darkslateblue', 'hexValue'=>'#483D8B'),
											137=>Array('colorName'=>'lightpink', 'hexValue'=>'#FFB6C1'),
											138=>Array('colorName'=>'paleturquoise', 'hexValue'=>'#AFEEEE'),
											139=>Array('colorName'=>'yellowgreen', 'hexValue'=>'#9ACD32'),
								   		  );
	/**
	 * Function to get RGB value form its hex code
	 *
	 *
	 * @param $hexValue  string  - format #ff0067
	 * @return string - rgb value
	 */
	public function getRgbValuefromHex($hexValue) {
		if($hexValue[0] == '#' && strlen(trim($hexValue))==7) {
			$r = $hexValue[1].$hexValue[2];
			$g = $hexValue[3].$hexValue[4];
			$b = $hexValue[5].$hexValue[6];
			
			return hexdec($r).",".hexdec($g).",".hexdec($b);
		}
		else {
			echo "<b><font face='verdana' color='red' size='2'> ERROR: Invalid color Information. </font></b>";
			return false;
		}
	}
		
	/**
	 * Function to display the complete information of web safe colors
	 *
	 * @return array
	 */
	public function displayColorDatabase() {
		echo "<table width='80%' align='center' cellpadding='2' cellspacing='0' border=0' ><tr  style='background-color:blanchedalmond'> ";
		echo "<td><b><font face='verdana' color='brown' size='2'> S No. </font></b></td> 
			  <td><b><font face='verdana' color='black' size='2'> 'Color Name' </font></b></td> 
			  <td><b><font face='verdana' color='green' size='2'> 'Hex Value' </font></b></td> 
			  <td><b><font face='verdana' color='red' size='2'> 'RGB Value' </font></b></td>
			  <td><b><font face='verdana' color='blue' size='2'> 'COLOR' </font></b></td></tr> <tr><td colspan='5'><BR></td></tr>";
		
		
		for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
			if($i%2 == 0) {
				$bgColor ='lavender';
			}
			else {
				$bgColor ='#FFFFFF';
			}
			
			echo "<tr style='background-color:$bgColor'>
				<td><b><font face='verdana' color='brown' size='2'>". ($i+1) ."</font></b></td> 
				<td><b><font face='verdana' color='black' size='2'>". $this->webSafeColorDatabase[$i]['colorName'] ."</font></b></td> 
				<td><b><font face='verdana' color='green' size='2'>". $this->webSafeColorDatabase[$i]['hexValue'] ."</font></b></td> 
				<td><b><font face='verdana' color='red' size='2'>". $this->getRgbValuefromHex($this->webSafeColorDatabase[$i]['hexValue']) ."</font></b></td>
				<td align='center' style='background-color:".$this->webSafeColorDatabase[$i]['colorName']."'></td> </tr>";

		}
		echo "</tr></table>";
	}
	
	
	/**
	 * Function to return the name of the web safe color
	 *
	 * @param $colorInfo string - could be the value of color in hexadecimal or in rgb
	 *							  usage getColorName('#FF0033')  
	 *	 									OR
	 *							  usage getColorName('255,23,56')  
	 * @return string - name of the web safe color (if present in the above $webSafeColorDatabase) , false otherwise
	 */
	public function getColorName($colorInfo) {
		// input check
		if( trim($colorInfo[0]) != '#' && substr_count($colorInfo, ",") != 2 ) {
			echo "<b><font face='verdana' color='red' size='2'> ERROR: Invalid color Information. </font></b>";
			return false;
		}
		else{
			if(trim($colorInfo[0]) == '#' && strlen(trim($colorInfo)) == 7) {
				// the input color is in hexadecimal
				for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
					if( strtolower(trim($this->webSafeColorDatabase[$i]['hexValue']))== strtolower(trim($colorInfo)) ) {
						return $this->webSafeColorDatabase[$i]['colorName'];								
					}
				}	
			}
			else {
				// the input color is in RGB
				for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
					if( strtolower(trim($this->getRgbValuefromHex($this->webSafeColorDatabase[$i]['hexValue']))) == trim($colorInfo) ) {
						return $this->webSafeColorDatabase[$i]['colorName'];								
					}
				}
			}			
		}
		return false;
	}
	
	
	/**
	 * Function to return the hexadecimal value of the web safe color
	 *
	 * @param $colorInfo string - could be the value of color in rgb or name of the color (no spaces between the name)
	 *							  usage getHexValue('aliceblue')  
	 *	 									OR
	 *							  usage getHexValue('255,23,56')  
	 * @return string - Hexadecimal value of the web safe color (if present in the above $webSafeColorDatabase) , false otherwise
	 */
	public function getHexValue($colorInfo) {
	 	
		if(substr_count($colorInfo, ",") == 2) {
			// the input color is in rgb
			for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
				if( strtolower(trim($this->getRgbValuefromHex($this->webSafeColorDatabase[$i]['hexValue'])))== trim($colorInfo) ) {
					return $this->webSafeColorDatabase[$i]['hexValue'];								
				}
			}	
		}
		else{
			// the input is name of the color
			for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
				if( strtolower(trim($this->webSafeColorDatabase[$i]['colorName']))== strtolower(trim($colorInfo)) ) {
					return $this->webSafeColorDatabase[$i]['hexValue'];								
				}
			}
		}		
		return false;
	}
	
	/**
	 * Function to return the RGB value of the web safe color
	 *
	 * @param $colorInfo string - could be the value of color in hexadecimal or name of the color (no spaces between the name)
	 *							  usage getRgbValue('#ff0000')  
	 *	 									OR
	 *							  usage getRgbValue('255,23,56')  
	 * @return string - RGB value of the web safe color (if present in the above $webSafeColorDatabase) , false otherwise
	 */
	public function getRgbValue($colorInfo) {
	 	
		if(trim($colorInfo[0]) == '#' && strlen(trim($colorInfo))==7) {
			// the input color is in rgb
			for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
				if( strtolower(trim($this->webSafeColorDatabase[$i]['hexValue']))== trim($colorInfo) ) {
					return $this->getRgbValuefromHex($this->webSafeColorDatabase[$i]['hexValue']);								
				}
			}	
		}
		else{
			// the input is name of the color
			for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
				if( strtolower(trim($this->webSafeColorDatabase[$i]['colorName']))== strtolower(trim($colorInfo)) ) {
					return $this->getRgbValuefromHex($this->webSafeColorDatabase[$i]['hexValue']);								
				}
			}
		}		
		return false;
	}

	
	/**
	 * Function to check if the input color is web safe or not
	 *
	 * @param $colorInfo string - could be the value of color in hexadecimal/RGB or name of the color (no spaces between the name)
	 *							  usage isWebSafeColor('#ff0000')  
	 *	 									OR
	 *							  usage isWebSafeColor('red')  
	 *	 									OR
	 *							  usage isWebSafeColor('255,23,56')  
	 *
	 * @return boolean - true if present in the above $webSafeColorDatabase , false otherwise
	 */
	public function isWebSafeColor($colorInfo) {
		if( trim($colorInfo[0]) == '#' && strlen(trim($colorInfo))==7) {
			//input color is in hexadecimal format	
			for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
				if( strtolower(trim($this->webSafeColorDatabase[$i]['hexValue']))== strtolower(trim($colorInfo)) ) {
					return true;
				}
			}
		}
		elseif(substr_count($colorInfo, ",") == 2) {
			//input color is in rgb format
			for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
				if( strtolower(trim($this->getRgbValuefromHex($this->webSafeColorDatabase[$i]['hexValue'])))== trim($colorInfo) ) {
					return true;
				}
			}
		}
		else {
			//input is the name of the color
			for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
				if( strtolower(trim($this->webSafeColorDatabase[$i]['colorName']))== strtolower(trim($colorInfo)) ) {
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * Fucntion to return closet color to main function
	 *
	 * @access private
	 *
	 * @param $colorInfo - string
	 *
	 * @return string
	 */
	private function returnClosestColor($colorInfo) {
		if( strlen(trim($colorInfo)) =='') {
			echo "<b><font face='verdana' color='red' size='2'> ERROR: Invalid color Information. </font></b>";
			return false;
		}

		// no shortest distance found, yet
		$temp_lev = 9999;
		
		// loop through words to find the closest
		for($i=0; $i<count($this->webSafeColorDatabase); $i++) {
			
			$color = $this->webSafeColorDatabase[$i]['colorName'];
			
			// calculate the distance between the input color,
			// and the current color
		   	$lev = levenshtein($colorInfo, $color);
		
		   // check for an exact match
		   if ($lev == 0) {
				   	
		       // closest color is this one (exact match)
		       $closest = $color;
		       $this->isExactMatch = 0;
		
		       // break out of the loop; we've found an exact match
		       break;
		   }
		   else {
		   	
		   	if($lev<=$temp_lev) {
		   		$temp_lev = $lev;
		   		$closest = $color;
		   	}
		   }
		  
		}
		return $closest;		
	}	
	
	/**
	 * Function to return closest websafe color
	 *
	 * @param $colorInfo string- only the name of the color
	 *
	 * @return $string
	 */
	public function getClosestMatchingWebsafeColor($colorInfo) {
		if( (strlen(trim($colorInfo))=='') || ( trim($colorInfo[0]) == '#' && strlen(trim($colorInfo)) == 7) || (substr_count($colorInfo, ",") == 2) ) {
			echo "<b><font face='verdana' color='red' size='2'> ERROR: Invalid color Information. </font></b>";
			return false;
		}
		else {
			// color name
			return $this->returnClosestColor($colorInfo);
		}
		return false;
	}
} 
?>