CKTIS / CSS
contrast() — image-filter contrast
`contrast()` scales RGB components away from or toward the midpoint, increasing or decreasing the difference between light and dark tones.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
contrast( [ <number> | <percentage> ]? )contrast() scales RGB components away from or toward the midpoint, increasing or decreasing the difference between light and dark tones.
The example sets filter: contrast(200%), computed as contrast(2): pixel rgb(64 128 192) becomes 0,128,255. Boundary contrast(0) collapses the color to gray 127,127,127, demonstrating real pixel processing.
Grammar: contrast( [ <number> | <percentage> ]? ).
The argument can be a <number> or <percentage>: 1 and 100% leave the image unchanged, 0 and 0% produce uniform gray, and values above 1 increase contrast without an upper bound. Negative values are forbidden and an omitted argument means 1. The filter does not alter alpha or layout geometry, and output components outside the range are clamped.
Examples
const source=document.createElement('canvas'),high=document.createElement('canvas'),flat=document.createElement('canvas');for(const canvas of [source,high,flat]){canvas.width=canvas.height=1}const input=source.getContext('2d');input.fillStyle='rgb(64 128 192)';input.fillRect(0,0,1,1);const highContext=high.getContext('2d');highContext.filter='contrast(200%)';highContext.drawImage(source,0,0);const flatContext=flat.getContext('2d');flatContext.filter='contrast(0)';flatContext.drawImage(source,0,0);const highPixel=[...highContext.getImageData(0,0,1,1).data].slice(0,3),flatPixel=[...flatContext.getImageData(0,0,1,1).data].slice(0,3);const box=document.createElement('div');document.body.append(box);box.style.filter='contrast(200%)';const computed=getComputedStyle(box).filter;if(computed!=='contrast(2)'||highPixel.join(',')!=='0,128,255'||flatPixel.join(',')!=='127,127,127')throw new Error('contrast filter evidence mismatch: '+[computed,highPixel,flatPixel]);console.log('computed='+computed+'; doubled='+highPixel.join(',')+'; zero='+flatPixel.join(','));Result
computed=contrast(2); doubled=0,128,255; zero=127,127,127
`contrast()` scales RGB components away from or toward the midpoint, increasing or decreasing the difference between light and dark tones.