之前寫了一篇 CSS 的濾鏡 CSS 的 filter ,裡頭有一些設定,例如飽合度(saturate) 和色相旋轉(hue-rotate) 等,在 SVG 的濾鏡中也可以做到,而且還有一個更強大的 Matrix 轉換
首先先說明一下 feColorMatrix 的參數
type matrix , saturate , hueRotate , luminanceToAlpha
values 設定的值
matrix
先從最複雜的 matrix 說明
matrix 的定義如下,從
先來一個簡單的範例, 這個範例中把 R,G,B 的值都乘以2,可以增加亮度
<svg width="300" height="400">
<defs>
<filter id="a121721_f1">
<feColorMatrix in="SourceGraphic" type="matrix" values="2 0 0 0 0
0 2 0 0 0
0 0 2 0 0
0 0 0 1 0"></feColorMatrix>
</filter>
</defs>
<text x="80" y="20">原圖</text>
<image href="https://yiharng.github.io/bird.jpg" x="0" y="0" width="200" height="200"></image>
<text x="80" y="220">濾鏡</text>
<image href="https://yiharng.github.io/bird.jpg" x="0" y="200" width="200" height="200" filter="url(#a121721_f1)"></image>
</svg>
經由這個矩陣,把 r,g,b 都乘以2,即可增加亮度
saturate
要增加飽合度的話,只要 type 設為 saturate , 然後修改 values 即可, 如果想手動設定陣列來達到效果,公式如下:
<svg width="300" height="400">
<defs>
<filter id="a121721_f2">
<feColorMatrix in="SourceGraphic" type="saturate" values="2"></feColorMatrix>
</filter>
</defs>
<text x="80" y="20">原圖</text>
<image href="https://yiharng.github.io/bird.jpg" x="0" y="0" width="200" height="200"></image>
<text x="80" y="220">濾鏡</text>
<image href="https://yiharng.github.io/bird.jpg" x="0" y="200" width="200" height="200" filter="url(#a121721_f2)"></image>
</svg>
hueRotate
hueRotate 公式
<svg width="300" height="400">
<defs>
<filter id="a121721_f3">
<feColorMatrix in="SourceGraphic" type="hueRotate" values="180"></feColorMatrix>
</filter>
</defs>
<text x="80" y="20">原圖</text>
<image href="https://yiharng.github.io/bird.jpg" x="0" y="0" width="200" height="200"></image>
<text x="80" y="220">濾鏡</text>
<image href="https://yiharng.github.io/bird.jpg" x="0" y="200" width="200" height="200" filter="url(#a121721_f3)"></image>
</svg>
luminanceToAlpha
luminanceToAlpha 公式
<svg width="300" height="400">
<defs>
<filter id="a121721_f4">
<feColorMatrix in="SourceGraphic" type="luminanceToAlpha"></feColorMatrix>
</filter>
</defs>
<text x="80" y="20">原圖</text>
<image href="https://yiharng.github.io/bird.jpg" x="0" y="0" width="200" height="200"></image>
<text x="80" y="220">濾鏡</text>
<image href="https://yiharng.github.io/bird.jpg" x="0" y="200" width="200" height="200" filter="url(#a121721_f4)"></image>
</svg>
最後寫了一個自己設定矩陣參數的程式,做為這一篇的結束。
brightness
m
n
沒有留言:
張貼留言