Skip to main content
Color builds a color from one input form and converts it to any other. Construct it as a call or with new; both work. You cannot mix input forms in a single call.
import { Color } from "@nemu-ai/pdf";

const a = Color({ hex: "#2563eb" });
const b = new Color({ rgb: [37, 99, 235] });
const c = Color({ hsl: [217, 91, 60] });

Input forms

A Color takes exactly one of the following keys. The shape of the accepted argument is the ColorInput type.
type ColorInput =
  | { hex: string }
  | { rgb: [number, number, number] }
  | { rgba: [number, number, number, number] }
  | { hsl: [number, number, number] }
  | { hsla: [number, number, number, number] };
hex
{ hex: string }
A hex string such as "#2563eb".
rgb
{ rgb: [number, number, number] }
Red, green, and blue channels from 0 to 255.
rgba
{ rgba: [number, number, number, number] }
Red, green, and blue channels from 0 to 255, plus an alpha from 0 to 1.
hsl
{ hsl: [number, number, number] }
Hue, saturation, and lightness.
hsla
{ hsla: [number, number, number, number] }
Hue, saturation, and lightness, plus an alpha from 0 to 1.
Pass a single input form per Color. Mixing forms, for example { hex, rgb }, is not supported.

Conversions and operations

A Color converts to any output form and supports common color operations. The operations that return a color produce a new Color and leave the original unchanged.
const a = Color({ hex: "#2563eb" });
const b = new Color({ rgb: [37, 99, 235] });

a.to_hex();      // "#2563eb"
a.to_hexa();
a.to_rgb();      // "rgb(37, 99, 235)"
a.to_rgba();     // "rgba(37, 99, 235, 1)"
a.to_hsl();
a.to_hsla();
a.alpha(0.5);    // a new Color at 50% opacity
a.lighten(0.1);
a.darken(0.1);
a.mix(b, 0.5);
a.luminance();
a.is_dark();     // true
to_hex()
string
Returns the color as a hex string.
to_hexa()
string
Returns the color as a hex string including the alpha channel.
to_rgb()
string
Returns an "rgb(...)" string.
to_rgba()
string
Returns an "rgba(...)" string.
to_hsl()
string
Returns an "hsl(...)" string.
to_hsla()
string
Returns an "hsla(...)" string.
alpha(value)
Color
Returns a new Color with the given alpha, from 0 to 1.
lighten(amount)
Color
Returns a new Color lightened by amount.
darken(amount)
Color
Returns a new Color darkened by amount.
mix(other, ratio)
Color
Returns a new Color blended with other by ratio.
luminance()
number
Returns the relative luminance of the color.
is_dark()
boolean
Returns whether the color reads as dark.

Using a Color anywhere a color is accepted

A Color is accepted anywhere a color is, so ColorLike = string | Color. Any color, background_color, or shape color field takes a hex string or a Color.
import { Doc, Color } from "@nemu-ai/pdf";

const accent = Color({ hex: "#2563eb" });

const doc = new Doc({ page_size: "A4", margin: 54 });

doc.set_style({
  heading: { color: accent },
  link: { color: accent.lighten(0.1) },
  code: { background_color: accent.alpha(0.08) },
});
Because operations return a new Color, you can derive a whole palette from one base color with lighten, darken, alpha, and mix.
See the Reference for the full list of color related type exports, including ColorLike, ColorInput, and ColorValue.