Class de couleur

Description

Suite à un commentaire interessant de Coucou747 sur la source "PHP5-CLASSE-CONVERSION-RGB-HEXA", j'ai voulu créer une Classe qui correspond à ses critères.

Il suffit juste d'instancier la Class "Color" avec (au choix) le nom d'une couleur, son code hexadécimal ou son code RGB et on obtient le reste des informations concernant la couleur.

La Class abstraite "ConvertColor" peut être également utilisée pour la conversion Hexadécimal en RGB (ou inversement). Je vous conseille d'ailleurs d'utilisé "is_Hex" ou "is_RGB" avant toute conversion à moins biensur que vous êtes sûr des données que vous utilisez.

Source / Exemple :


<?php

abstract class ConvertColor {
	// Constante de Class "__COLORSFILE__" : Contient le chemin du fichier Colors.
	const __COLORSFILE__ = 'Colors.xml';
	// Propriété privée et statique "$oXML" : Contient l'objet SimpleXML du fichier Colors.
	private static $oXml; 

	// Méthode protégée" __construct" : Lit le fichier Colors.
	protected function __construct() {
		if (!isset($oXML)) {
			if (!is_file(self::__COLORSFILE__)) throw new Exception('Le fichier "'.self::__COLORSFILE.'" est manquant.');
			clearstatcache();
			self::$oXml = simplexml_load_file(self::__COLORSFILE__);
			if (self::$oXml === FALSE) throw new Exception('Erreur dans le fichier "'.self::__COLORSFILE.'".');
		}
	}

	// Méthode protégée "AddColorHex" : Retourne l'addition de la chaîne Hex du paramètre "$sHex" et de la chaîne Hex du paramètre "$sHex2".
	protected function AddColorHex($sHex1, $sHex2) {
		$aFinal[0] = hexdec(substr($sHex1, 0, 2))+hexdec(substr($sHex2, 0, 2));
		if ($aFinal[0] > 255) $aFinal[0] = 255;
		$aFinal[1] = hexdec(substr($sHex1, 2, 2))+hexdec(substr($sHex2, 2, 2));
		if ($aFinal[1] > 255) $aFinal[1] = 255;
		$aFinal[2] = hexdec(substr($sHex1, 4, 2))+hexdec(substr($sHex2, 4, 2));
		if ($aFinal[2] > 255) $aFinal[2] = 255;
		return sprintf('%02X%02X%02X', $aFinal[0], $aFinal[1], $aFinal[2]);
	}

	// Méthode protégée "AddColorRGB" : Retourne l'addition de la chaîne RGB du paramètre "$sRGB1" et de la chaîne RGB du paramètre "$sRGB2".
	protected function AddColorRGB($sRGB1, $sRGB2) {
		$aTemp = array(explode(',', $sRGB1), explode(',', $sRGB2));
		$aFinal = array($aTemp[0][0]+$aTemp[1][0], $aTemp[0][1]+$aTemp[1][1], $aTemp[0][2]+$aTemp[1][2]);
		if ($aFinal[0] > 255) $aFinal[0] = 255;
		if ($aFinal[1] > 255) $aFinal[1] = 255;
		if ($aFinal[2] > 255) $aFinal[2] = 255;
		return $aFinal[0].','.$aFinal[1].','.$aFinal[2];
	}

	// Méthode protégée "Hex3toHex6" : Retourne une chaîne hexadécimale de 6 nombres suivant la chaîne hexadécimale du paramètre "$sHex".
	protected function Hex3toHex6($sHex) {
		if (strlen($sHex) === 3) {
			$sHex = substr_replace($sHex, '0', 0, 0);
			$sHex = substr_replace($sHex, '0', 2, 0);
			$sHex = substr_replace($sHex, '0', 4, 0);
		}
		return $sHex;
	}

	// Méthode protégée "HextoHSL" : Retourne la chaîne HSL suivant la chaîne hexadécimale du paramètre "$sHex".
	protected function HextoHSL($sHex) {
		$aTemp = array(hexdec(substr($sHex, 0, 2)), hexdec(substr($sHex, 2, 2)), hexdec(substr($sHex, 4, 2)));
		$iMax = max($aTemp[0], $aTemp[1], $aTemp[2]);
		$iMin = min($aTemp[0], $aTemp[1], $aTemp[2]);
		$iDelta = $iMax-$iMin;
		if ($iDelta === 0) $iH = 160;
		elseif ($iMax === $aTemp[0]) {
			if ($aTemp[1] >= $aTemp[2]) $iH = round(60*(($aTemp[1]-$aTemp[2])/$iDelta));
			else $iH = round(60*(($aTemp[1]-$aTemp[2])/$iDelta)+360);
		}
		elseif ($iMax === $aTemp[1]) $iH = round(60*(($aTemp[2]-$aTemp[0])/$iDelta)+120);
		else $iH = round(60*(($aTemp[0]-$aTemp[1])/$iDelta)+240);
		$iL = round(240/255*$iMax);
		if ($iL === 0 OR $iMax === $iMin) $iS = 0;
		else $iS = round(240*($iDelta/$iMax));
		return $iH.','.$iS.','.$iL;
	}

	// Méthode protégée "HextoName" : Retourne le nom d'une couleur suivant la chaîne hexadécimale du paramètre "$sHex", sinon retourne NULL si le nom de la couleur n'est pas trouvé dans le fichier Colors.
	protected function HextoName($sHex) {
		$aXpath = self::$oXml->xpath('//color[@hex="'.strtoupper($sHex).'"]');
		if (end($aXpath) === FALSE) return NULL;
		return (string) end($aXpath)->attributes()->name;
	}

	// Méthode protégée "HextoRGB" : Retourne une chaîne RGB suivant la chaîne hexadécimale du paramètre "$sHex".
	protected function HextoRGB($sHex) {
		return hexdec(substr($sHex, 0, 2)).','.hexdec(substr($sHex, 2, 2)).','.hexdec(substr($sHex, 4, 2));
	}

	// Méthode protégée "HSL240toHSLother" : Retourne la chaîne HSL du paramètre "$sHSL" convertie pour avoir comme valeur maximale le nombre "$iMax".
	protected function HSL240toHSLother($sHSL, $iMax) {
		$aTemp = explode(',', $sHSL);
		return $aTemp[0].','.round($aTemp[1]/240*$iMax).','.round($aTemp[2]/240*$iMax);
	}

	// Méthode protégée "HSLChangeHue" : Retourne la chaîne HSL du paramètre "$sHSL" en ayant modifier la teite suivant le nombre du paramètre "$iHue".
	protected function HSLChangeHue($sHSL, $iHue) {
		$aTemp = explode(',', $sHSL);
		$aTemp[0] = ($aTemp[0]+$iHue)%360;
		if ($aTemp[0] < 0) return (360+$aTemp[0]).','.$aTemp[1].','.$aTemp[2];
		return $aTemp[0].','.$aTemp[1].','.$aTemp[2];
	}

	// Méthode protégée "HSLChangeLuminosity" : Retourne la chaîne HSL du paramètre "$sHSL" en ayant modifier la luminosité suivant le nombre du paramètre "$iLuminosity".
	protected function HSLChangeLuminosity($sHSL, $iLuminosity) {
		$aTemp = explode(',', $sHSL);
		$aTemp[2] += $iLuminosity;
		if ($aTemp[2] < 0) return $aTemp[0].','.$aTemp[1].',0';
		elseif ($aTemp[2] > 240) return $aTemp[0].','.$aTemp[1].',240';
		else return $aTemp[0].','.$aTemp[1].','.$aTemp[2];
	}

	// Méthode protégée "HSLChangeSaturation" : Retourne la chaîne HSL du paramètre "$sHSL" en ayant modifier la saturation suivant le nombre du paramètre "$iSaturation".
	protected function HSLChangeSaturation($sHSL, $iSaturation) {
		$aTemp = explode(',', $sHSL);
		$aTemp[1] += $iSaturation;
		if ($aTemp[1] < 0) return $aTemp[0].',0,'.$aTemp[2];
		elseif ($aTemp[1] > 240) return $aTemp[0].',240,'.$aTemp[2];
		else return $aTemp[0].','.$aTemp[1].','.$aTemp[2];
	}

	// Méthode protégée "HSLtoHex" : Retourne une chaîne Hexadécimale suivant la chaîne HSL du paramètre "$sHSL".
	protected function HSLtoHex($sHSL) {
		$aTemp = explode(',', $sHSL);
		$iH0 = (6*$aTemp[0])/360;
		$iMax = 255*($aTemp[2]/240);
		$iDelta = $iMax*($aTemp[1]/240);
		$iMin = $iMax-$iDelta;
		switch ((int) $iH0) {
			case 0:
				return sprintf('%02X%02X%02X', round($iMax), round($iMin+$iH0*$iDelta), round($iMin));
				break;
			case 1:
				return sprintf('%02X%02X%02X', round($iMin+(2-$iH0)*$iDelta), round($iMax), round($iMin));
				break;
			case 2:
				return sprintf('%02X%02X%02X', round($iMin), round($iMax), round($iMin+($iH0-2)*$iDelta));
				break;
			case 3:
				return sprintf('%02X%02X%02X', round($iMin), round($iMin+(4-$iH0)*$iDelta), round($iMax));
				break;
			case 4:
				return sprintf('%02X%02X%02X', round($iMin+($iH0-4)*$iDelta), round($iMin), round($iMax));
				break;
			case 5:
				return sprintf('%02X%02X%02X', round($iMax), round($iMin), round($iMin+(6-$iH0)*$iDelta));
				break;
		}
	}

	// Méthode protégée "HSLtoName" : Retourne le nom d'une couleur suivant la chaîne HSL du paramètre "$sHSL", sinon retourne NULL si le nom de la couleur n'est pas trouvé dans le fichier Colors.
	protected function HSLtoName($sHSL) {
		$aXpath = self::$oXml->xpath('//color[@hsl="'.$sHSL.'"]');
		if (end($aXpath) === FALSE) return NULL;
		return (string) end($aXpath)->attributes()->name;
	}

	// Méthode protégée "HSLtoRGB" : Retourne une chaîne RGB suivant la chaîne HSL du paramètre "$sHSL".
	protected function HSLtoRGB($sHSL) {
		$aTemp = explode(',', $sHSL);
		$iH0 = (6*$aTemp[0])/360;
		$iMax = 255*($aTemp[2]/240);
		$iDelta = $iMax*($aTemp[1]/240);
		$iMin = $iMax-$iDelta;
		switch ((int) $iH0) {
			case 0:
				return round($iMax).','.round($iMin+$iH0*$iDelta).','.round($iMin);
				break;
			case 1:
				return round($iMin+(2-$iH0)*$iDelta).','.round($iMax).','.round($iMin);
				break;
			case 2:
				return round($iMin).','.round($iMax).','.round($iMin+($iH0-2)*$iDelta);
				break;
			case 3:
				return round($iMin).','.round($iMin+(4-$iH0)*$iDelta).','.round($iMax);
				break;
			case 4:
				return round($iMin+($iH0-4)*$iDelta).','.round($iMin).','.round($iMax);
				break;
			case 5:
				return round($iMax).','.round($iMin).','.round($iMin+(6-$iH0)*$iDelta);
				break;
		}
	}

	// Méthode protégé "InverseHex" : Retourne la chaîne hexadécimale inverse à la chaîne héxadécimal du paramètre "$sHex".
	protected function InverseHex($sHex) {
		return strtr($sHex, array('0' => 'F', '1' => 'E', '2' => 'D', '3' => 'C', '4' => 'B', '5' => 'A', '6' => '9', '7' => '8', '8' => '7', '9' => '6', 'A' => '5', 'B' => '4', 'C' => '3', 'D' => '2', 'E' => '1', 'F' => '0'));
	}

	// Méthode protégé "InverseHSL" : Retourne la chaîne HSL inverse à la chaîne HSL du paramètre "$sHSL".
	protected function InverseHSL($sHSL) {
		$aTemp = explode(',', $sHSL);
		$iMin = 255*($aTemp[2]/240);
		$iDelta = $iMin*($aTemp[1]/240);
		$iMax = $iMin-$iDelta;
		$iMin = 255-$iMin;
		$iMax = 255-$iMax;
		$aTemp[0] -= 180;
		if ($aTemp[0] < 0) $aTemp[0] = 360+$aTemp[0];
		$aTemp[2] = round((240/255)*$iMax);
		if ($aTemp[2] === 0 OR $iMax === $iMin) $aTemp[1] = 0;
		else $aTemp[1] = round(240*($iDelta/$iMax));
		return $aTemp[0].','.$aTemp[1].','.$aTemp[2];
	}

	// Méthode protégé "InverseName" : Retourne le nom de la couleur inverse au nom de la couleur du paramètre "$sName".
	protected function InverseName($sName) {
		$aXpath = self::$oXml->xpath('//color[@name="'.$sName.'"]');
		if (end($aXpath) === FALSE) return NULL;
		return (string) end($aXpath)->attributes()->inversename;
	}

	// Méthode protégé "InverseRGB" : Retourne la chaîne RGB inverse à la chaîne RGB du paramètre "$sRGB".
	protected function InverseRGB($sRGB) {
		$aTemp = explode(',', $sRGB);
		return (255-$aTemp[0]).','.(255-$aTemp[1]).','.(255-$aTemp[2]);
	}

	// Méthode protégée "is_Hex" : Retourne TRUE si le paramètre "$sHex" est une chaîne hexadécimale, sinon retourne FALSE.
	protected function is_Hex($sHex) {
		if (preg_match('`^([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$`', (string) $sHex) === 0) return FALSE;
		else return TRUE;
	}

	// Méthode protégée "is_HSL" : Retourne TRUE si le paramètre "$sHSL" est une chaîne HSL, sinon retourne FALSE.
	protected function is_HSL($sHSL) {
		$sHSL = str_replace(' ', '', (string) $sHSL);
		$aTemp = explode(',', $sHSL);
		if (count($aTemp) !== 3) return FALSE;
		if (is_numeric($aTemp[0]) AND $aTemp[0] >= 0 AND $aTemp[0] <= 360 AND is_numeric($aTemp[1]) AND $aTemp[1] >= 0 AND $aTemp[1] <= 240 AND is_numeric($aTemp[2]) AND $aTemp[0] >= 0 AND $aTemp[0] <= 240) return TRUE;
		else return FALSE;
	}

	// Méthode protégée "is_Name" : Retourne TRUE si le paramètre "$sName" est une chaîne faisant référence à un nom de couleur du fichier Colors, sinon retourne FALSE.
	protected function is_Name($sName) {
		$aXpath = self::$oXml->xpath('//color[@name="'.strtolower((string) $sName).'"]');
		if (end($aXpath) === FALSE) return FALSE;
		else return TRUE;
	}

	// Méthode protégée "is_RGB" : Retourne TRUE si le paramètre "$sRGB" est une chaîne RGB, sinon retourne FALSE.
	protected function is_RGB($sRGB) {
		$sRGB = str_replace(' ', '', (string) $sRGB);
		$aTemp = explode(',', $sRGB);
		if (count($aTemp) !== 3) return FALSE;
		if (is_numeric($aTemp[0]) AND $aTemp[0] >= 0 AND $aTemp[0] <= 255 AND is_numeric($aTemp[1]) AND $aTemp[1] >= 0 AND $aTemp[1] <= 255 AND is_numeric($aTemp[2]) AND $aTemp[0] >= 0 AND $aTemp[0] <= 255) return TRUE;
		else return FALSE;
	}

	// Méthode protégée "NametoHex" : Retourne une chaîne hexadécimale suivant la chaîne du nom de la couleur du paramètre "$sName".
	protected function NametoHex($sName) {
		$aXpath = self::$oXml->xpath('//color[@name="'.strtolower($sName).'"]');
		return (string) end($aXpath)->attributes()->hex;
	}

	// Méthode protégée "NametoHSL" : Retourne une chaîne HSL suivant la chaîne du nom de la couleur du paramètre "$sName".
	protected function NametoHSL($sName) {
		$aXpath = self::$oXml->xpath('//color[@name="'.strtolower($sName).'"]');
		return (string) end($aXpath)->attributes()->hsl;
	}

	// Méthode protégée "NametoRGB" : Retourne une chaîne RGB suivant la chaîne du nom de la couleur du paramètre "$sName".
	protected function NametoRGB($sName) {
		$aXpath = self::$oXml->xpath('//color[@name="'.strtolower($sName).'"]');
		return (string) end($aXpath)->attributes()->rgb;
	}

	// Méthode protégée "RemoveColorHex" : Retourne la soustraction de la chaîne Hex du paramètre "$sHex1" et de la chaîne Hex du paramètre "$sHex2".
	protected function RemoveColorHex($sHex1, $sHex2) {
		$aFinal[0] = hexdec(substr($sHex1, 0, 2))-hexdec(substr($sHex2, 0, 2));
		if ($aFinal[0] < 0) $aFinal[0] = 0;
		$aFinal[1] = hexdec(substr($sHex1, 2, 2))-hexdec(substr($sHex2, 2, 2));
		if ($aFinal[1] < 0) $aFinal[1] = 0;
		$aFinal[2] = hexdec(substr($sHex1, 4, 2))-hexdec(substr($sHex2, 4, 2));
		if ($aFinal[2] < 0) $aFinal[2] = 0;
		return sprintf('%02X%02X%02X', $aFinal[0], $aFinal[1], $aFinal[2]);
	}

	// Méthode protégée "RemoveColorRGB" : Retourne la soustraction de la chaîne RGB du paramètre "$sRGB1" et de la chaîne RGB du paramètre "$sRGB2".
	protected function RemoveColorRGB($sRGB1, $sRGB2) {
		$aTemp = array(explode(',', $sRGB1), explode(',', $sRGB2));
		$aFinal = array($aTemp[0][0]-$aTemp[1][0], $aTemp[0][1]-$aTemp[1][1], $aTemp[0][2]-$aTemp[1][2]);
		if ($aFinal[0] < 0) $aFinal[0] = 0;
		if ($aFinal[1] < 0) $aFinal[1] = 0;
		if ($aFinal[2] < 0) $aFinal[2] = 0;
		return $aFinal[0].','.$aFinal[1].','.$aFinal[2];
	}

	// Méthode protégée "RGB255toRGBother" : Retourne la chaîne RGB du paramètre "$sRGN" convertie pour avoir comme valeur maximale le nombre "$iMax".
	protected function RGB255toRGBother($sRGB, $iMax) {
		$aTemp = explode(',', $sRGB);
		return round($aTemp[0]/255*$iMax).','.round($aTemp[1]/255*$iMax).','.round($aTemp[2]/255*$iMax);
	}

	// Méthode protégée "RGBChangeBlue" : Retourne la chaîne RGB du paramètre "$sRGB" en ayant modifier le bleu suivant le nombre du paramètre "$iBlue".
	protected function RGBChangeBlue($sRGB, $iBlue) {
		$aTemp = explode(',', $sRGB);
		$aTemp[2] += $iBlue;
		if ($aTemp[2] < 0) return $aTemp[0].','.$aTemp[1].',0';
		elseif ($aTemp[2] > 255) return $aTemp[0].','.$aTemp[1].',255';
		else return $aTemp[0].','.$aTemp[1].','.$aTemp[2];
	}

	// Méthode protégée "RGBChangeGreen" : Retourne la chaîne RGB du paramètre "$sRGB" en ayant modifier le vert suivant le nombre du paramètre "$iGreen".
	protected function RGBChangeGreen($sRGB, $iGreen) {
		$aTemp = explode(',', $sRGB);
		$aTemp[1] += $iGreen;
		if ($aTemp[1] < 0) return $aTemp[0].',0,'.$aTemp[2];
		elseif ($aTemp[1] > 255) return $aTemp[0].',255,'.$aTemp[2];
		else return $aTemp[0].','.$aTemp[1].','.$aTemp[2];
	}

	// Méthode protégée "RGBChangeRed" : Retourne la chaîne RGB du paramètre "$sRGB" en ayant modifier le rouge suivant le nombre du paramètre "$iRed".
	protected function RGBChangeRed($sRGB, $iRed) {
		$aTemp = explode(',', $sRGB);
		$aTemp[0] += $iRed;
		if ($aTemp[0] < 0) return '0,'.$aTemp[1].','.$aTemp[2];
		elseif ($aTemp[0] > 255) return '255,'.$aTemp[1].','.$aTemp[2];
		else return $aTemp[0].','.$aTemp[1].','.$aTemp[2];
	}

	// Méthode protégée "RGBtoHex" : Retourne une chaîne hexadécimale suivant la chaîne RGB du paramètre "$sRGB".
	// Méthode corrigée (voir le commentaire de "jon at ovbb dot org" sur "http://www.php.net/manual/fr/function.dechex.php".
	protected function RGBtoHex($sRGB) {
		$aTemp = explode(',', $sRGB);
		return sprintf('%02X%02X%02X', $aTemp[0], $aTemp[1], $aTemp[2]);
	}

	// Méthode protégée "RGBtoHSL" : Retourne une chaîne HSL suivant la chaîne RGB du paramètre "$sRGB".
	protected function RGBtoHSL($sRGB) {
		$aTemp = explode(',', $sRGB);
		$iMax = max($aTemp[0], $aTemp[1], $aTemp[2]);
		$iMin = min($aTemp[0], $aTemp[1], $aTemp[2]);
		$iDelta = $iMax-$iMin;
		if ($iDelta === 0) $iH = 160;
		elseif ($iMax === $aTemp[0]) {
			if ($aTemp[1] >= $aTemp[2]) $iH = round(60*(($aTemp[1]-$aTemp[2])/$iDelta));
			else $iH = round(60*(($aTemp[1]-$aTemp[2])/$iDelta)+360);
		}
		elseif ($iMax === $aTemp[1]) $iH = round(60*(($aTemp[2]-$aTemp[0])/$iDelta)+120);
		else $iH = round(60*(($aTemp[0]-$aTemp[1])/$iDelta)+240);
		$iL = round(240/255*$iMax);
		if ($iL === 0 OR $iMax === $iMin) $iS = 0;
		else $iS = round(240*($iDelta/$iMax));
		return $iH.','.$iS.','.$iL;
	}

	// Méthode protégée "RGBtoName" : Retourne le nom d'une couleur suivant la chaîne RGB du paramètre "$sRGB", sinon retourne NULL si le nom de la couleur n'est pas trouvé dans le fichier Colors.
	protected function RGBtoName($sRGB) {
		$aXpath = self::$oXml->xpath('//color[@rgb="'.$sRGB.'"]');
		if (end($aXpath) === FALSE) return NULL;
		return (string) end($aXpath)->attributes()->name;
	}

}

class Color extends ConvertColor {
	private $aColor = array('Hex' => NULL, 'HSL' => NULL, 'Name' => NULL, 'RGB' => NULL);

	// Constructeur optimisé sur les conseils de Kankrelune.
	public function __construct($sColor) {
		parent::__construct();
		$sColor = str_replace(array(' ', '#'), '', (string) $sColor);
		if ($this->is_Hex($sColor) === TRUE) {
			$sColor = $this->Hex3toHex6(strtoupper($sColor));
			$this->aColor = array('Hex' => $sColor, 'HSL' => $this->HextoHSL($sColor), 'Name' => $this->HextoName($sColor), 'RGB' => $this->HextoRGB($sColor));
		}
		elseif ($this->is_Name($sColor) === TRUE) $this->aColor = array('Hex' => $this->NametoHex($sColor), 'HSL' => $this->NametoHSL($sColor), 'Name' => strtolower($sColor), 'RGB' => $this->NametoRGB($sColor));
		elseif ($this->is_RGB($sColor) === TRUE) $this->aColor = array('Hex' => $this->RGBtoHex($sColor), 'HSL' => $this->RGBtoHSL($sColor), 'Name' => $this->RGBtoName($sColor), 'RGB' => $sColor);
		else throw new Exception('Le paramètre "'.$sColor.'" n\'est pas valide.');
	}

	public function AddColor($oColor) {
		if ($oColor instanceof Color === FALSE) $oColor = new $this($oColor);
		$this->aColor['Hex'] = $this->AddColorHex($this->aColor['Hex'], $oColor->ColorHex());
		$this->aColor['HSL'] = $this->HextoHSL($this->aColor['Hex']);
		$this->aColor['Name'] = $this->HextoName($this->aColor['Hex']);
		$this->aColor['RGB'] = $this->AddColorRGB($this->aColor['RGB'], $oColor->ColorRGB());
	}

	public function ColorHex() {
		return $this->aColor['Hex'];
	}

	public function ColorHSL($iMax = 240) {
		if (is_numeric($iMax) AND $iMax !== 240) return $this->HSL240toHSLother($this->aColor['HSL'], (int) $iMax);
		else return $this->aColor['HSL'];
	}

	public function ColorInverse() {
		$this->aColor['Hex'] = $this->InverseHex($this->aColor['Hex']);
		$this->aColor['Name'] = $this->InverseName($this->aColor['Name']);
		$this->aColor['RGB'] = $this->InverseRGB($this->aColor['RGB']);
		$this->aColor['HSL'] = $this->InverseHSL($this->aColor['HSL']);
	}

	public function ColorLessBlue($iBlue = 10) {
		if (is_numeric($iBlue) AND $iBlue > 0) {
			$this->aColor['RGB'] = $this->RGBChangeBlue($this->aColor['RGB'], -round($iBlue));
			$this->aColor['Hex'] = $this->RGBtoHex($this->aColor['RGB']);
			$this->aColor['HSL'] = $this->RGBtoHSL($this->aColor['RGB']);
			$this->aColor['Name'] = $this->RGBtoName($this->aColor['RGB']);
		}
	}

	public function ColorLessGreen($iGreen = 10) {
		if (is_numeric($iGreen) AND $iGreen > 0) {
			$this->aColor['RGB'] = $this->RGBChangeGreen($this->aColor['RGB'], -round($iGreen));
			$this->aColor['Hex'] = $this->RGBtoHex($this->aColor['RGB']);
			$this->aColor['HSL'] = $this->RGBtoHSL($this->aColor['RGB']);
			$this->aColor['Name'] = $this->RGBtoName($this->aColor['RGB']);
		}
	}

	public function ColorLessHue($iHue = 10) {
		if (is_numeric($iHue) AND $iHue > 0) {
			$this->aColor['HSL'] = $this->HSLChangeHue($this->aColor['HSL'], -round($iHue));
			$this->aColor['Hex'] = $this->HSLtoHex($this->aColor['HSL']);
			$this->aColor['Name'] = $this->HSLtoName($this->aColor['HSL']);
			$this->aColor['RGB'] = $this->HSLtoRGB($this->aColor['HSL']);
		}
	}

	public function ColorLessLuminosity($iLuminosity = 10) {
		if (is_numeric($iLuminosity) AND $iLuminosity > 0) {
			$this->aColor['HSL'] = $this->HSLChangeLuminosity($this->aColor['HSL'], -round($iLuminosity));
			$this->aColor['Hex'] = $this->HSLtoHex($this->aColor['HSL']);
			$this->aColor['Name'] = $this->HSLtoName($this->aColor['HSL']);
			$this->aColor['RGB'] = $this->HSLtoRGB($this->aColor['HSL']);
		}
	}

	public function ColorLessSaturation($iSaturation = 10) {
		if (is_numeric($iSaturation) AND $iSaturation > 0) {
			$this->aColor['HSL'] = $this->HSLChangeSaturation($this->aColor['HSL'], -round($iSaturation));
			$this->aColor['Hex'] = $this->HSLtoHex($this->aColor['HSL']);
			$this->aColor['Name'] = $this->HSLtoName($this->aColor['HSL']);
			$this->aColor['RGB'] = $this->HSLtoRGB($this->aColor['HSL']);
		}
	}

	public function ColorLessRed($iRed = 10) {
		if (is_numeric($iRed) AND $iRed > 0) {
			$this->aColor['RGB'] = $this->RGBChangeRed($this->aColor['RGB'], -round($iRed));
			$this->aColor['Hex'] = $this->RGBtoHex($this->aColor['RGB']);
			$this->aColor['HSL'] = $this->RGBtoHSL($this->aColor['RGB']);
			$this->aColor['Name'] = $this->RGBtoName($this->aColor['RGB']);
		}
	}

	public function ColorMoreBlue($iBlue = 10) {
		if (is_numeric($iBlue) AND $iBlue > 0) {
			$this->aColor['RGB'] = $this->RGBChangeBlue($this->aColor['RGB'], round($iBlue));
			$this->aColor['Hex'] = $this->RGBtoHex($this->aColor['RGB']);
			$this->aColor['HSL'] = $this->RGBtoHSL($this->aColor['RGB']);
			$this->aColor['Name'] = $this->RGBtoName($this->aColor['RGB']);
		}
	}

	public function ColorMoreGreen($iGreen = 10) {
		if (is_numeric($iGreen) AND $iGreen > 0) {
			$this->aColor['RGB'] = $this->RGBChangeGreen($this->aColor['RGB'], round($iGreen));
			$this->aColor['Hex'] = $this->RGBtoHex($this->aColor['RGB']);
			$this->aColor['HSL'] = $this->RGBtoHSL($this->aColor['RGB']);
			$this->aColor['Name'] = $this->RGBtoName($this->aColor['RGB']);
		}
	}

	public function ColorMoreHue($iHue = 10) {
		if (is_numeric($iHue) AND $iHue > 0) {
			$this->aColor['HSL'] = $this->HSLChangeHue($this->aColor['HSL'], round($iHue));
			$this->aColor['Hex'] = $this->HSLtoHex($this->aColor['HSL']);
			$this->aColor['Name'] = $this->HSLtoName($this->aColor['HSL']);
			$this->aColor['RGB'] = $this->HSLtoRGB($this->aColor['HSL']);
		}
	}

	public function ColorMoreLuminosity($iLuminosity = 10) {
		if (is_numeric($iLuminosity) AND $iLuminosity > 0) {
			$this->aColor['HSL'] = $this->HSLChangeLuminosity($this->aColor['HSL'], round($iLuminosity));
			$this->aColor['Hex'] = $this->HSLtoHex($this->aColor['HSL']);
			$this->aColor['Name'] = $this->HSLtoName($this->aColor['HSL']);
			$this->aColor['RGB'] = $this->HSLtoRGB($this->aColor['HSL']);
		}
	}

	public function ColorMoreSaturation($iSaturation = 10) {
		if (is_numeric($iSaturation) AND $iSaturation > 0) {
			$this->aColor['HSL'] = $this->HSLChangeSaturation($this->aColor['HSL'], round($iSaturation));
			$this->aColor['Hex'] = $this->HSLtoHex($this->aColor['HSL']);
			$this->aColor['Name'] = $this->HSLtoName($this->aColor['HSL']);
			$this->aColor['RGB'] = $this->HSLtoRGB($this->aColor['HSL']);
		}
	}

	public function ColorMoreRed($iRed = 10) {
		if (is_numeric($iRed) AND $iRed > 0) {
			$this->aColor['RGB'] = $this->RGBChangeRed($this->aColor['RGB'], round($iRed));
			$this->aColor['Hex'] = $this->RGBtoHex($this->aColor['RGB']);
			$this->aColor['HSL'] = $this->RGBtoHSL($this->aColor['RGB']);
			$this->aColor['Name'] = $this->RGBtoName($this->aColor['RGB']);
		}
	}

	public function ColorName() {
		return $this->aColor['Name'];
	}

	public function ColorRGB($iMax = 255) {
		if (is_numeric($iMax) AND $iMax !== 255) return $this->RGB255toRGBother($this->aColor['RGB'], (int) $iMax);
		else return $this->aColor['RGB'];
	}

	public function RemoveColor($oColor) {
		if ($oColor instanceof Color === FALSE) $oColor = new $this($oColor);
		$this->aColor['Hex'] = $this->RemoveColorHex($this->aColor['Hex'], $oColor->ColorHex());
		$this->aColor['HSL'] = $this->HextoHSL($this->aColor['Hex']);
		$this->aColor['Name'] = $this->HextoName($this->aColor['Hex']);
		$this->aColor['RGB'] = $this->RemoveColorRGB($this->aColor['RGB'], $oColor->ColorRGB());
	}

}

try {
	$color1 = new Color('Red');
	echo '<span style="color:',$color1->ColorHex(),';">La couleur a pour nom "',$color1->ColorName(),'" a pour code Hexadécimal "',$color1->ColorHex(),'" et pour code RGB "',$color1->ColorRGB(),'" et a pour code HSL "',$color1->ColorHSL(),'".</span>',"<br />\r\n";
	$color1->ColorInverse();
	echo '<span style="color:',$color1->ColorHex(),';">La couleur inverse a pour nom "',$color1->ColorName(),'" a pour code Hexadécimal "',$color1->ColorHex(),'" et pour code RGB "',$color1->ColorRGB(),'" et a pour code HSL "',$color1->ColorHSL(),'".</span>',"<br />\r\n";
	$color1->AddColor('Red');
	echo '<span style="color:',$color1->ColorHex(),';">La couleur inverse additionner à la couleur rouge, a pour nom "',$color1->ColorName(),'" a pour code Hexadécimal "',$color1->ColorHex(),'" et pour code RGB "',$color1->ColorRGB(),'" et a pour code HSL "',$color1->ColorHSL(),'".</span>',"<br />\r\n";
	$color1->RemoveColor('Cyan');
	echo '<span style="color:',$color1->ColorHex(),';">La couleur moins la couleur cyan, a pour nom "',$color1->ColorName(),'" a pour code Hexadécimal "',$color1->ColorHex(),'" et pour code RGB "',$color1->ColorRGB(),'" et a pour code HSL "',$color1->ColorHSL(),'".</span>',"<br /><br />\r\n\r\n";
	
	$color2 = new Color('4682B4');
	echo '<span style="color:',$color2->ColorHex(),';">La couleur a pour nom "',$color2->ColorName(),'" a pour code Hexadécimal "',$color2->ColorHex(),'" a pour code RGB "',$color2->ColorRGB(),'" et a pour code HSL "',$color2->ColorHSL(),'".</span>',"<br />\r\n";
	$color2->ColorMoreBlue(60);
	echo '<span style="color:',$color2->ColorHex(),';">La couleur avec plus de bleu a pour code Hexadécimal "',$color2->ColorHex(),'" et pour code RGB "',$color2->ColorRGB(),'" et a pour code HSL "',$color2->ColorHSL(),'".</span>',"<br />\r\n";
	$color2->ColorLessGreen(60);
	echo '<span style="color:',$color2->ColorHex(),';">La couleur avec moins de vert a pour code Hexadécimal "',$color2->ColorHex(),'" et pour code RGB "',$color2->ColorRGB(),'" et a pour code HSL "',$color2->ColorHSL(),'".</span>',"<br />\r\n";
	$color2->ColorMoreRed(60);
	echo '<span style="color:',$color2->ColorHex(),';">La couleur avec plus de rouge a pour code Hexadécimal "',$color2->ColorHex(),'" et pour code RGB "',$color2->ColorRGB(),'" et a pour code HSL "',$color2->ColorHSL(),'".</span>',"<br />\r\n";
	$color2->ColorInverse();
	echo '<span style="color:',$color2->ColorHex(),';">La couleur inverse a pour code Hexadécimal "',$color2->ColorHex(),'" et pour code RGB "',$color2->ColorRGB(),'" et a pour code HSL "',$color2->ColorHSL(),'".</span>',"<br /><br />\r\n\r\n";
	
	$color3 = new Color('72,61,139');
	echo '<span style="color:',$color3->ColorHex(),';">La couleur a pour nom "',$color3->ColorName(),'" a pour code Hexadécimal "',$color3->ColorHex(),'" et pour code RGB "',$color3->ColorRGB(),'" et a pour code HSL "',$color3->ColorHSL(),'".</span>',"<br />\r\n";
	$color3->ColorMoreLuminosity(60);
	echo '<span style="color:',$color3->ColorHex(),';">La couleur avec plus de luminosité a pour code Hexadécimal "',$color3->ColorHex(),'" et pour code RGB "',$color3->ColorRGB(),'" et a pour code HSL "',$color3->ColorHSL(),'".</span>',"<br />\r\n";
	$color3->ColorLessHue(60);
	echo '<span style="color:',$color3->ColorHex(),';">La couleur avec moins de teinte a pour code Hexadécimal "',$color3->ColorHex(),'" et pour code RGB "',$color3->ColorRGB(),'" et a pour code HSL "',$color3->ColorHSL(),'".</span>',"<br />\r\n";
	$color3->ColorMoreSaturation(60);
	echo '<span style="color:',$color3->ColorHex(),';">La couleur avec plus de saturation a pour code Hexadécimal "',$color3->ColorHex(),'" et pour code RGB "',$color3->ColorRGB(),'" et a pour code HSL "',$color3->ColorHSL(),'".</span>',"<br />\r\n";
	$color3->ColorInverse();
	echo '<span style="color:',$color3->ColorHex(),';">La couleur inverse a pour code Hexadécimal "',$color3->ColorHex(),'" et pour code RGB "',$color3->ColorRGB(),'" et a pour code HSL "',$color3->ColorHSL(),'".</span>',"<br /><br />\r\n\r\n";
}
catch (Exception $e) {
	echo $e;
}

?>

Conclusion :


Pas de bug connu.
Bien entendu si vous trouvez un bug ou si vous trouvez une amélioration, n'hésitez pas à m'en faire part ^^

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.