Typst Fumadocs

Color

Documentation for the Color type.

A color in a specific color space.

Typst supports:

Example

Loading compiler...

Predefined colors

Typst defines the following built-in colors:

ColorDefinition
blackluma(0)
grayluma(170)
silverluma(221)
whiteluma(255)
navyrgb("#001f3f")
bluergb("#0074d9")
aquargb("#7fdbff")
tealrgb("#39cccc")
easternrgb("#239dad")
purplergb("#b10dc9")
fuchsiargb("#f012be")
maroonrgb("#85144b")
redrgb("#ff4136")
orangergb("#ff851b")
yellowrgb("#ffdc00")
olivergb("#3d9970")
greenrgb("#2ecc40")
limergb("#01ff70")

The predefined colors and the most important color constructors are available globally and also in the color type's scope, so you can write either color.red or just red.

Preview

Predefined color maps

Typst also includes a number of preset color maps that can be used for gradients. These are simply arrays of colors defined in the module color.map.

Loading compiler...
MapDetails
turboA perceptually uniform rainbow-like color map. Read this blog post for more details.
cividisA blue to gray to yellow color map. See this blog post for more details.
rainbowCycles through the full color spectrum. This color map is best used by setting the interpolation color space to HSL. The rainbow gradient is not suitable for data visualization because it is not perceptually uniform, so the differences between values become unclear to your readers. It should only be used for decorative purposes.
spectralRed to yellow to blue color map.
viridisA purple to teal to yellow color map.
infernoA black to red to yellow color map.
magmaA black to purple to yellow color map.
plasmaA purple to pink to yellow color map.
rocketA black to red to white color map.
makoA black to teal to white color map.
coolwarmA blue to white to red color map with smooth transitions.
vlagA light blue to white to red color map.
icefireA light teal to black to orange color map.
flareA orange to purple color map that is perceptually uniform.
crestA light green to blue color map.

Some popular presets are not included because they are not available under a free licence. Others, like Jet, are not included because they are not color blind friendly. Feel free to use or create a package with other presets that are useful to you!

Preview

Methods

Create a grayscale color.

A grayscale color is represented internally by a single lightness component.

These components are also available using the components method.

Loading compiler...
#color.luma(
  lightness,
  alpha,
  color
) -> color

Parameters

Prop

Type

Create an Oklab color.

This color space is well suited for the following use cases:

  • Color manipulation such as saturating while keeping perceived hue
  • Creating grayscale images with uniform perceived lightness
  • Creating smooth and uniform color transition and gradients

A linear Oklab color is represented internally by an array of four components:

  • lightness (ratio)
  • a (float or ratio. Ratios are relative to 0.4; meaning 50% is equal to 0.2)
  • b (float or ratio. Ratios are relative to 0.4; meaning 50% is equal to 0.2)
  • alpha (ratio)

These components are also available using the components method.

Loading compiler...
#color.oklab(
  lightness,
  a,
  b,
  alpha,
  color
) -> color

Parameters

Prop

Type

Create an Oklch color.

This color space is well suited for the following use cases:

  • Color manipulation involving lightness, chroma, and hue
  • Creating grayscale images with uniform perceived lightness
  • Creating smooth and uniform color transition and gradients

A linear Oklch color is represented internally by an array of four components:

  • lightness (ratio)
  • chroma (float or ratio. Ratios are relative to 0.4; meaning 50% is equal to 0.2)
  • hue (angle)
  • alpha (ratio)

These components are also available using the components method.

Loading compiler...
#color.oklch(
  lightness,
  chroma,
  hue,
  alpha,
  color
) -> color

Parameters

Prop

Type

Create an RGB(A) color with linear luma.

This color space is similar to sRGB, but with the distinction that the color component are not gamma corrected. This makes it easier to perform color operations such as blending and interpolation. Although, you should prefer to use the oklab function for these.

A linear RGB(A) color is represented internally by an array of four components:

These components are also available using the components method.

Loading compiler...
#color.linear-rgb(
  red,
  green,
  blue,
  alpha,
  color
) -> color

Parameters

Prop

Type

Create an RGB(A) color.

The color is specified in the sRGB color space.

An RGB(A) color is represented internally by an array of four components:

These components are also available using the components method.

Loading compiler...
#color.rgb(
  red,
  green,
  blue,
  alpha,
  hex,
  color
) -> color

Parameters

Prop

Type

Create a CMYK color.

This is useful if you want to target a specific printer. The conversion to RGB for display preview might differ from how your printer reproduces the color.

A CMYK color is represented internally by an array of four components:

These components are also available using the components method.

Note that CMYK colors are not currently supported when PDF/A output is enabled.

Loading compiler...
#color.cmyk(
  cyan,
  magenta,
  yellow,
  key,
  color
) -> color

Parameters

Prop

Type

Create an HSL color.

This color space is useful for specifying colors by hue, saturation and lightness. It is also useful for color manipulation, such as saturating while keeping perceived hue.

An HSL color is represented internally by an array of four components:

These components are also available using the components method.

Loading compiler...
#color.hsl(
  hue,
  saturation,
  lightness,
  alpha,
  color
) -> color

Parameters

Prop

Type

Create an HSV color.

This color space is useful for specifying colors by hue, saturation and value. It is also useful for color manipulation, such as saturating while keeping perceived hue.

An HSV color is represented internally by an array of four components:

These components are also available using the components method.

Loading compiler...
#color.hsv(
  hue,
  saturation,
  value,
  alpha,
  color
) -> color

Parameters

Prop

Type

Extracts the components of this color.

The size and values of this array depends on the color space. You can obtain the color space using space. Below is a table of the color spaces and their components:

Color spaceC1C2C3C4
lumaLightness
oklabLightnessabAlpha
oklchLightnessChromaHueAlpha
linear-rgbRedGreenBlueAlpha
rgbRedGreenBlueAlpha
cmykCyanMagentaYellowKey
hslHueSaturationLightnessAlpha
hsvHueSaturationValueAlpha

For the meaning and type of each individual value, see the documentation of the corresponding color space. The alpha component is optional and only included if the alpha argument is true. The length of the returned array depends on the number of components and whether the alpha component is included.

Loading compiler...
#color.components(
  alpha: bool
) -> array

Parameters

Prop

Type

Returns the constructor function for this color's space.

Returns one of:

Loading compiler...

Returns the color's RGB(A) hex representation (such as #ffaa32 or #020304fe). The alpha component (last two digits in #020304fe) is omitted if it is equal to ff (255 / 100%).

Lightens a color by a given factor.

#color.lighten(
  factor
) -> color

Parameters

Prop

Type

Darkens a color by a given factor.

#color.darken(
  factor
) -> color

Parameters

Prop

Type

Increases the saturation of a color by a given factor.

#color.saturate(
  factor
) -> color

Parameters

Prop

Type

Decreases the saturation of a color by a given factor.

#color.desaturate(
  factor
) -> color

Parameters

Prop

Type

Produces the complementary color using a provided color space. You can think of it as the opposite side on a color wheel.

Loading compiler...
#color.negate(
  space: any
) -> color

Parameters

Prop

Type

Rotates the hue of the color by a given angle.

#color.rotate(
  angle,
  space: any
) -> color

Parameters

Prop

Type

Create a color by mixing two or more colors.

In color spaces with a hue component (hsl, hsv, oklch), only two colors can be mixed at once. Mixing more than two colors in such a space will result in an error!

Loading compiler...
#color.mix(
  colors,
  space: any
) -> color

Parameters

Prop

Type

Makes a color more transparent by a given factor.

This method is relative to the existing alpha value. If the scale is positive, calculates alpha - alpha * scale. Negative scales behave like color.opacify(-scale).

Loading compiler...
#color.transparentize(
  scale
) -> color

Parameters

Prop

Type

Makes a color more opaque by a given scale.

This method is relative to the existing alpha value. If the scale is positive, calculates alpha + scale - alpha * scale. Negative scales behave like color.transparentize(-scale).

Loading compiler...
#color.opacify(
  scale
) -> color

Parameters

Prop

Type

On this page