SVG filter - Hue selection
by PhilQ
HTML
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" id="hue-filters">
<defs>
<filter id="tonegroup-select"
x="0%" y="0%"
width="100%" height="100%"
primitiveUnits="objectBoundingBox"
color-interpolation-filters="sRGB"
>
<!-- Test layers -->
<feColorMatrix type="matrix" in="SourceGraphic" result="test-r-gte-g"
values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 -255 0 0 1"
/>
<feColorMatrix type="matrix" in="SourceGraphic" result="test-r-gte-b"
values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 -255 0 1"
/>
<feColorMatrix type="matrix" in="SourceGraphic" result="test-g-gte-r"
values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -255 255 0 0 1"
/>
<feColorMatrix type="matrix" in="SourceGraphic" result="test-g-gte-b"
values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 -255 0 1"
/>
<feColorMatrix type="matrix" in="SourceGraphic" result="test-b-gte-r"
values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -255 0 255 0 1"
/>
<feColorMatrix type="matrix" in="SourceGraphic" result="test-b-gte-g"
values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -255 255 0 1"
/>
<!-- Logic masks for tone groups -->
<feComposite operator="in" in="test-r-gte-g" in2="test-r-gte-b" result="red-mask" />
<feComposite operator="in" in="test-g-gte-r" in2="test-g-gte-b" result="green-mask" />
<feComposite operator="in" in="test-b-gte-r" in2="test-b-gte-g" result="blue-mask" />
<feComposite operator="in" in="test-g-gte-r" in2="test-b-gte-r" result="cyan-mask" />
<feComposite operator="in" in="test-b-gte-g" in2="test-r-gte-g" result="magenta-mask" />
<feComposite operator="in" in="test-r-gte-b" in2="test-g-gte-b" result="yellow-mask" />
<!-- Uncomment the right tone group selection -->
<!-- Note: greyscale colors will always be selected -->
<feComposite operator="in" in="SourceGraphic" in2="red-mask" result="selection" />
<!-- <feComposite operator="in" in="SourceGraphic" in2="green-mask" result="selection" /> -->
<!--...
SCSS
/**
* Colorwheel generator mixin
* (c) 2021
*/
@mixin colorwheel($property, $smooth: true, $hue_step: 6, $lightness: 50%, $saturation: 100%, $opacity: 1) {
$wheel: ();
@for $hue from 0 through $hue_step {
$wheel: append($wheel, hsla($hue * (360 / $hue_step), $saturation, $lightness, $opacity));
}
$stops: ();
@if $smooth {
@for $i from 1 through ($hue_step + 1) {
$c: nth($wheel, $i);
$stops: append($stops, #{$c} #{ (($i - 1) / $hue_step) * 100% } );
}
} @else {
$p1: 0%;
$p2: 100%;
$step: 1 / ($hue_step * 2);
@for $i from 1 through ($hue_step + 1) {
$c: nth($wheel, $i);
@if $i == ($hue_step + 1) {
$p2: 100%;
} @else {
$p2: (2 * $i - 1) * $step * 100%;
}
$stops: append($stops, #{$c} #{$p1} #{$p2} );
$p1: $p2;
}
}
#{$property}: linear-gradient(to right, join($stops, (), comma) );
}
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,400;0,700;1,400;1,700&family=Roboto:ital,wght@0,400;0,700;1,400;1,700&display=swap');
* { margin: 0; padding: 0; box-sizing: border-box; }
#hue-filters {
width: 0;
height: 0;
visibility: hidden;
position: absolute;
z-index: -1;
}
.label, .before::before, .after::before {
position: absolute;
display: inline-block;
background: rgba(255,255,255, 0.8);
color: #333;
font-size: 16px;
font-weight: bold;
font-family: 'Roboto', sans-serif;
padding: 2px 4px;
border-radius: 3px;
z-index: 1;
margin-top: 5px;
margin-left: 5px;
}
.before::before {
content: 'Original';
}
.after::before {
content: 'With #hue-values filter';
}
.code {
font-family: 'Roboto Mono', monospace;
}
.wheel {
position: relative;
display: block;
width: 100%;
height: 100px;
/* background: linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00); */
@include colorwheel(background, true, 6, 50%, 100%, 0.8);
}
.very-light {
/* background: linear-gradient(to right, #fffafa, #fffffa, #fafffa, #faffff, #fafaff, #fffaff, #fffafa); */
/*
background:...