在PHP中,將RGB三元組轉換爲HSV值最直接的方法是什麼?PHP中的RGB到HSV
回答
<?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;
}
我整理了這段代碼中的一些錯誤,並根據已知的結果對它進行了測試,它工作正常。謝謝! – 2011-10-06 12:38:14
我想了解HSV的工作原理。在您返回值之前,您是否必須將$ H乘以360,並將$ S和$ V乘以100? – 2012-11-24 02:13:12
@JackHumphries - 查看我的答案稍微簡單一些,也會返回度數和百分比。 – Unsigned 2012-12-15 00:02:45
下面是一個簡單,直接的方法,它返回HSV值作爲度數和百分比,這是Photoshop的顏色選擇器使用的。
請注意,返回值不是四捨五入的,如果需要,您可以自己做。請記住,H(360) == H(0)
,所以H
值359.5
和更大值應舍入到0
爲學習目的大量記錄。
/**
* 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);
}
?>
真棒回答。正是我需要學習如何計算HSV的價值部分。 TY。 – Deanie 2017-09-28 02:00:43
有沒有辦法扭轉這個過程,將其轉換回來? – JacobTheDev 2018-02-13 14:38:55
@JacobTheDev當然。 [這個問題](https://stackoverflow.com/q/3018313/629493)有例子。 – Unsigned 2018-02-13 18:40:35
- 1. OpenCV - RGB到HSV並回到RGB?
- 2. RGB到HSV顏色的JavaScript?
- 3. 從RGB到HSV在OpenGL GLSL
- 4. android opencv rgb to hsv
- 5. 在OpenCV的轉換HSV到RGB
- 6. HSV到RGB停在黃色C#
- 7. 通過PIL和colorsys RGB到HSV
- 8. 將RGB值轉換爲HSV
- 9. ImageSharp將Rgb轉換爲Hsv
- 10. 將RGB轉換爲HSV
- 11. HSV(0 .. 255)RGB(0 .. 255)
- 12. RGB和HSV空間之間的轉換
- 13. k-means聚類在RGB或HSV規模?
- 14. 將RGB轉換爲HSB(又名HSV)
- 15. 如何將RGB轉換爲HSV?
- 16. HSV/RGB色彩空間轉換
- 17. RGB到HSV色彩轉換的Android使用opencv
- 18. 從RGB到HSV的OpenCV圖像轉換閾值痛苦
- 19. 從RGB到HSV的UIColor轉換,將亮度設置爲UIColor
- 20. 我們爲什麼要從RGB轉換到HSV
- 21. 如何在RGB空間中複製HSV值的添加/混合
- 22. HSV vs RGB色彩空間中像素的平均值
- 23. 如何將RGB轉換爲Cocoa Touch中的HSV?
- 24. 在轉換RGB 2 HSV中的塊僞像
- 25. 僅在RGB或HSV圖像中增加黃色飽和度(Matlab)
- 26. 在java中將RGB圖像轉換爲gray_scale HSV
- 27. 在OpenCV中將HSV轉換爲RGB失敗
- 28. 使用matlab /八度音程從rgb轉換爲hsv圖像回rgb
- 29. rgb值到php中的r,g,b值
- 30. 使用python將RGB值轉換爲等效的HSV值
快速谷歌搜索發現http://delphi.about.com/od/adptips2006/qt/RgbToHsb.htm。不在PHP中,但它都是數學的,所以它應該很容易。 – mk12 2009-11-20 22:56:03