Module

Puregrain.Palette

Package
purescript-puregrain
Repository
roman-mkh/puregrain

Dithering to a palette: each pixel becomes the nearest palette color, with all three channels chosen together. nearestColor is the ready quantizer; ready-made palettes (black and white, web-safe, CGA, ANSI, Commodore 64, ZX Spectrum) are in Puregrain.Palette.Presets.

Distances are plain RGB distances, which aren't perceptual: pure green is nearer to black than to white.

#CompiledPalette Source

newtype CompiledPalette

A palette ready to search: its colors, never empty, and the distance to search them by. Build it once per image with compilePalette or compilePaletteFromArray; read its colors with paletteColors. Opaque, so its representation may change, to make the search faster, say.

#compilePalette Source

compilePalette :: (RGB -> RGB -> Number) -> NonEmptyArray RGB -> CompiledPalette

Makes a palette ready to search.

  • RGB -> RGB -> Number: the distance between a pixel and a color, usually distance2.
  • NonEmptyArray RGB: the colors.
palette = compilePalette distance2 websafe216

#compilePaletteFromArray Source

compilePaletteFromArray :: (RGB -> RGB -> Number) -> Array RGB -> Maybe CompiledPalette

As compilePalette, for colors in a plain Array, as they come from JSON or JavaScript, say.

  • RGB -> RGB -> Number: the distance, usually distance2.
  • Array RGB: the colors.

Nothing for an empty array.

#paletteColors Source

paletteColors :: CompiledPalette -> NonEmptyArray RGB

The palette's colors, in the order they were given.

#distance2 Source

distance2 :: RGB -> RGB -> Number

The squared distance between two colors in RGB, the usual metric for palettes: (r₁ − r₂)² + (g₁ − g₂)² + (b₁ − b₂)². Squared, since finding the nearest color only compares distances.

#nearestColorFast Source

nearestColorFast :: CompiledPalette -> RGB -> RGB

The palette's color nearest to a pixel, by the palette's distance; on a tie, the earlier color. For many pixels, compile the palette once; nearestColor does that for you.

  • CompiledPalette: the palette.
  • RGB: the pixel.

#nearestColor Source

nearestColor :: NonEmptyArray RGB -> Quantize RGB

A quantizer that turns each pixel into the nearest palette color, all three channels together, by distance2; on a tie, the earlier color.

  • NonEmptyArray RGB: the palette, e.g. from Puregrain.Palette.Presets.
ditherImage floydSteinberg (nearestColor websafe216) image

With a gamma (Puregrain.Dither.ditherImageWith), the nearest color is still chosen on stored values, as the eye sees them: each corrected pixel is brought back from light for the comparison, and the chosen color taken to light, while the error stays in light. A pixel that's exactly a byte's light comes back as that byte, so without diffusion the result is the one ditherImage gives.

Its tone, for the methods whose weights depend on it (Puregrain.Dither.ostromoukhov), is the mean of the pixel's three channels, the same in every channel: one set of weights for the whole pixel, as the palette decides on the whole pixel.