PHP comment marche la fonction...
Résolu
bibo5088
Messages postés
6
Date d'inscription
Statut
Membre
Dernière intervention
-
bibo5088 Messages postés 6 Date d'inscription Statut Membre Dernière intervention -
bibo5088 Messages postés 6 Date d'inscription Statut Membre Dernière intervention -
Bonjour, j'ai trouver une fonction qui nous retourne dans un array la portion de rouge, de bleu et de vert de la couleur hexadécimal indiqué.
Sauf que quand je regarde comment la fonction est faites, je n'y comprend rien ! (Enfin, si un petit peu).
voilà la fonction :
function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
$rgbArray = array();
if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
$colorVal = hexdec($hexStr);
$rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
$rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
$rgbArray['blue'] = 0xFF & $colorVal;
} elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
$rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
$rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
$rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
} else {
return false; //Invalid hex color code
}
return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
}
Pouvez-vous m'expliquer marche telle? merci.
Sauf que quand je regarde comment la fonction est faites, je n'y comprend rien ! (Enfin, si un petit peu).
voilà la fonction :
function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
$rgbArray = array();
if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
$colorVal = hexdec($hexStr);
$rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
$rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
$rgbArray['blue'] = 0xFF & $colorVal;
} elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
$rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
$rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
$rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
} else {
return false; //Invalid hex color code
}
return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
}
Pouvez-vous m'expliquer marche telle? merci.
A voir également:
- PHP comment marche la fonction...
- Fonction si et - Guide
- Easy php - Télécharger - Divers Web & Internet
- Fonction miroir - Guide
- Fonction moyenne excel - Guide
- Expert php pinterest - Télécharger - Langages
2 réponses
function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
$rgbArray = array();
if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
$colorVal = hexdec($hexStr);
$rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
$rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
$rgbArray['blue'] = 0xFF & $colorVal;
} elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
$rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
$rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
$rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
} else {
return false; //Invalid hex color code
}
return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
}
Pour comprendre son rôle, renseigne toi sur les expression régulière. En gros ca permet d'extraire des sous chaines d'une chaine
strlen renvoi la longueur de chaine. C'est simplement pour vérifier que la valaeur récupérée est bien une valeur rgb (r+g+b =3*2octets=6octets, soit 6 caractères)
le '&' est une masque , le >> un décalage. Je en vais pas faire de cours dessus, je te laisse rechercher "travail sur les bits" et "décalages binaires" sur google ;).
c'est une conversion, regarde ici : https://www.php.net/manual/fr/function.hexdec.php
tu peux aussi lire de cette manière :
(en gros, soit tu retourne le tableau de couleur, soit il est "transformé" (mis à plat) dans un string, chaque élément étant séparé par '$separator'
Ca devrait déjà t'aider pour comprendre le code je pense ;)
naga
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
$rgbArray = array();
if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
$colorVal = hexdec($hexStr);
$rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
$rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
$rgbArray['blue'] = 0xFF & $colorVal;
} elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
$rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
$rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
$rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
} else {
return false; //Invalid hex color code
}
return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
}
preg_replace("/[^0-9A-Fa-f]/", '', $hexStr);
Pour comprendre son rôle, renseigne toi sur les expression régulière. En gros ca permet d'extraire des sous chaines d'une chaine
strlen($hexStr)
strlen renvoi la longueur de chaine. C'est simplement pour vérifier que la valaeur récupérée est bien une valeur rgb (r+g+b =3*2octets=6octets, soit 6 caractères)
0xFF & ($colorVal >> 0x10);
le '&' est une masque , le >> un décalage. Je en vais pas faire de cours dessus, je te laisse rechercher "travail sur les bits" et "décalages binaires" sur google ;).
hexdec( )
c'est une conversion, regarde ici : https://www.php.net/manual/fr/function.hexdec.php
$returnAsString ? implode($seperator, $rgbArray) : $rgbArray
tu peux aussi lire de cette manière :
if( $returnAsString == true) return implode($seperator, $rgbArray); else return $rgbArray;
(en gros, soit tu retourne le tableau de couleur, soit il est "transformé" (mis à plat) dans un string, chaque élément étant séparé par '$separator'
Ca devrait déjà t'aider pour comprendre le code je pense ;)
naga