En PHP, ¿cuál es la forma más directa de convertir un triplete RGB a valores HSV?RGB a HSV en PHP
Respuesta
<?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;
}
He arreglado algunos errores en este código, lo he probado contra los resultados conocidos y funciona bien. ¡Gracias! –
Estoy tratando de entender cómo funciona HSV. Antes de devolver los valores, ¿no tiene que multiplicar $ H por 360, y $ S y $ V por 100? –
@JackHumphries - Vea mi respuesta para una versión un poco más fácil de seguir, que también devuelve grados y porcentajes. – Unsigned
Aquí es un método simple y directo que devuelve valores como HSV grados y porcentajes, que es la que utiliza el selector de color de Photoshop.
Tenga en cuenta que los valores de retorno no son redondeados, puede hacerlo usted mismo si es necesario. Tenga en cuenta que H(360) == H(0)
, por lo H
valores de 359.5
y mayor debe ronda a 0
fuertemente documentada con fines didácticos.
/**
* 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);
}
?>
Awesome answer. Era justo lo que necesitaba para aprender a calcular el valor de la parte de HSV. Ty. – Deanie
¿Hay alguna forma de revertir este proceso y convertirlo nuevamente? – JacobTheDev
@JacobTheDev Por supuesto. [Esta pregunta] (https://stackoverflow.com/q/3018313/629493) tiene ejemplos. – Unsigned
- 1. color RGB a HSV en javascript?
- 2. Conversión de RGB a HSV usando PIL
- 3. ¿Cómo cambiar el color RGB a HSV?
- 4. RGB a HSB Algoritmo
- 5. PHP VHS a RGB fórmula comprensión
- 6. cmyk a rgb usando php
- 7. ¿Hay una API del sistema C#/.NET incorporada para HSV a RGB?
- 8. PHP generar RGB
- 9. RGB Int a RGB - Python
- 10. Detección de umbrales en el espacio de color HSV (desde RGB) usando Python/PIL
- 11. Algoritmo para generar colores graduados RGB en PHP
- 12. Convertir código de círculo HSV de Delphi a C#
- 13. Recrear color HSV utilizando modos de fusión
- 14. eficiente umbral rojo usando HSV en OpenCV
- 15. Cómo convertir Hex a RGB?
- 16. Conversión de RGB a monocromo
- 17. Convertir valores RGB a Entero
- 18. ¿Convertir color RGB a CMYK?
- 19. cómo convertir hexadecimal a RGB
- 20. rgb valores a 0 a 1 escala
- 21. ¿Por qué no funciona este código Javascript RGB a HSL?
- 22. Cómo convertir RGB a 1px base64 datos codificados uri en php
- 23. Convierta un valor rgb int negativo en bruto a un valor rgb de 3 números
- 24. Convertir RGB a ColorName Cadena Javascript
- 25. Transformación sin pérdida de RGB a Y'CbCr
- 26. ¿Cómo lidiar con RGB a YUV conversión
- 27. ¿Crear imagen a partir de datos RGB?
- 28. PHP - obtiene el nombre del color de RGB
- 29. convertir color html o rgb a system.drawing.brush
- 30. Convertir System.Drawing.Color a RGB y Hex Value
Se encontró una búsqueda rápida en Google http://delphi.about.com/od/adptips2006/qt/RgbToHsb.htm. No en PHP, pero todo es matemático, así que debería ser fácil. – mk12