toRgb
toRgb converts a color to a CSS rgb() string. The result uses integer sRGB channels and includes an alpha value when the input is translucent.
Example
import { colore } from 'colore-js';
import { toRgb } from 'colore-js/fn';
colore('#2563eb').rgb(); // 'rgb(37 99 235)'
toRgb('#2563eb'); // 'rgb(37 99 235)'
Syntax
colore(color).rgb(): string
toRgb(color: ColorInput): string
Parameters
Returns
A CSS string with three integer channels from 0 to 255. If alpha is less than 1, the output includes a slash and alpha rounded to at most four decimal places. Colors outside sRGB are clipped.
Throws
A ColorError if the input is invalid.
Convert a transparent hex token to CSS or numbers
Use the string for a CSS declaration. The class method named toRgb() gives you numeric channels and the full alpha value for use in application code.
import { colore } from 'colore-js';
import { toRgb } from 'colore-js/fn';
toRgb('#2563eb80'); // 'rgb(37 99 235 / 0.502)'
colore('#2563eb80').toRgb(); // {r: 37, g: 99, b: 235, alpha: 0.5019607843137255}
The class object channels are rounded to integers but can extend beyond the sRGB range. Use toObject('srgb') when you need floating-point channels.
Related
- toHex for compact sRGB output
- toCss to preserve wide-gamut colors
- Object output methods for numbers in other color spaces