Increase SVG Contrast With feComponentTransfer
Use feComponentTransfer to increase SVG contrast around mid-gray. Calculate the slope and intercept, set sRGB, and check opacity with five measured swatches.
On this page
- Calculate the intercept for the gray you want to keep
- Compare two copies of the same swatches
- Check the filter in your destination
- Start with editable source artwork
- FAQ
- What slope and intercept increase SVG contrast?
- How do I keep transparency when increasing SVG contrast?
- Why did my SVG contrast filter shift mid-gray?
- Sources
To increase SVG contrast with feComponentTransfer, set the same linear slope above 1 on red, green, and blue. Use an intercept of (1 - slope) / 2 to keep the midpoint at half-scale. Start with slope="1.5" and intercept="-0.25", set color-interpolation-filters="sRGB", and omit feFuncA to preserve input opacity. W3C Filter Effects specification
Calculate the intercept for the gray you want to keep
The Filter Effects specification defines the linear transfer as output = slope × input + intercept, with channel values on a scale from 0 to 1. A pivot is the input value that should come out unchanged. Substituting the pivot for both input and output gives intercept = pivot × (1 - slope).
With a pivot of 0.5 and a slope of 1.5, the intercept is −0.25. An input of 0.25 becomes 0.125, 0.5 stays at 0.5, and 0.75 becomes 0.875. A slope of 1 and intercept of 0 leave the channel alone. A slope between 0 and 1 draws values toward the pivot.
The calculation happens in the filter's color space. SVG filter primitives start in linearRGB, so the tutorial sets color-interpolation-filters="sRGB" to work with the channel values of ordinary hex swatches. The pivot is an sRGB channel value in this example. W3C color-interpolation-filters property
The renderer clamps each output channel to the valid range. At these settings, any input at or below 1/6 maps to 0 and any at or above 5/6 maps to 1. If two shades merge at either end, try a smaller slope and recalculate the intercept. On colored artwork, an individual channel can clip before the others, so also inspect colors you need to preserve. W3C filter primitive overview
Compare two copies of the same swatches
Save the sample as swatches.svg in a text editor. The first copy uses the original fills. The second applies the filter to a group containing the same artwork. For your own artwork, place the relevant elements in a group and put the filter reference on that group. W3C SVG grouping
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="460"
viewBox="0 0 600 460">
<defs>
<filter id="contrast" color-interpolation-filters="sRGB">
<feComponentTransfer in="SourceGraphic">
<feFuncR type="linear" slope="1.5" intercept="-0.25"/>
<feFuncG type="linear" slope="1.5" intercept="-0.25"/>
<feFuncB type="linear" slope="1.5" intercept="-0.25"/>
</feComponentTransfer>
</filter>
<g id="swatches">
<rect x="20" y="20" width="104" height="105" fill="#202020"/>
<rect x="134" y="20" width="104" height="105" fill="#505050"/>
<rect x="248" y="20" width="104" height="105" fill="#808080"/>
<rect x="362" y="20" width="104" height="105" fill="#b0b0b0"/>
<rect x="476" y="20" width="104" height="105" fill="#e0e0e0"/>
<rect x="248" y="151" width="104" height="60" fill="#808080"
fill-opacity="0.5"/>
</g>
</defs>
<use href="#swatches"/>
<g transform="translate(0 230)" filter="url(#contrast)">
<use href="#swatches"/>
</g>
</svg>We rendered the sample to a transparent PNG in resvg 0.47.0 and sampled each swatch at its center. The five opaque grays measured 32, 80, 128, 176, 224 before filtering and 0, 56, 128, 200, 255 after. The smaller, half-opacity swatch had an alpha value of 128 out of 255 in both renders. These are 8-bit measurements from this renderer. The opaque mid-gray stayed at 128. The translucent swatch came out at RGB 126 instead of 128, with the same alpha.

Check the filter in your destination
- Open the saved SVG in the renderer you use to deliver the artwork, then compare the two solid strips.
- Check for darker shadows, lighter highlights, and a middle gray close to its original appearance.
- View the partial-opacity swatch over both a light and a dark background. Repeat that check on any translucent edges in your own artwork.
If both strips still look identical, check that the group's filter="url(#contrast)" matches the filter ID and that the destination renders SVG filters. If the dark and light ends change but the middle gray shifts, confirm sRGB is set and the same intercept is on all three color channels. If your destination needs newly editable colors on the paths, edit their source paints. The SVG feColorMatrix guide shows how to distinguish a rendered effect from a fill edit.
Start with editable source artwork
PerfectVector can turn a flat raster logo or icon into editable SVG paths when the original vector is unavailable. Apply the filter to the SVG master if you have it. To cut out faint pixels by opacity, use an SVG alpha threshold instead.
Vectorize the source image, then open the SVG and inspect the smallest shape you need to keep before applying the contrast filter to a copy.
FAQ
What slope and intercept increase SVG contrast?
Use a slope above 1 on all three color channels. To keep 0.5 fixed, set the intercept to (1 − slope) / 2. For a slope of 1.5, the intercept is −0.25.
How do I keep transparency when increasing SVG contrast?
Omit feFuncA or set it to identity. That preserves input alpha. Apply the linear transfer to red, green, and blue, and test a partially transparent edge or swatch in your destination.
Why did my SVG contrast filter shift mid-gray?
Check the filter's working space and intercept. Half-scale in linearRGB is a different displayed gray than half-scale in sRGB. Set color-interpolation-filters to sRGB if the pivot comes from an sRGB swatch.
Sources
- W3C — Filter Effects Module Level 1 — Defines the linear transfer, identity for missing channels, output clamping, and filter color space.
- W3C — SVG 2 grouping — Defines the group that holds the artwork and carries the filter reference.
- Linebender — resvg — Documents the SVG renderer used for the swatch comparison.
More from the blog
SVG Grain Texture: Keep Noise Inside Your Artwork
Add SVG grain with feTurbulence, keep monochrome noise inside SourceAlpha, tune it at two sizes, and preserve a clean path master for other destinations.
SVG feComposite: Compare in, out and xor Artwork
Compare SVG filter inputs with in, out and xor, test operand order and partial alpha, and preserve source paths when a visual opening is not editable geometry.


