from Color Map
Custom theme from a precomputed color map.
Use this when deriving colors from Material 3 dynamic color, app branding, or any non-CSS source. The colorMap keys are Highlight.js class names without the leading dot (e.g. "hljs-keyword", "hljs-string", "hljs"). The "hljs" entry is used to derive HighlightTheme.backgroundColor and HighlightTheme.defaultTextColor; you can also override those explicitly via backgroundColor and defaultTextColor.
Use the constants in HljsSelectors for known hljs scope keys to avoid typos and get IDE autocomplete. The full set of available scopes is documented in that object with @see links to the official hljs CSS Classes Reference.
val colorMap = mapOf(
HljsSelectors.BASE to SpanStyle(color = Color(0xFF24292E), background = Color(0xFFFFFFFF)),
HljsSelectors.KEYWORD to SpanStyle(color = Color(0xFFD73A49), fontWeight = FontWeight.Bold),
HljsSelectors.STRING to SpanStyle(color = Color(0xFF032F62)),
HljsSelectors.COMMENT to SpanStyle(color = Color(0xFF6A737D), fontStyle = FontStyle.Italic),
HljsSelectors.TITLE_FUNCTION to SpanStyle(color = Color(0xFF6F42C1)),
// ... add more token types as needed
)
val theme = HighlightTheme.fromColorMap(
name = "my-dynamic-theme",
colorMap = colorMap,
backgroundColor = Color(0xFFFFFFFF),
defaultTextColor = Color(0xFF24292E),
)Return
A HighlightTheme backed by the provided colorMap.
Parameters
Display name for the theme.
Map of hljs class name → SpanStyle. Use HljsSelectors constants for known scopes (e.g. HljsSelectors.KEYWORD instead of "hljs-keyword").
Optional explicit background color. If null, derived from colorMap["hljs"].
Optional explicit default text color. If null, derived from colorMap["hljs"].