Color
Documentation for the Color type.
A color in a specific color space.
Typst supports:
- sRGB through the
rgbfunction - Device CMYK through the
cmykfunction - D65 Gray through the
lumafunction - Oklab through the
oklabfunction - Oklch through the
oklchfunction - Linear RGB through the
color.linear-rgbfunction - HSL through the
color.hslfunction - HSV through the
color.hsvfunction
Example
Predefined colors
Typst defines the following built-in colors:
| Color | Definition |
|---|---|
black | luma(0) |
gray | luma(170) |
silver | luma(221) |
white | luma(255) |
navy | rgb("#001f3f") |
blue | rgb("#0074d9") |
aqua | rgb("#7fdbff") |
teal | rgb("#39cccc") |
eastern | rgb("#239dad") |
purple | rgb("#b10dc9") |
fuchsia | rgb("#f012be") |
maroon | rgb("#85144b") |
red | rgb("#ff4136") |
orange | rgb("#ff851b") |
yellow | rgb("#ffdc00") |
olive | rgb("#3d9970") |
green | rgb("#2ecc40") |
lime | rgb("#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.
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.
| Map | Details |
|---|---|
turbo | A perceptually uniform rainbow-like color map. Read this blog post for more details. |
cividis | A blue to gray to yellow color map. See this blog post for more details. |
rainbow | Cycles 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. |
spectral | Red to yellow to blue color map. |
viridis | A purple to teal to yellow color map. |
inferno | A black to red to yellow color map. |
magma | A black to purple to yellow color map. |
plasma | A purple to pink to yellow color map. |
rocket | A black to red to white color map. |
mako | A black to teal to white color map. |
coolwarm | A blue to white to red color map with smooth transitions. |
vlag | A light blue to white to red color map. |
icefire | A light teal to black to orange color map. |
flare | A orange to purple color map that is perceptually uniform. |
crest | A 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!
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.
#color.luma(
lightness,
alpha,
color
) -> colorParameters
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 (
floatorratio. Ratios are relative to0.4; meaning50%is equal to0.2) - b (
floatorratio. Ratios are relative to0.4; meaning50%is equal to0.2) - alpha (
ratio)
These components are also available using the components method.
#color.oklab(
lightness,
a,
b,
alpha,
color
) -> colorParameters
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 (
floatorratio. Ratios are relative to0.4; meaning50%is equal to0.2) - hue (
angle) - alpha (
ratio)
These components are also available using the components method.
#color.oklch(
lightness,
chroma,
hue,
alpha,
color
) -> colorParameters
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.
#color.linear-rgb(
red,
green,
blue,
alpha,
color
) -> colorParameters
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.
#color.rgb(
red,
green,
blue,
alpha,
hex,
color
) -> colorParameters
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.
#color.cmyk(
cyan,
magenta,
yellow,
key,
color
) -> colorParameters
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.
#color.hsl(
hue,
saturation,
lightness,
alpha,
color
) -> colorParameters
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.
#color.hsv(
hue,
saturation,
value,
alpha,
color
) -> colorParameters
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 space | C1 | C2 | C3 | C4 |
|---|---|---|---|---|
luma | Lightness | |||
oklab | Lightness | a | b | Alpha |
oklch | Lightness | Chroma | Hue | Alpha |
linear-rgb | Red | Green | Blue | Alpha |
rgb | Red | Green | Blue | Alpha |
cmyk | Cyan | Magenta | Yellow | Key |
hsl | Hue | Saturation | Lightness | Alpha |
hsv | Hue | Saturation | Value | Alpha |
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.
#color.components(
alpha: bool
) -> arrayParameters
Prop
Type
Returns the constructor function for this color's space.
Returns one of:
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
) -> colorParameters
Prop
Type
Darkens a color by a given factor.
#color.darken(
factor
) -> colorParameters
Prop
Type
Increases the saturation of a color by a given factor.
#color.saturate(
factor
) -> colorParameters
Prop
Type
Decreases the saturation of a color by a given factor.
#color.desaturate(
factor
) -> colorParameters
Prop
Type
Produces the complementary color using a provided color space. You can think of it as the opposite side on a color wheel.
#color.negate(
space: any
) -> colorParameters
Prop
Type
Rotates the hue of the color by a given angle.
#color.rotate(
angle,
space: any
) -> colorParameters
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!
#color.mix(
colors,
space: any
) -> colorParameters
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).
#color.transparentize(
scale
) -> colorParameters
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).
#color.opacify(
scale
) -> colorParameters
Prop
Type