Module: Fatty::Colors::Palette

Defined in:
lib/fatty/colors/palette.rb

Overview

Palette compiles a resolved symbolic theme_spec into a renderer-ready palette.

A theme_spec is produced by Fatty::Themes::Resolver. It maps role names to symbolic color specifications, usually color names and attributes:

+begin_src ruby

{ output: { fg: "white", bg: "navy" }, alert_warn: { fg: "yellow", bg: "pink", attrs: [:bold] }, }

+end_src

A palette maps those same role names to concrete rendering data:

+begin_src ruby

{ output: { fg: 7, bg: 4, fg_rgb: [255, 255, 255], bg_rgb: [0, 0, 128], pair: 1, attrs: [], }, }

+end_src

Curses renderers use :pair and :attrs. Truecolor renderers use :fg_rgb, :bg_rgb, and :attrs.

  • .build :: returns the compiled palette without touching curses.
  • .apply! :: also initializes curses color pairs.

Constant Summary collapse

DEFAULT_ROLE =
{ fg: "default", bg: "default" }.freeze

Class Method Summary collapse

Class Method Details

.compile(theme_spec, available_colors:) ⇒ Object

Compile a theme_spec into a renderer-ready palette without initializing curses color pairs.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fatty/colors/palette.rb', line 44

def self.compile(theme_spec, available_colors:)
  palette = {}
  roles = (theme_spec.keys + Pairs::ROLE_TO_PAIR.keys).compact.uniq

  roles.each do |role|
    role_spec = theme_spec.fetch(role, DEFAULT_ROLE)

    fg_spec = role_spec[:fg] || DEFAULT_ROLE[:fg]
    bg_spec = role_spec[:bg] || DEFAULT_ROLE[:bg]

    fg = Fatty::Color.resolve(fg_spec, available_colors: available_colors)
    bg = Fatty::Color.resolve(bg_spec, available_colors: available_colors)
    attrs = Array(role_spec[:attrs] || role_spec["attrs"]).map(&:to_sym)
    fg_rgb = Fatty::Color.rgb(fg_spec)
    bg_rgb = Fatty::Color.rgb(bg_spec)

    palette[role] = {
      fg: fg,
      bg: bg,
      fg_rgb: fg_rgb,
      bg_rgb: bg_rgb,
      pair: Pairs::ROLE_TO_PAIR[role],
      attrs: attrs,
    }
  end
  palette
end