Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.4

0.19.4 / src / theme.lucb

## Semantic colors and font-relative metrics. Themes are values, inherited by
## layouts; a subtree may override one without mutating another application.
from luce_gpu import gpu
import interop
from luce_std import math
from luce_color import color, transfer
from font import Font
from text.style import EditorTheme
import result

## Compact spacing defaults, in logical points; explicit layout sizes stay literal.
pub let default_spacing: f64 = 8.0
pub let default_padding: f64 = 12.0

## How much of its ink a disabled control keeps (`Theme.disabled`).
pub let disabled_opacity: f64 = 0.35

pub struct Theme:
    pub var background: gpu.Color = gpu.Color(0.012, 0.017, 0.027)
    pub var panel: gpu.Color = gpu.Color(0.021, 0.028, 0.041)
    pub var foreground: gpu.Color = gpu.Color(0.68, 0.73, 0.82)
    pub var muted: gpu.Color = gpu.Color(0.26, 0.34, 0.45)
    pub var selection: gpu.Color = gpu.Color(0.035, 0.11, 0.22)
    pub var accent: gpu.Color = gpu.Color(0.2, 0.66, 0.92)
    pub var button: gpu.Color = gpu.Color(0.032, 0.044, 0.064)
    ## An explicit, brighter fill for a hovered button, rather than a dim panel tint.
    pub var button_hover: gpu.Color = gpu.Color(0.075, 0.11, 0.16)
    pub var pressed: gpu.Color = gpu.Color(0.055, 0.1, 0.16)
    ## Menus and dropdowns carry their own palette: the surface, a hovered title or
    ## row, and the selected row, independent of panels and text selection.
    pub var menu: gpu.Color = gpu.Color(0.028, 0.037, 0.053)
    pub var menu_hover: gpu.Color = gpu.Color(0.075, 0.11, 0.16)
    pub var menu_selection: gpu.Color = gpu.Color(0.06, 0.15, 0.26)
    pub var border: gpu.Color = gpu.Color(0.06, 0.085, 0.12)
    pub var active_border: gpu.Color = gpu.Color(0.10, 0.22, 0.32)
    pub var gutter: gpu.Color = gpu.Color(0.021, 0.028, 0.041)
    ## Version-control accents for lists such as the file explorer: `vcs_added` marks
    ## an untracked path, `vcs_modified` a tracked path with uncommitted changes.
    pub var vcs_added: gpu.Color = gpu.Color(0.36, 0.72, 0.42)
    pub var vcs_modified: gpu.Color = gpu.Color(0.85, 0.68, 0.28)
    ## Problem severities, for diagnostics in a code editor and anything else that
    ## reports an error, a warning or a note.
    pub var diagnostic_error: gpu.Color = gpu.Color(0.86, 0.26, 0.24)
    pub var diagnostic_warning: gpu.Color = gpu.Color(0.88, 0.64, 0.18)
    pub var diagnostic_info: gpu.Color = gpu.Color(0.2, 0.66, 0.92)
    pub var shadow: gpu.Color = gpu.Color()
    ## The recess a tab strip sits in; section headings.
    pub var strip: gpu.Color = gpu.Color(0.021, 0.028, 0.041)
    ## A sunken surface: field wells, slider grooves, a pane's bottom toolbar.
    pub var sunken: gpu.Color = gpu.Color(0.032, 0.044, 0.064)
    ## A raised surface: menus, popovers, raised buttons.
    pub var raised: gpu.Color = gpu.Color(0.028, 0.037, 0.053)
    ## Text and icons on the accent color.
    pub var accent_text: gpu.Color = gpu.Color(1.0, 1.0, 1.0)
    pub var shadow_offset: f64 = 4.0
    pub var shadow_opacity: f64 = 0.35
    pub var control_lines: f64 = 1.0
    pub var inset_cells: f64 = 1.0
    pub var header_inset_cells: f64 = 1.5
    pub var caret_width: f64 = 2.0
    ## Corner radius of buttons and similar controls, in points; zero is square.
    pub var control_radius: f64 = 0.0

    pub func validate() -> !:
        if not math.is_finite(self.header_inset_cells) or self.header_inset_cells < 0.0 or self.header_inset_cells > 4.0:
            error(result.invalid, "header inset must be in 0..4 character cells")
        if not math.is_finite(self.shadow_offset) or self.shadow_offset < 0.0 or self.shadow_offset > 32.0:
            error(result.invalid, "popup shadow offset must be in 0..32 points")
        if not math.is_finite(self.shadow_opacity) or self.shadow_opacity < 0.0 or self.shadow_opacity > 1.0:
            error(result.invalid, "popup shadow opacity must be in 0..1")
        if not math.is_finite(self.control_lines) or self.control_lines < 1.0 or self.control_lines > 4.0:
            error(result.invalid, "control height must be in 1..4 text lines")
        if not math.is_finite(self.inset_cells) or self.inset_cells < 0.0 or self.inset_cells > 4.0:
            error(result.invalid, "control inset must be in 0..4 character cells")
        if not math.is_finite(self.caret_width) or self.caret_width < 1.0 or self.caret_width > 8.0:
            error(result.invalid, "caret width must be in 1..8 points")
        if not math.is_finite(self.control_radius) or self.control_radius < 0.0 or self.control_radius > 32.0:
            error(result.invalid, "control radius must be in 0..32 points")

    ## Keep a small-font row tall enough for a checkbox and its border.
    pub func row_height(font: interop.Reference[Font]) -> f64:
        return math.max(16.0, math.ceil(font.value().line_height() * self.control_lines))

    pub func inset(font: interop.Reference[Font]) -> f64:
        return math.ceil(font.value().advance() * self.inset_cells)

    ## `ink` as a disabled control draws it: the same color at `disabled_opacity` of its
    ## alpha, so a disabled icon or label reads pale in either theme and over any face.
    pub func disabled(ink: gpu.Color) -> gpu.Color:
        return gpu.Color(ink.red, ink.green, ink.blue, ink.alpha * disabled_opacity)

    ## Interaction states are derived, not stored, so a palette cannot fall out of
    ## step with itself. Each lifts a surface toward the foreground in perceptual
    ## lightness, keeping its hue: a grayscale theme stays gray, a tinted one tints.
    pub func hover() -> gpu.Color:
        return lift(self.panel, self.foreground, 0.1)

    ## A hovered frame border sits partway from its resting to its focused color.
    pub func hover_border() -> gpu.Color:
        return between(self.border, self.active_border, 0.5)

    ## `ink` (a label, a muted detail, a kind badge) on a selected menu, popup or
    ## completion row (`menu_selection`): `accent_text` when the row is the accent color,
    ## where neither the foreground, `muted` nor the accent itself reads; else `ink`.
    pub func on_menu_selection(ink: gpu.Color) -> gpu.Color:
        return self.accent_text if self.menu_selection == self.accent else ink

    ## The same for a selected list or table row, or a default button (`selection`).
    pub func on_selection(ink: gpu.Color) -> gpu.Color:
        return self.accent_text if self.selection == self.accent else ink

    pub func gutter_active() -> gpu.Color:
        return lift(self.gutter, self.foreground, 0.12)

    pub func gutter_hover() -> gpu.Color:
        return lift(self.gutter, self.foreground, 0.06)

    pub func editor() -> EditorTheme:
        return EditorTheme(background = self.background, foreground = self.foreground,
                           gutter = self.gutter, line_number = self.muted, current_line = self.panel,
                           gutter_active = self.gutter_active(), gutter_hover = self.gutter_hover(), active_line_number = self.foreground,
                           selection = lift(self.selection, self.foreground, 0.28), caret = lift(self.accent, self.foreground, 0.6), caret_width = self.caret_width, scrollbar = self.border,
                           diagnostic_error = self.diagnostic_error, diagnostic_warning = self.diagnostic_warning, diagnostic_info = self.diagnostic_info)

## Perceptual lift of a surface toward a reference lightness, keeping its hue.
func lift(base: gpu.Color, reference: gpu.Color, amount: f64) -> gpu.Color:
    let shade = color.elevate(carry(base), carry(reference), amount)
    return gpu.Color(shade.red, shade.green, shade.blue)

## A perceptual blend between two colors, for a midpoint state like a hovered border.
func between(first: gpu.Color, second: gpu.Color, amount: f64) -> gpu.Color:
    let shade = color.blend(carry(first), carry(second), amount)
    return gpu.Color(shade.red, shade.green, shade.blue)

## Both models are linear-light sRGB triples, so bridging is a field-wise copy.
func carry(value: gpu.Color) -> color.Color:
    return color.Color(value.red, value.green, value.blue)

## The linear components a GPU target takes for an sRGB-encoded color (a picked
## HSB value, a typed hex), so it shows as the same color does elsewhere.
pub func srgb_decoded(value: gpu.Color) -> gpu.Color:
    return gpu.Color(transfer.srgb_decode(value.red), transfer.srgb_decode(value.green), transfer.srgb_decode(value.blue))