How it works

Fewer colors, on purpose

Posterizing throws away tonal detail: every color channel snaps to a handful of levels, so smooth shading turns into flat bands, like a screen print or a cel-shaded cartoon. One SVG primitive does it; ink lines along the edges complete the toon look.

1. Quantize: feComponentTransfer type="discrete"

<feComponentTransfer> runs each channel through its own transfer function. With type="discrete" the function is a step function: n values in tableValues split the input range into n equal bins, and every input in bin k comes out as the k-th value. Listing n evenly spaced values from 0 to 1 gives n levels per channel with black and white both kept: 0 0.3333 0.6667 1 for 4 levels, up to 64 colors in total.

This is the same table the dither mode of the halftone page uses, but here it runs on R, G and B separately and keeps the hue, where halftone and duotone first reduce the image to luminance. That also means a channel can jump on its own: a gradient from orange to red crosses band borders in green only, which is where posterized images get their unexpected color steps.

2. Cleaner bands: blur and saturation first

  • Smoothing blurs before quantizing. Without it, every bit of texture or noise near a band border flips between two levels and the border frays. Blurred, the borders follow the large shapes, which is what makes it read as cel shading rather than as a low color depth. The blur also pulls transparent pixels into the border, so the transfer function forces alpha to 1 and a final <feComposite operator="in" in2="SourceGraphic"> cuts the result back to the source's shape.
  • Saturation is an <feColorMatrix type="saturate"> before the steps. Quantizing tends to grey out mid-tones; a little extra saturation keeps the bands punchy.
  • Color space decides where the bins sit. In sRGB they are even on the gamma-encoded values, which is close to even for the eye. In linearRGB they are even in light, so on a perceptual ramp the darkest band is the widest, as the second ramp in the control panel shows.

3. Ink lines: edges from the edge filter

The ink comes from the same primitives as the edges and sketch page: two Sobel kernels in <feConvolveMatrix> with a bias of 0.5, folded to a magnitude by a 1 0 1 table and added up. Here they run on R, G and B separately and the three magnitudes are summed, because cartoon outlines sit between colors, and a red ball in front of a blue wall can have almost the same luminance as the wall. They read the smoothed source, not the bands, so lines follow shapes and not the shading steps.

An <feColorMatrix> then moves the edge strength into alpha (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0), which turns it into the coverage of a flat <feFlood> in the ink color, merged over the bands with <feMerge>.

<svg width="0" height="0" aria-hidden="true">
  <filter id="posterize" x="0" y="0" width="100%" height="100%"
          color-interpolation-filters="sRGB">
    <feGaussianBlur stdDeviation="1" result="soft" />
    <feColorMatrix in="soft" type="saturate" values="1.15" />
    <feComponentTransfer result="flat">
      <feFuncR type="discrete" tableValues="0 0.3333 0.6667 1" />
      <feFuncG type="discrete" tableValues="0 0.3333 0.6667 1" />
      <feFuncB type="discrete" tableValues="0 0.3333 0.6667 1" />
      <feFuncA type="linear" slope="0" intercept="1" />
    </feComponentTransfer>
    <!-- ink: Sobel on R, G and B (alpha forced to 1) -->
    <feColorMatrix in="soft" type="matrix" result="opaque"
                   values="1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 0 1" />
    <feConvolveMatrix in="opaque" order="3" kernelMatrix="1 0 -1  2 0 -2  1 0 -1"
                      divisor="2.6667" bias="0.5" edgeMode="duplicate" preserveAlpha="true" />
    <feComponentTransfer result="g0">
      <feFuncR type="table" tableValues="1 0 1" />
      <feFuncG type="table" tableValues="1 0 1" />
      <feFuncB type="table" tableValues="1 0 1" />
    </feComponentTransfer>
    <feConvolveMatrix in="opaque" order="3" kernelMatrix="1 2 1  0 0 0  -1 -2 -1"
                      divisor="2.6667" bias="0.5" edgeMode="duplicate" preserveAlpha="true" />
    <feComponentTransfer result="g1">
      <feFuncR type="table" tableValues="1 0 1" />
      <feFuncG type="table" tableValues="1 0 1" />
      <feFuncB type="table" tableValues="1 0 1" />
    </feComponentTransfer>
    <feComposite in="g0" in2="g1" operator="arithmetic" k2="1" k3="1" />
    <feColorMatrix type="matrix"
                   values="0.5 0.5 0.5 0 0  0.5 0.5 0.5 0 0  0.5 0.5 0.5 0 0  0 0 0 1 0" />
    <feComponentTransfer>
      <feFuncR type="linear" slope="1.5385" intercept="-0.5385" />
      <feFuncG type="linear" slope="1.5385" intercept="-0.5385" />
      <feFuncB type="linear" slope="1.5385" intercept="-0.5385" />
    </feComponentTransfer>
    <feMorphology operator="dilate" radius="1" />
    <!-- edge strength → coverage of a flat ink layer -->
    <feColorMatrix type="matrix" result="coverage"
                   values="0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  1 0 0 0 0" />
    <feFlood flood-color="#15111f" />
    <feComposite in2="coverage" operator="in" result="ink" />
    <feMerge>
      <feMergeNode in="flat" />
      <feMergeNode in="ink" />
    </feMerge>
    <!-- back to the source's shape -->
    <feComposite in2="SourceGraphic" operator="in" />
  </filter>
</svg>
.poster {
  filter: url(#posterize);
}

Browser support and performance

SVG filters on HTML, including feComponentTransfer and feConvolveMatrix, have been Baseline widely available since July 2015 (web-features, svg-filters). Checked in Chromium and Firefox; Safari is untested.

  • Posterizing alone is one of the cheapest filters there is: a table lookup per channel. Smoothing adds a blur, and its cost grows with the radius.
  • The ink lines add two 3×3 convolutions on three channels, a few cheap per-pixel steps and an optional dilate. Fine for static images, which are filtered once and cached; on animated, full-screen content it adds up.
  • Kernels and dilation work on the browser's offscreen pixels, so on a high-DPI screen the lines are thinner in CSS pixels than at 1×.
  • Text inside the filtered element is posterized and outlined too. Put the filter on images and illustrations, not on content that has to stay readable.

Back to the demo ↑