SVG filter - Partial hue rotation
by PhilQ
HTML
<h3>Original</h3>
<div class="block-wrapper">
<div class="block b1"></div>
<div class="block b2"></div>
<div class="block b3"></div>
<div class="block b4"></div>
<div class="block b5"></div>
</div>
<h3>With <code>hue-rotate: 45deg;</code></h3>
<div class="block-wrapper hue-45">
<div class="block b1"></div>
<div class="block b2"></div>
<div class="block b3"></div>
<div class="block b4"></div>
<div class="block b5"></div>
</div>
<h3>With <code>filter: url(#test-partial-hue-rotation);</code></h3>
<div class="block-wrapper marked-range">
<div class="block b1"></div>
<div class="block b2"></div>
<div class="block b3"></div>
<div class="block b4"></div>
<div class="block b5"></div>
</div>
<h3>With <code>filter: url(#partial-hue-rotation-srgb);</code></h3>
<div class="block-wrapper partial-hue-45">
<div class="block b1"></div>
<div class="block b2"></div>
<div class="block b3"></div>
<div class="block b4"></div>
<div class="block b5"></div>
</div>
<h3>With <code>filter: url(#partial-hue-rotation-linear);</code></h3>
<div class="block-wrapper partial-hue-45-2">
<div class="block b1"></div>
<div class="block b2"></div>
<div class="block b3"></div>
<div class="block b4"></div>
<div class="block b5"></div>
</div>
<h3>What I want: update hue only for red colors</h3>
<div class="block-wrapper">
<div class="block b1-goal"></div>
<div class="block b2-goal"></div>
<div class="block b3-goal"></div>
<div class="block b4-goal"></div>
<div class="block b5-goal"></div>
</div>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<filter id="partial-hue-rotation-srgb">
<!--
1) Mask away the colors that shouldn't be hue-rotated.
This is done based on the R-channel value only.
The R-channel value comes in at [0-1],
so multiply it by 255 to get the original value (as in rgb()).
Then subtract (lowest R-channel value of color range - 1)
to leave all color with a R-channel...
CSS
.block-wrapper {
width: 100%;
height: 50px;
display: flex;
margin-bottom: 10px;
}
.block {
width: 20%;
height: 100%;
}
.b1 {
background-color: rgb(29 85 34);
}
.b1-goal {
background-color: rgb(29 85 34);
}
.b2 {
background-color: rgb(32 53 79);
}
.b2-goal {
background-color: rgb(32 53 79);
}
.b3 {
background-color: rgb(175 43 52);
}
.b3-goal {
background-color: rgb(173 75 51);
}
.b4 {
background-color: rgb(172 94 50);
}
.b4-goal {
background-color: rgb(166 160 44);
}
.b5 {
background-color: rgb(96 230 33);
}
.b5-goal {
background-color: rgb(96 230 33);
}
.hue-45 {
filter: hue-rotate(45deg);
}
.marked-range {
background: #000;
}
.marked-range .block {
filter: url(#test-partial-hue-rotation);
}
.partial-hue-45 {
filter: url(#partial-hue-rotation-srgb);
}
.partial-hue-45-2 {
filter: url(#partial-hue-rotation-linear);
}