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.
102 103 104 105 106 107 108 109 110 |
# File 'lib/studio/theme_resolver.rb', line 102 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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) danger = colors[:danger] || "#EF4444" { "--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" => danger, # THE DANGER *INK*, which is not the danger *colour*. --color-danger is a # brand fill (button backgrounds, borders) and is free to be vivid; # --color-danger-ink is the same red used as TEXT, where it must clear # WCAG AA 4.5:1 on every surface it can land on. # # No STATIC red clears AA on BOTH themes — measured: text-red-300 is # 1.92:1 light / 5.81 dark, text-red-400 2.77 / 4.03, #EF4444 3.76 / 2.96, # text-red-600 4.77 / 2.34. So it is DERIVED per theme by the same bounded # search that already produces --color-text-secondary and --color-text-muted. # # start: 0.0 is deliberate — the search tries the operator's actual danger # colour FIRST and blends only as far as AA demands, so a theme whose red # already passes keeps its exact brand hex. "--color-danger-ink" => contrast_ink(danger, direction: :lighten, start: 0.0, target: 4.5, against: dark_surfaces(dark_base)), "--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.
84 85 86 87 88 89 |
# File 'lib/studio/theme_resolver.rb', line 84 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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/studio/theme_resolver.rb', line 132 def light_mode_vars light_base = colors[:light] || "#f8fafc" primary = colors[:primary] || "#8E82FE" danger = colors[:danger] || "#EF4444" { "--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" => danger, # THE DANGER *INK*, which is not the danger *colour*. --color-danger is a # brand fill (button backgrounds, borders) and is free to be vivid; # --color-danger-ink is the same red used as TEXT, where it must clear # WCAG AA 4.5:1 on every surface it can land on. # # No STATIC red clears AA on BOTH themes — measured: text-red-300 is # 1.92:1 light / 5.81 dark, text-red-400 2.77 / 4.03, #EF4444 3.76 / 2.96, # text-red-600 4.77 / 2.34. So it is DERIVED per theme by the same bounded # search that already produces --color-text-secondary and --color-text-muted. # # start: 0.0 is deliberate — the search tries the operator's actual danger # colour FIRST and blends only as far as AA demands, so a theme whose red # already passes keeps its exact brand hex. "--color-danger-ink" => contrast_ink(danger, direction: :darken, start: 0.0, target: 4.5, against: light_surfaces(light_base)), "--color-accent" => colors[:accent] || "#F72585" } end |
#light_surfaces(light_base) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/studio/theme_resolver.rb', line 91 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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/studio/theme_resolver.rb', line 113 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 |