我有一张图片,颜色为纯红色,#FF0000。我的目标是将其更改为 #1100FF。
就在几周前,我了解到我们在 CSS3 中具有着色/过滤功能。它工作得很好,但图像的最终颜色不是#1100FF。我通过 Chrome 工具“Awesome Color Picker”检查颜色
那么我是怎么做到的就是您一定要问的问题。
function RGB_TO_HSV($R, $G, $B) // RGB values: 0-255, 0-255, 0-255
{ // HSV values: 0-360, 0-100, 0-100
$HSL = array();
// Convert the RGB byte-values to percentages
$R = ($R / 255);
$G = ($G / 255);
$B = ($B / 255);
$maxRGB = max($R, $G, $B);
$minRGB = min($R, $G, $B);
$chroma = $maxRGB - $minRGB;
$computedV = 100 * $maxRGB;
if ($chroma == 0)
{
$HSL['H']= "0deg";
$HSL['S']= "0%";
$HSL['V']= (3*$computedV)."%";
return $HSL;
}
$computedS = 100 * ($chroma / $maxRGB);
if ($R == $minRGB)
$h = 3 - (($G - $B) / $chroma);
elseif ($B == $minRGB)
$h = 1 - (($R - $G) / $chroma);
else // $G == $minRGB
$h = 5 - (($B - $R) / $chroma);
$computedH = 60 * $h;
$HSL['H'] = $computedH."deg";
$HSL['S'] = $computedS."%";
$HSL['V'] = $computedV."%";
return $HSL;
}
然后在 CSS 中,我这样做:
img {-webkit-filter: hue-rotate(<?=$HSL['H']?>) saturate(<?=$HSL['S']?>) brightness(<?=$HSL['V']?>);}
现在请告诉我怎样才能达到所需的颜色?我是否需要添加或减去原始颜色(即#FF0000)以获得所需的颜色或我使用的代码不正确或什么?
输出颜色是:#0F2DFF
编辑:没有限制图像的颜色是#FF0000。我可以更改它,只要我可以将图像准确地着色为任何颜色即可。
最佳答案
这似乎是一个 webkit 或 css 错误:https://code.google.com/p/chromium/issues/detail?id=237524 或者至少是 mathematical transformation其背后并不像直接转移色调那么简单。 在这个例子中可以很容易地观察到这一点:
<div style="width: 200px; height: 100px; color:#333; background-color: hsl(100,100%,50%)">Original</div>
<div style="width: 200px; height: 100px; color:#333; background-color: hsl(200,100%,50%)">Expected</div>
<div style="width: 200px; height: 100px; color:#333; background-color: hsl(100,100%,50%); -webkit-filter: hue-rotate(100deg);">Filtered</div>
所以我认为如果不对 webkit 算法进行一些疯狂的逆向工程,就不可能将一种颜色精确地转换为另一种颜色。
关于php - CSS3 Webkit-filter 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221037/