Use your own image path in place of ramp.png, or get the sample from the download.
dotnet add package RecursiveAI.OneImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneImage; Image ramp = Image.Load("ramp.png"); ramp.Quantize(4).Save("plain.png"); ramp.Quantize(4, Dither.FloydSteinberg) .Save("dithered.png"); Console.WriteLine("Wrote both PNG files");
Unzip the example, then dotnet run.
Wrote both PNG filesramp.png is a generated 64×32 RGB image. In every row, the red, green, and blue channels start at 0 and increase together by 4 per column, ending at 252.
The full program
using OneImage; Image ramp = Image.Load("ramp.png"); ramp.Quantize(colors: 4).Save("plain.png"); ramp.Quantize(4, Dither.FloydSteinberg) .Save("dithered.png"); Console.WriteLine("Wrote both PNG files"); foreach (string path in new[] { "ramp.png", "plain.png", "dithered.png" }) { Rgba32[] pixels = Image.Load(path).ToArray<Rgba32>(); int colors = pixels.Distinct().Count(); // Read the red channel down column 16; this sample is neutral gray. var column = Enumerable.Range(0, ramp.Height) .Select(y => pixels[y * ramp.Width + 16].R) .GroupBy(value => value) .OrderBy(group => group.Key) .Select(group => $"{group.Key} ({group.Count()} pixels)"); Console.WriteLine($"{path}: {colors} colors; column 16: {string.Join(", ", column)}"); }
dotnet run prints:
Wrote both PNG files ramp.png: 64 colors; column 16: 64 (32 pixels) plain.png: 4 colors; column 16: 94 (32 pixels) dithered.png: 4 colors; column 16: 30 (15 pixels), 94 (17 pixels)
Column 16 starts at shade 64. The plain version uses one shade for the whole column; the dithered version mixes two. Both obey the four-color cap. Quantize throws ArgumentOutOfRangeException for a color count outside 2–256 and ImageOperationException for CMYK or Lab input.
Variations
Use exact colors you choose. Quantize builds a palette from the image. To use a fixed list, such as black and white, pass it to Dither instead.
using OneImage; Rgba32[] palette = [ new(0, 0, 0, 255), new(255, 255, 255, 255) ]; Image.Load("ramp.png") .Dither(palette, Dither.FloydSteinberg) .Save("black-white.png");
Related recipes
The short answer
Call Quantize(colors: 4) to choose up to four colors for the image and replace the original pixels with those colors. Add dither: Dither.FloydSteinberg to spread the rounding error among nearby pixels. The result still uses at most four colors, but it can look less banded on a gradient.
What is color quantization?
Color quantization picks a small set of colors to represent an image, then assigns each pixel a color from that set. The count is a ceiling, not a promise to add colors. If you give the default quantizer a two-color image and ask for sixteen, it doesn’t invent fourteen more. OneImage chooses the palette separately for each frame. It reduces higher bit depths to eight bits per channel, and the default quantizer includes alpha in the color calculation when the image has it.
An image with only a few colors isn’t necessarily an indexed file. Quantize leaves the result as ordinary RGB or RGBA pixels. When the result has no more than 256 distinct 8-bit RGB or RGBA colors, OneImage’s default PNG encoder can write an exact indexed palette: each pixel stores a reference to a palette entry instead of storing all its channels. If you need an exact list of permitted colors across different images, use the Dither variation rather than asking Quantize to choose a palette for each image.
What is dithering in image processing?
Without dithering, equal input colors map to equal palette colors, so large parts of a gradient can turn into solid bands. Floyd–Steinberg dithering carries part of each pixel’s error forward to neighbors that haven’t been processed yet. Those neighbors can choose a different palette color even if they started at the same shade. At normal viewing size a mix of nearby dark and light pixels can suggest an in-between shade without adding a palette entry. Zoomed in, the pattern is visible. Compare the two PNGs from the download: dithering makes sense for the ramp, but you might prefer the plain result on artwork whose flat fills should remain flat.