En PHP, quel est le moyen le plus simple de convertir un triplet RGB en valeurs HSV?RGB à HSV en PHP
Répondre
<?php
function RGB_TO_HSV ($R, $G, $B) // RGB Values:Number 0-255
{ // HSV Results:Number 0-1
$HSL = array();
$var_R = ($R/255);
$var_G = ($G/255);
$var_B = ($B/255);
$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;
$V = $var_Max;
if ($del_Max == 0)
{
$H = 0;
$S = 0;
}
else
{
$S = $del_Max/$var_Max;
$del_R = ((($var_Max - $var_R)/6) + ($del_Max/2))/$del_Max;
$del_G = ((($var_Max - $var_G)/6) + ($del_Max/2))/$del_Max;
$del_B = ((($var_Max - $var_B)/6) + ($del_Max/2))/$del_Max;
if ($var_R == $var_Max) $H = $del_B - $del_G;
else if ($var_G == $var_Max) $H = (1/3) + $del_R - $del_B;
else if ($var_B == $var_Max) $H = (2/3) + $del_G - $del_R;
if ($H<0) $H++;
if ($H>1) $H--;
}
$HSL['H'] = $H;
$HSL['S'] = $S;
$HSL['V'] = $V;
return $HSL;
}
J'ai corrigé quelques erreurs dans ce code, je l'ai testé par rapport à des résultats connus et cela fonctionne très bien. Merci! –
J'essaie de comprendre le fonctionnement de HSV. Avant de retourner les valeurs, n'avez-vous pas à multiplier $ H par 360, et $ S et $ V par 100? –
@JackHumphries - Voir ma réponse pour une version légèrement plus facile à suivre, qui renvoie également des degrés et des pourcentages. – Unsigned
Voici une méthode simple et directe qui renvoie les valeurs HSV sous forme de degrés et de pourcentages, ce que le sélecteur de couleurs de Photoshop utilise.
Notez que les valeurs de retour ne sont pas arrondies, vous pouvez le faire vous-même si nécessaire. Gardez à l'esprit que H(360) == H(0)
, donc H
valeurs de 359.5
et plus doivent tour à 0
très documentée à des fins d'apprentissage.
/**
* Licensed under the terms of the BSD License.
* (Basically, this means you can do whatever you like with it,
* but if you just copy and paste my code into your app, you
* should give me a shout-out/credit :)
*/
<?php
function RGBtoHSV($R, $G, $B) // RGB values: 0-255, 0-255, 0-255
{ // HSV values: 0-360, 0-100, 0-100
// Convert the RGB byte-values to percentages
$R = ($R/255);
$G = ($G/255);
$B = ($B/255);
// Calculate a few basic values, the maximum value of R,G,B, the
// minimum value, and the difference of the two (chroma).
$maxRGB = max($R, $G, $B);
$minRGB = min($R, $G, $B);
$chroma = $maxRGB - $minRGB;
// Value (also called Brightness) is the easiest component to calculate,
// and is simply the highest value among the R,G,B components.
// We multiply by 100 to turn the decimal into a readable percent value.
$computedV = 100 * $maxRGB;
// Special case if hueless (equal parts RGB make black, white, or grays)
// Note that Hue is technically undefined when chroma is zero, as
// attempting to calculate it would cause division by zero (see
// below), so most applications simply substitute a Hue of zero.
// Saturation will always be zero in this case, see below for details.
if ($chroma == 0)
return array(0, 0, $computedV);
// Saturation is also simple to compute, and is simply the chroma
// over the Value (or Brightness)
// Again, multiplied by 100 to get a percentage.
$computedS = 100 * ($chroma/$maxRGB);
// Calculate Hue component
// Hue is calculated on the "chromacity plane", which is represented
// as a 2D hexagon, divided into six 60-degree sectors. We calculate
// the bisecting angle as a value 0 <= x < 6, that represents which
// portion of which sector the line falls on.
if ($R == $minRGB)
$h = 3 - (($G - $B)/$chroma);
elseif ($B == $minRGB)
$h = 1 - (($R - $G)/$chroma);
else // $G == $minRGB
$h = 5 - (($B - $R)/$chroma);
// After we have the sector position, we multiply it by the size of
// each sector's arc (60 degrees) to obtain the angle in degrees.
$computedH = 60 * $h;
return array($computedH, $computedS, $computedV);
}
?>
Super réponse. Était juste ce dont j'avais besoin pour apprendre comment calculer la partie de la valeur de HSV. Ty. – Deanie
Y at-il un moyen d'inverser ce processus, en le convertissant en retour? – JacobTheDev
@JacobTheDev Bien sûr. [Cette question] (https://stackoverflow.com/q/3018313/629493) a des exemples. – Unsigned
- 1. conversion d'image OpenCV de RGB à HSV
- 2. Couleur Nom RGB/Hex/HSL/HSV etc
- 3. convertir HSL ou HSV en RGB via jquery
- 4. Y a-t-il une API système C#/.NET intégrée pour HSV à RGB?
- 5. hex <-> RGB <-> HSV de conversion de l'espace couleur avec Python
- 6. L'objet bitmap transforme rgb (13,11,12) en rgb (211,211,211)?
- 7. RGB en VIBGYOR en python
- 8. Convertir la valeur RVB en HSV
- 9. convertir les valeurs RVB en valeurs HSV équivalentes en utilisant python
- 10. HBRUSH à la valeur RGB
- 11. Objective-C RGB à HSB
- 12. comment convertir hexadécimal en RGB
- 13. Comment convertir les RGB en BGR?
- 14. Convertir IGBImage RGB en 3 tableaux
- 15. FFmpeg: Encode Rgb Cadres (AvFrames) à H264
- 16. Python PIL: indice de couleur à RGB
- 17. Création d'un histogramme HSV à l'aide de l'image RVB
- 18. Fonction pour convertir YCbCr en RGB?
- 19. Comment convertir RVB en HSV dans Cocoa Touch?
- 20. Comment accéder à une image RGB
- 21. Les images rgb brutes à MPEG?
- 22. Pour convertir des données RGB 12 bits en données RGB 12 bits empaquetées
- 23. Comment puis-je convertir un entier RGB en un tuple RGB correspondant (R, G, B)?
- 24. Array RGB manipulation
- 25. Convertir la méthode RGB?
- 26. Extraire rgb de UIColor
- 27. Modélisation de l'espace couleur HSV dans MATLAB
- 28. YCbCr à RGB à partir de la table matricielle
- 29. Convertir l'image CYMK en RGB en utilisant BOOST :: GIL
- 30. Comment convertir hex en rgb en utilisant Java?
Une recherche rapide sur google a été trouvée http://delphi.about.com/od/adptips2006/qt/RgbToHsb.htm. Pas en PHP, mais tout mathématique donc ça devrait être facile. – mk12