dymokomi/luced
Source of release 0.4.1
0.4.1 / src / configuration / theme.luc
## Configured hex colors are sRGB; standard gpu colors use linear components.
from luce_ui.ui import Theme
from luce_gpu.gpu import Color
from language.appearance import SyntaxTheme
from luce_config.toml import Table
from luce_std import math
from luce_ui import ui
pub class Appearance:
pub let theme: Theme
pub let syntax: SyntaxTheme
pub func init(self, source: str = "") -> unit!:
let table = Table(source)
table.check_keys(["colors.background", "colors.panel", "colors.foreground", "colors.muted", "colors.selection", "colors.accent", "colors.button", "colors.button_hover", "colors.pressed", "colors.menu", "colors.menu_hover", "colors.menu_selection", "colors.border", "colors.active_border", "colors.gutter", "colors.vcs_added", "colors.vcs_modified", "colors.shadow", "colors.strip", "colors.sunken", "colors.raised", "colors.accent_text", "popup.shadow_offset", "popup.shadow_opacity", "layout.header_inset_cells", "layout.caret_width", "layout.control_radius", "syntax.keyword", "syntax.string", "syntax.comment", "syntax.section", "syntax.number", "syntax.type", "syntax.other"])
var theme = Theme()
theme.background = read_color(table, "colors.background", theme.background)
theme.panel = read_color(table, "colors.panel", theme.panel)
theme.foreground = read_color(table, "colors.foreground", theme.foreground)
theme.muted = read_color(table, "colors.muted", theme.muted)
theme.selection = read_color(table, "colors.selection", theme.selection)
theme.accent = read_color(table, "colors.accent", theme.accent)
theme.button = read_color(table, "colors.button", theme.button)
theme.button_hover = read_color(table, "colors.button_hover", theme.button_hover)
theme.pressed = read_color(table, "colors.pressed", theme.pressed)
theme.menu = read_color(table, "colors.menu", theme.menu)
theme.menu_hover = read_color(table, "colors.menu_hover", theme.menu_hover)
theme.menu_selection = read_color(table, "colors.menu_selection", theme.menu_selection)
theme.border = read_color(table, "colors.border", theme.border)
theme.active_border = read_color(table, "colors.active_border", theme.active_border)
theme.gutter = read_color(table, "colors.gutter", theme.gutter)
theme.vcs_added = read_color(table, "colors.vcs_added", theme.vcs_added)
theme.vcs_modified = read_color(table, "colors.vcs_modified", theme.vcs_modified)
theme.shadow = read_color(table, "colors.shadow", theme.shadow)
theme.strip = read_color(table, "colors.strip", theme.strip)
theme.sunken = read_color(table, "colors.sunken", theme.sunken)
theme.raised = read_color(table, "colors.raised", theme.raised)
theme.accent_text = read_color(table, "colors.accent_text", theme.accent_text)
theme.shadow_offset = table.number("popup.shadow_offset", theme.shadow_offset, 0.0, 32.0)
theme.shadow_opacity = table.number("popup.shadow_opacity", theme.shadow_opacity, 0.0, 1.0)
theme.header_inset_cells = table.number("layout.header_inset_cells", theme.header_inset_cells, 0.0, 4.0)
theme.caret_width = table.number("layout.caret_width", theme.caret_width, 1.0, 8.0)
theme.control_radius = table.number("layout.control_radius", theme.control_radius, 0.0, 32.0)
var syntax: map[str, Color] = {}
syntax["keyword"] = read_color(table, "syntax.keyword", Color(0.55, 0.26, 0.65))
syntax["string"] = read_color(table, "syntax.string", Color(0.39, 0.58, 0.28))
syntax["comment"] = read_color(table, "syntax.comment", Color(0.19, 0.32, 0.31))
syntax["section"] = read_color(table, "syntax.section", Color(0.663, 0.812, 0.788))
syntax["number"] = read_color(table, "syntax.number", Color(0.72, 0.43, 0.19))
syntax["type"] = read_color(table, "syntax.type", Color(0.12, 0.54, 0.65))
syntax["other"] = read_color(table, "syntax.other", Color(0.57, 0.64, 0.36))
self.theme = theme
self.syntax = SyntaxTheme(syntax)
func read_color(table: Table, key: str, fallback: Color) -> Color!:
let value = table.values[key] else return fallback
let text = value.string()
if text.length != 7 or not text.starts_with("#"):
error(ui.invalid, f"Line {value.line}: {key} needs a color like #1C232E")
var components: list[float] = []
for index in 0..<3:
let start = 1 + index * 2
var number = 0
for character in text[start..<start + 2].lower():
let digit = "0123456789abcdef".index_of(character) else error(ui.invalid, f"Line {value.line}: invalid hexadecimal color")
number = number * 16 + digit
let encoded = float(number) / 255.0
components.append(encoded / 12.92 if encoded <= 0.04045 else math.pow((encoded + 0.055) / 1.055, 2.4))
return Color(components[0], components[1], components[2])
pub func defaults() -> str:
return "# luced theme, after eleusis-layout: greys with one orange. Colors are sRGB hex; save to apply immediately.\n\n[colors]\nbackground = \"#1a1a1a\"\npanel = \"#323232\"\nstrip = \"#262626\"\nsunken = \"#292929\"\nraised = \"#2e2e2e\"\nforeground = \"#d9d9d9\"\nmuted = \"#8c8c8c\"\nselection = \"#e7a03f\"\naccent = \"#e7a03f\"\naccent_text = \"#000000\"\n\nbutton = \"#292929\"\nbutton_hover = \"#3a3a3a\"\npressed = \"#4a4a4a\"\n\nmenu = \"#2e2e2e\"\nmenu_hover = \"#3a3a3a\"\nmenu_selection = \"#e7a03f\"\n\nborder = \"#3a3a3a\"\nactive_border = \"#969696\"\ngutter = \"#262626\"\nvcs_added = \"#6FCF7F\"\nvcs_modified = \"#E0B341\"\nshadow = \"#000000\"\n\n[popup]\nshadow_offset = 0.0\nshadow_opacity = 0.0\n\n[layout]\nheader_inset_cells = 1.5\ncaret_width = 2.0\ncontrol_radius = 5.0\n\n[syntax]\nkeyword = \"#ff7959\"\nstring = \"#76715a\"\ncomment = \"#799997\"\nsection = \"#A9CFC9\"\nnumber = \"#8eadff\"\ntype = \"#61c2b7\"\nother = \"#bd82d3\"\n"