Skip to main content
Version: main

Colour access

Patterns for accessing colours in templates.

By role name

The most common way to access colours:

# Core
{{ get . "background" | hex }}
{{ get . "foreground" | hex }}

# Accents
{{ get . "accent1" | hex }}
{{ get . "accent2" | hex }}

# Semantic
{{ get . "danger" | hex }}
{{ get . "warning" | hex }}
{{ get . "success" | hex }}
{{ get . "info" | hex }}

# Surface
{{ get . "surface" | hex }}
{{ get . "onSurface" | hex }}

With fallbacks

Check if a role exists before using:

{{ if has . "border" }}
border {{ get . "border" | hex }}
{{ else }}
border {{ get . "outline" | hex }}
{{ end }}

By index

Access colours by luminance-sorted index:

{{- range $i := seq 0 15 }}
{{- $colour, _ := getByIndex . $i }}
colour{{ $i }} {{ $colour | hex }}
{{- end }}

ANSI terminal colours

Find closest matches to standard terminal colours:

# Standard 16 colours
{{ range $i, $name := list "black" "red" "green" "yellow" "blue" "magenta" "cyan" "white" }}
colour{{ $i }} {{ ansi $ $name | hex }}
{{ end }}

{{ range $i, $name := list "brightblack" "brightred" "brightgreen" "brightyellow" "brightblue" "brightmagenta" "brightcyan" "brightwhite" }}
colour{{ add $i 8 }} {{ ansi $ $name | hex }}
{{ end }}

Complete role list

Core (4)

RoleDescription
backgroundBase background
backgroundMutedInactive background
foregroundPrimary text
foregroundMutedSecondary text

Accents (8)

RoleDescription
accent1Primary accent
accent1MutedMuted variant
accent2Secondary accent
accent2MutedMuted variant
accent3Tertiary accent
accent3MutedMuted variant
accent4Quaternary accent
accent4MutedMuted variant

Semantic (5)

RoleDescription
dangerErrors, destructive actions
warningWarnings, caution
successSuccess, confirmation
infoInformation
notificationNotifications

Surface (8)

RoleDescription
surfaceCard backgrounds
onSurfaceText on surface
surfaceVariantAlternate surface
onSurfaceVariantText on variant
outlineBorders, dividers
outlineVariantSecondary borders
borderPrimary borders
borderMutedInactive borders

On-colours (8)

RoleDescription
onAccent1Text on accent1
onAccent2Text on accent2
onAccent3Text on accent3
onAccent4Text on accent4
onDangerText on danger
onWarningText on warning
onSuccessText on success
onInfoText on info

Inverse (3)

RoleDescription
inverseSurfaceTooltip background
inverseOnSurfaceText on inverse
inversePrimaryInverse accent

Overlay (2)

RoleDescription
scrimModal backdrop (32% opacity)
shadowElevation shadow (15% opacity)

Container elevation (5)

RoleDescription
surfaceContainerLowestElevation 0
surfaceContainerLowElevation 1
surfaceContainerElevation 2
surfaceContainerHighElevation 3
surfaceContainerHighestElevation 4

Positional (30+)

Used for ambient lighting effects:

positionTopLeft
positionTop
positionTopRight
positionRight
positionBottomRight
positionBottom
positionBottomLeft
positionLeft
positionCenter
# ... and more

Using variables

Cache colour lookups for efficiency:

{{- $bg := get . "background" }}
{{- $fg := get . "foreground" }}
{{- $accent := get . "accent1" }}

background {{ $bg | hex }}
foreground {{ $fg | hex }}
accent {{ $accent | hex }}

# Reuse with different formats
selection_bg {{ $accent | withAlpha 0.3 | rgba }}

See also