Colore JS

Search documentation

Find a function or jump to a section.
v2.0.1

setAlpha

setAlpha sets a color’s opacity directly. Use it for an overlay, shadow or tint that should keep its original color channels.

Example

import { colore } from 'colore-js';
import { setAlpha } from 'colore-js/fn';

colore('#2563eb').setAlpha(0.5).css(); // '#2563eb80'
setAlpha('#2563eb', 0.5); // '#2563eb80'

Syntax

colore(color).setAlpha(alpha: number): Color
setAlpha(color: ColorInput, alpha: number): string

Parameters

NameTypeDescription
colorColorInputAny supported CSS color, tagged color object, Color instance or packed RGB number.
alphanumberOpacity from 0 (transparent) to 1 (opaque). Pass 0.5 for 50%. This replaces any alpha the color already carries.

Returns

The method returns a new Color. The standalone function returns a CSS string. In sRGB a fractional alpha is encoded as the last byte of an eight-digit hex color.

Throws

A ColorError if the input is invalid or the alpha is nonfinite or outside 0–1.

Set the opacity of an overlay

Use the same base color for several overlay strengths. On a broad-gamut color the output can stay in Display P3 with a decimal alpha channel.

import { setAlpha } from 'colore-js/fn';

setAlpha('#2563eb80', 0.25); // '#2563eb40'
setAlpha('color(display-p3 0 1 0)', 0.25); // 'color(display-p3 0.00000 1.00000 0.00000 / 0.25)'

If the overlay affects readability, check the resulting foreground and background together. A transparent color’s appearance depends on what sits behind it.

  • toHex for the hex alpha format
  • toCss for gamut-aware output
  • contrastRatio to measure a translucent color against its background