CKTIS / CSS
grayscale() — proportional grayscale conversion
`grayscale()` mixes an input image’s color components toward their luminance, controlling how much saturation is removed.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
grayscale( [ <number> | <percentage> ]? )grayscale() mixes an input image’s color components toward their luminance, controlling how much saturation is removed.
The example processes a red pixel: grayscale(50%) yields 155,27,27, while grayscale(100%) yields 54,54,54. grayscale(200%) gives the same full result because over-range amounts clamp; grayscale(-1) is invalid.
Grammar: grayscale( [ <number> | <percentage> ]? ).
The argument may be a number or percentage: 0 leaves the image unchanged and 1 means full conversion. An omitted argument defaults to 1, intermediate values linearly mix the effect, values above 1 clamp to 1, and negatives are forbidden. The filter operates on the rendered buffer and changes neither layout nor the source image data.
Examples
const sample=async filter=>{const svg='<svg xmlns="http://www.w3.org/2000/svg" width="4" height="4"><foreignObject width="4" height="4"><div xmlns="http://www.w3.org/1999/xhtml" style="width:4px;height:4px;background:rgb(255 0 0);filter:'+filter+'"></div></foreignObject></svg>';const image=new Image();image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(svg);await image.decode();const canvas=document.createElement('canvas');canvas.width=canvas.height=4;const context=canvas.getContext('2d');context.drawImage(image,0,0);return [...context.getImageData(2,2,1,1).data].slice(0,3).join(',')};
const half=await sample('grayscale(50%)');
const full=await sample('grayscale(100%)');
const over=await sample('grayscale(200%)');
const negative=CSS.supports('filter','grayscale(-1)');
console.log('half='+half+'; full='+full+'; over='+over+'; negative='+negative);Result
half=155,27,27; full=54,54,54; over=54,54,54; negative=false
`grayscale()` mixes an input image’s color components toward their luminance, controlling how much saturation is removed.