{
 "id": "oklab-palette-quantization",
 "title": "OKLab palette quantization",
 "aliases": [],
 "domain": "npr",
 "subdomain": "colour reduction",
 "stage": "post",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity",
  "unreal",
  "godot",
  "cpu",
  "python"
 ],
 "year_introduced": 2020,
 "inputs": [
  "ldr-image"
 ],
 "outputs": [
  "ldr-image"
 ],
 "style": "stylized",
 "dynamic": "both",
 "summary": "Maps each pixel to the nearest colour in a fixed palette measured in OKLab, which tracks perceived colour difference far better than Euclidean RGB distance.",
 "how_it_works": "Convert the pixel from linear sRGB to OKLab through two small matrix multiplications and a cube root. Do the same for each palette entry (precomputed offline). Then find the palette colour with the smallest Euclidean distance in OKLab, which approximates perceived difference because OKLab is built to be perceptually uniform in lightness, chroma and hue. To dither, add a Bayer or blue-noise threshold to the L (or all) coordinates before the nearest-colour search, which dithers across palette entries. For palettes larger than around 32 entries, store them in a texture or a lookup cube; for small palettes a loop over uniforms suffices.",
 "when_to_use": "Choose it for limited-palette looks (Game Boy, PICO-8, custom swatches) where matching human perception of hue and brightness matters. Prefer plain `posterization` when you only need fewer levels per channel with no fixed palette, and `error-diffusion-dithering` for offline conversion with higher quality.",
 "cost": {
  "class": "cheap",
  "notes": "Loop over palette size per pixel; cheap for small palettes, medium for large ones without a LUT."
 },
 "failure_modes": [
  "Nearest-colour assignment has hard contours between palette regions without dithering",
  "OKLab distance still disagrees with perception for very saturated blues and out-of-gamut colours",
  "Large palettes cost per-pixel loops unless baked into a 3D LUT",
  "Temporal flicker where a pixel sits near a boundary between two palette colours"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://bottosson.github.io/posts/oklab",
   "type": "web",
   "title": "A perceptual color space for image processing",
   "authors": [
    "Björn Ottosson"
   ],
   "year": null,
   "venue": "bottosson.github.io",
   "url": "https://bottosson.github.io/posts/oklab",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261104030601/https://bottosson.github.io/posts/oklab/",
     "timestamp": "20261104030601"
    },
    "page_title": "A perceptual color space for image processing",
    "host": "bottosson.github.io",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://bottosson.github.io/posts/oklab → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://bottosson.github.io/posts/oklab → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://bottosson.github.io/posts/oklab → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "ottossonperceptual"
  }
 ],
 "presets": [
  {
   "id": "oklab-palette-quantization-specimen",
   "title": "Left: nearest palette colour by RGB distance. Right: nearest by OKLab distance (same 10-colour palette)",
   "runtime": "glsl-fragment",
   "params": {},
   "code": "float hash(vec2 c){uvec2 u=uvec2(ivec2(floor(c)));uint h=u.x*1597334673u^u.y*3812015801u;h^=h>>15;h*=2246822519u;h^=h>>13;h*=3266489917u;h^=h>>16;return float(h)*(1./4294967296.);}\nfloat lum(vec3 c){return dot(c,vec3(.2126,.7152,.0722));}\nvec3 gam(vec3 c){return pow(clamp(c,0.,1.),vec3(1./2.2));}\nvec3 img(vec2 p){\n  vec3 c=mix(vec3(.12,.18,.34),vec3(.95,.72,.45),clamp(p.y+.5,0.,1.));\n  vec2 q=p-vec2(-.03,.14); float rr=.16; float d=length(q)-rr;\n  vec3 n=normalize(vec3(q/rr,sqrt(max(1.-dot(q,q)/(rr*rr),0.))));\n  float l=clamp(dot(n,normalize(vec3(-.5,.6,.6))),0.,1.);\n  c=mix(c,vec3(.85,.26,.2)*(.18+.9*l),smoothstep(.002,-.002,d));\n  vec2 r=p-vec2(0.,-.2);\n  if(abs(r.x)<.2&&abs(r.y)<.09) c=mix(vec3(.1,.35,.3),vec3(.93,.92,.85),smoothstep(-.3,.3,sin(r.x*340.+r.y*90.)));\n  if(p.y<-.34&&p.y>-.42){ vec3 h=.5+.5*cos(6.2832*((p.x+.25)*2.+vec3(0.,.33,.67))); c=h; }\n  c=mix(c,vec3(.97,.85,.2),smoothstep(.003,-.003,abs(p.x-.17)-.025)*step(.0,p.y)*step(p.y,.4));\n  return c;\n}\n\nvec3 toOk(vec3 c){c=pow(c,vec3(2.2));\n  float l=pow(.4122214708*c.r+.5363325363*c.g+.0514459929*c.b,1./3.),m=pow(.2119034982*c.r+.6806995451*c.g+.1073969566*c.b,1./3.),s=pow(.0883024619*c.r+.2817188376*c.g+.6299787005*c.b,1./3.);\n  return vec3(.2104542553*l+.793617785*m-.0040720468*s,1.9779984951*l-2.428592205*m+.4505937099*s,.0259040371*l+.7827717662*m-.808675766*s);}\nconst vec3 PAL[10]=vec3[10](vec3(.07,.08,.18),vec3(.35,.2,.5),vec3(.15,.45,.5),vec3(.3,.65,.3),vec3(.95,.85,.3),vec3(.95,.55,.2),vec3(.8,.2,.2),vec3(.9,.55,.7),vec3(.55,.6,.7),vec3(.95,.95,.9));\nvoid mainImage(out vec4 fragColor,in vec2 fragCoord){\n  vec2 uv=(fragCoord-.5*iResolution.xy)/iResolution.y;bool R=uv.x>0.;vec2 q=vec2(uv.x+(R?-.25:.25),uv.y);\n  vec3 c=img(q),col=c,co=toOk(c);float best=1e9;\n  for(int i=0;i<10;i++){float d=R?dot(toOk(PAL[i])-co,toOk(PAL[i])-co):dot(PAL[i]-c,PAL[i]-c);if(d<best){best=d;col=PAL[i];}}\n  if(abs(uv.x)<.0015)col=vec3(1.);fragColor=vec4(clamp(col,0.,1.),1.);\n}",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "oklab-palette-quantization",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.478,
      "std": 0.2525,
      "unique_colors": 10
     },
     "image": "/specimens/oklab-palette-quantization-specimen.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "pixel-art-3d-rendering",
  "ordered-dithering-bayer",
  "posterization"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "ordered-dithering-bayer",
   "why": "Threshold offset applied before the palette search spreads error across nearest palette entries."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/npr/oklab-palette-quantization.json"
 },
 "solves": [
  "mapping to a fixed palette gives wrong perceived colours",
  "game boy or pico-8 look needs correct nearest colour",
  "rgb distance picks ugly hue matches",
  "limit image to custom swatches with good perceptual match"
 ],
 "authority": "Ottosson",
 "url": "https://texelatlas.vercel.app/t/oklab-palette-quantization/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 4.36,
  "tier": "light",
  "p50_ms": 0.1435,
  "p95_ms": 0.1468,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "oklab-palette-quantization-specimen",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 3.18,
   "tier": "light",
   "p50_ms": 0.0506,
   "p95_ms": 0.0532,
   "speedup_x": 2.84,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 2.95,
   "tier": "light",
   "p50_ms": 0.085,
   "p95_ms": 0.0853,
   "speedup_x": 1.69,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}