Class: Studio::ThemeResolver
- Inherits:
-
Object
- Object
- Studio::ThemeResolver
- Defined in:
- lib/studio/theme_resolver.rb
Constant Summary collapse
- ROLES =
%i[primary dark light success warning danger accent].freeze
Instance Attribute Summary collapse
-
#colors ⇒ Object
readonly
Returns the value of attribute colors.
Instance Method Summary collapse
-
#contrast_ink(base, direction:, start:, target:, against:) ⇒ Object
Bounded, clamped search: raise the blend amount from
startuntil the ink clearstargetcontrast against every background inagainst. - #dark_mode_vars ⇒ Object
-
#dark_surfaces(dark_base) ⇒ Object
Every dark-mode background a text var can sit on (page, surface, surface-alt, inset) — the ink must clear its target on ALL of them.
-
#initialize(colors = {}) ⇒ ThemeResolver
constructor
colors: hash of role => hex string (e.g. { primary: "#8E82FE", dark: "#1A1535", ... }).
- #light_mode_vars ⇒ Object
- #light_surfaces(light_base) ⇒ Object
-
#primary_palette_vars ⇒ Object
Generate --color-primary-50.50..900 + RGB variants for Tailwind opacity support.
- #to_css ⇒ Object
Constructor Details
#initialize(colors = {}) ⇒ ThemeResolver
colors: hash of role => hex string (e.g. { primary: "#8E82FE", dark: "#1A1535", ... })
8 9 10 |
# File 'lib/studio/theme_resolver.rb', line 8 def initialize(colors = {}) @colors = colors.symbolize_keys end |
Instance Attribute Details
#colors ⇒ Object (readonly)
Returns the value of attribute colors.
5 6 7 |
# File 'lib/studio/theme_resolver.rb', line 5 def colors @colors end |
Instance Method Details
#contrast_ink(base, direction:, start:, target:, against:) ⇒ Object
Bounded, clamped search: raise the blend amount from start until the
ink clears target contrast against every background in against.
Clamps at 1.0 (pure white/black), so a pathological base degrades to the
best achievable ink instead of looping.
86 87 88 89 90 91 92 93 94 |
# File 'lib/studio/theme_resolver.rb', line 86 def contrast_ink(base, direction:, start:, target:, against:) amount = start loop do ink = direction == :lighten ? ColorScale.lighten(base, amount) : ColorScale.darken(base, amount) return ink if against.all? { |bg| ColorScale.contrast_ratio(ink, bg) >= target } || amount >= 1.0 amount = [amount + 0.02, 1.0].min end end |
#dark_mode_vars ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/studio/theme_resolver.rb', line 30 def dark_mode_vars dark_base = colors[:dark] || "#1A1535" primary = colors[:primary] || "#8E82FE" border_rgb = ColorScale.lighten(dark_base, 0.30) { "--color-page" => dark_base, "--color-surface" => ColorScale.lighten(dark_base, 0.15), "--color-surface-alt" => ColorScale.darken(dark_base, 0.14), "--color-inset" => ColorScale.darken(dark_base, 0.43), "--color-text" => "#ffffff", "--color-text-body" => "#e2e8f0", # Derived by a bounded contrast search, not hardcoded: fixed slate # grays fail WCAG on themes whose dark base lifts the surface (a # hardcoded #64748b measured 2.14:1 on a navy surface), and any FIXED # blend amount is tuned to particular hexes — the operator can pick an # arbitrary base in the theme editor. contrast_ink walks the blend up # until the ink clears its target on every emitted dark surface # (clamped at pure white for pathological bases). Note the blend # DESATURATES toward gray; only strongly-tinted bases keep a cast. "--color-text-secondary" => contrast_ink(dark_base, direction: :lighten, start: 0.70, target: 4.5, against: dark_surfaces(dark_base)), "--color-text-muted" => contrast_ink(dark_base, direction: :lighten, start: 0.55, target: 3.0, against: dark_surfaces(dark_base)), "--color-border" => ColorScale.with_opacity(border_rgb, 0.2), "--color-border-strong" => ColorScale.with_opacity(border_rgb, 0.4), "--color-shadow" => "transparent", "--color-cta" => primary, "--color-cta-hover" => ColorScale.darken(primary, 0.30), "--color-success" => colors[:success] || "#4BAF50", "--color-warning" => colors[:warning] || "#FF7C47", "--color-danger" => colors[:danger] || "#EF4444", "--color-accent" => colors[:accent] || "#F72585" } end |
#dark_surfaces(dark_base) ⇒ Object
Every dark-mode background a text var can sit on (page, surface, surface-alt, inset) — the ink must clear its target on ALL of them.
68 69 70 71 72 73 |
# File 'lib/studio/theme_resolver.rb', line 68 def dark_surfaces(dark_base) [ dark_base, ColorScale.lighten(dark_base, 0.15), ColorScale.darken(dark_base, 0.14), ColorScale.darken(dark_base, 0.43) ] end |
#light_mode_vars ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/studio/theme_resolver.rb', line 116 def light_mode_vars light_base = colors[:light] || "#f8fafc" primary = colors[:primary] || "#8E82FE" { "--color-page" => light_base, "--color-surface" => "#ffffff", "--color-surface-alt" => ColorScale.darken(light_base, 0.03), "--color-inset" => ColorScale.darken(light_base, 0.08), "--color-text" => "#0f172a", "--color-text-body" => "#334155", # Same bounded search as dark mode: the old fixed grays measured as # low as 2.05:1 (muted on --color-inset) — below the very defect this # derivation exists to prevent. Ink darkens away from the light base. "--color-text-secondary" => contrast_ink(light_base, direction: :darken, start: 0.55, target: 4.5, against: light_surfaces(light_base)), "--color-text-muted" => contrast_ink(light_base, direction: :darken, start: 0.40, target: 3.0, against: light_surfaces(light_base)), "--color-border" => ColorScale.darken(light_base, 0.08), "--color-border-strong" => ColorScale.darken(light_base, 0.15), "--color-shadow" => "rgba(0,0,0,0.05)", "--color-cta" => primary, "--color-cta-hover" => ColorScale.darken(primary, 0.30), "--color-success" => colors[:success] || "#4BAF50", "--color-warning" => colors[:warning] || "#FF7C47", "--color-danger" => colors[:danger] || "#EF4444", "--color-accent" => colors[:accent] || "#F72585" } end |
#light_surfaces(light_base) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/studio/theme_resolver.rb', line 75 def light_surfaces(light_base) [ light_base, "#ffffff", ColorScale.darken(light_base, 0.03), ColorScale.darken(light_base, 0.08) ] end |
#primary_palette_vars ⇒ Object
Generate --color-primary-Studio::ThemeResolver.50.50..900 + RGB variants for Tailwind opacity support
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/studio/theme_resolver.rb', line 97 def primary_palette_vars primary = colors[:primary] || "#8E82FE" scale = ColorScale.generate(primary) vars = {} scale.each do |shade, hex| vars["--color-primary-#{shade}"] = hex r, g, b = ColorScale.hex_to_rgb(hex) vars["--color-primary-#{shade}-rgb"] = "#{r} #{g} #{b}" end # DEFAULT aliases r, g, b = ColorScale.hex_to_rgb(primary) vars["--color-primary"] = primary vars["--color-primary-rgb"] = "#{r} #{g} #{b}" vars end |
#to_css ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/studio/theme_resolver.rb', line 12 def to_css dark_vars = dark_mode_vars.map { |k, v| " #{k}: #{v};" }.join("\n") light_vars = light_mode_vars.map { |k, v| " #{k}: #{v};" }.join("\n") palette_vars = primary_palette_vars.map { |k, v| " #{k}: #{v};" }.join("\n") <<~CSS :root, .dark { #{dark_vars} #{palette_vars} } html:not(.dark) { #{light_vars} #{palette_vars} } CSS end |