hue-rotate()
hue-rotate() CSS 函式用於旋轉元素及其內容的色相。其結果是 <filter-function>。
注意: hue-rotate() 被指定為對 RGB 顏色執行的矩陣運算。它實際上不會將顏色轉換為 HSL 模型,因為 HSL 是一種非線性操作。因此,它可能無法保持原始顏色的飽和度或亮度,尤其是對於飽和度較高的顏色。
試一試
filter: hue-rotate(0);
filter: hue-rotate(90deg);
filter: hue-rotate(-0.25turn);
filter: hue-rotate(3.142rad);
<section id="default-example">
<img
class="transition-all"
id="example-element"
src="/shared-assets/images/examples/firefox-logo.svg"
width="200" />
</section>
語法
hue-rotate(angle)
值
angle可選-
輸入樣本的色相相對變化,指定為
<angle>。值為0deg時,輸入保持不變。正色相旋轉會增加色相值,而負色相旋轉會減小色相值。插值的初始值為0。沒有最小值或最大值。對於hue-rotate(Ndeg),值超過360deg的效果計算為N模 360。預設值為0deg。
<angle> CSS 資料型別表示以度、梯度、弧度或圈數表示的角度值。以下是等效的:
hue-rotate(-180deg)
hue-rotate(540deg)
hue-rotate(200grad)
hue-rotate(3.14159rad)
hue-rotate(0.5turn)
正式語法
<hue-rotate()> =
hue-rotate( [ <angle> | <zero> ]? )
示例
使用 backdrop-filter 屬性
此示例透過 backdrop-filter CSS 屬性將 hue-rotate() 濾鏡應用於段落,從而將 <p> 後面的區域進行色相偏移。
.container {
background: url("/shared-assets/images/examples/listen_to_black_women.jpg")
no-repeat left / contain #011296;
}
p {
backdrop-filter: hue-rotate(240deg);
text-shadow: 2px 2px #011296;
}
使用 filter 屬性
此示例透過 filter CSS 屬性應用 hue-rotate() 濾鏡,將色相偏移應用於整個元素,包括內容、邊框和背景影像。
p {
filter: hue-rotate(-60deg);
text-shadow: 2px 2px blue;
background-color: magenta;
color: goldenrod;
border: 1em solid rebeccapurple;
box-shadow:
inset -5px -5px red,
5px 5px yellow;
}
使用 url() 和 SVG hue-rotate 濾鏡
SVG <filter> 元素用於定義自定義濾鏡效果,然後可以透過 id 進行引用。<filter> 的 <feColorMatrix> 原始型別 hueRotate 提供了相同的效果。鑑於以下內容:
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 220 220"
color-interpolation-filters="sRGB"
height="220"
width="220">
<filter id="hue-rotate">
<feColorMatrix type="hueRotate" values="90" />
</filter>
</svg>
這些值產生相同的結果:
filter: hue-rotate(90deg); /* 90deg rotation */
filter: url("#hue-rotate"); /* with embedded SVG */
filter: url("folder/fileName.svg#hue-rotate"); /* external svg filter definition */
此示例顯示了三張影像:應用了 hue-rotate() 濾鏡函式的影像、應用了等效 url() 濾鏡的影像,以及原始影像用於比較。
hue-rotate() 不保留飽和度或亮度
下圖比較了兩個從紅色開始的顏色漸變:第一個使用 hue-rotate() 生成,第二個使用實際的 HSL 顏色值。請注意 hue-rotate() 漸變在中間部分顯示出明顯的飽和度和亮度差異。
<div>
<p>Using <code>hue-rotate()</code></p>
<div id="hue-rotate"></div>
</div>
<div>
<p>Using <code>hsl()</code></p>
<div id="hsl"></div>
</div>
const hueRotate = document.getElementById("hue-rotate");
const hsl = document.getElementById("hsl");
for (let i = 0; i < 360; i++) {
const div1 = document.createElement("div");
div1.style.backgroundColor = `hsl(${i}, 100%, 50%)`;
hsl.appendChild(div1);
const div2 = document.createElement("div");
div2.style.backgroundColor = "red";
div2.style.filter = `hue-rotate(${i}deg)`;
hueRotate.appendChild(div2);
}
規範
| 規範 |
|---|
| 濾鏡效果模組第 1 級 # funcdef-filter-hue-rotate |
瀏覽器相容性
載入中…
另見
- CSS 濾鏡效果模組
- 在
filter和backdrop-filter屬性值中可用的其他<filter-function>函式包括: