Color to CSS filter() Converter

Generate a CSS filter string to recolor a black SVG/PNG to any color

Generate a CSS filter() property that recolors a black SVG or PNG to any target hex color. Uses an SPSA solver over invert, sepia, saturate, brightness, and hue-rotate, and reports the resulting color accuracy. Runs in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

Why does the element need to start black?

The solver assumes a pure black source. The first invert(0) step keeps it black, then sepia, saturate, brightness, and hue-rotate build up the target color from there. A non-black source will land on a different color.

The CSS filter property can tint a black icon to any color without editing the source asset, but the function chain has no simple inverse. This tool solves for a filter string numerically so you can recolor a black SVG or PNG to an exact target.

How it works

The filter chain is fixed in form and the solver tunes its parameters:

filter: invert(a%) sepia(b%) saturate(c%) hue-rotate(d deg) brightness(e%) contrast(f%);

Each function is modeled as a matrix or clamp operation on the linearized color. Starting from black, the tool runs an SPSA (Simultaneous Perturbation Stochastic Approximation) optimizer: it perturbs all parameters at once, measures the color distance to the target in both RGB and HSL space, and steps the parameters to reduce that distance over many iterations.

Practical example

To recolor a black SVG icon to a brand blue of #1a73e8, paste that hex into the tool and run the solver. A typical result might look like:

filter: invert(43%) sepia(57%) saturate(1200%) hue-rotate(201deg) brightness(97%) contrast(96%);

Apply that to the SVG element in CSS and the black icon renders in the target blue. The reported loss tells you how close it is — under 2 is usually visually indistinguishable.

Tips and notes

Source must be black. The element must begin as pure black (#000000) for the generated filter to be accurate. If your icon has a grey or coloured fill, prepend brightness(0) to force it black first:

filter: brightness(0) invert(43%) sepia(57%) saturate(1200%) hue-rotate(201deg) brightness(97%) contrast(96%);

Loss is your quality signal. Because the optimization is stochastic, re-run it if the reported loss is above 5 — a fresh random seed can find a better solution. Under 1 is effectively perfect for any screen use.

Why not just change the SVG fill? Inline SVG fill changes require touching the source file or using CSS currentColor. The filter approach lets you recolor externally-loaded SVGs or icon fonts where you cannot access the source markup — useful for third-party icons, img-embedded SVGs, or icon libraries that don’t support currentColor.

Limitations — complex gradients or images with multiple colours cannot be accurately recolored this way. The technique works best on flat, single-colour vector assets like icon sets and simple logos.