grayscale
grayscale turns a color into a neutral gray at the same OKLCH lightness. It keeps the color’s opacity, which is handy when the original is a translucent overlay.
Example
import { colore } from 'colore-js';
import { grayscale } from 'colore-js/fn';
colore('#0f766e').grayscale().css(); // '#666666'
grayscale('#0f766e'); // '#666666'
Syntax
colore(color).grayscale(): Color
grayscale(color: ColorInput): string
Parameters
Returns
The method returns a new Color. The standalone function returns the gray as a CSS string. With ordinary sRGB colors, the result is hex.
Throws
A ColorError if the input is an invalid color.
Make a transparent overlay neutral
Use the same lightness and opacity on a neutral overlay. The last two digits of the hex result encode the original alpha rounded to one byte.
import { grayscale } from 'colore-js/fn';
grayscale('#2563eb80'); // '#70707080'
grayscale('rgb(37 99 235 / 25%)'); // '#70707040'
For an accent that still has some color, use desaturate with a percentage below 100.
Related
- desaturate to keep some chroma
- setAlpha to change the opacity
- Color spaces and ranges for OKLCH units