# frozen_string_literal: true
module CommunityLanding
class StyleBuilder
include Helpers
def initialize(settings = SiteSetting)
@s = settings
end
# CSS custom properties for accent colors, gradients, backgrounds
def color_overrides
accent = hex(@s.accent_color) || "#d4a24e"
accent_hover = hex(@s.accent_hover_color) || "#c4922e"
dark_bg = hex(@s.dark_bg_color) || "#06060f"
light_bg = hex(@s.light_bg_color) || "#faf6f0"
stat_icon = hex(@s.stat_icon_color) || accent
about_g1 = hex(@s.about_gradient_start) || "#fdf6ec"
about_g2 = hex(@s.about_gradient_mid) || "#fef9f0"
about_g3 = hex(@s.about_gradient_end) || "#fdf6ec"
about_bg_img = @s.about_background_image_url.presence
app_g1 = hex(@s.app_cta_gradient_start) || accent
app_g2 = hex(@s.app_cta_gradient_mid) || accent_hover
app_g3 = hex(@s.app_cta_gradient_end) || accent_hover
stat_icon_bg = hex(@s.stat_icon_bg_color.presence) rescue nil
stat_counter = hex(@s.stat_counter_color.presence) rescue nil
space_card_bg = hex(@s.groups_card_bg_color.presence) rescue nil
topic_card_bg = hex(@s.topics_card_bg_color.presence) rescue nil
video_btn_bg = hex(@s.hero_video_button_color.presence) rescue nil
hero_card_dark = hex(@s.hero_card_bg_dark.presence) rescue nil
hero_card_light = hex(@s.hero_card_bg_light.presence) rescue nil
hero_card_opacity = [[@s.hero_card_opacity.to_s.to_f, 0].max, 1].min rescue 0.85
hero_card_opacity = 0.85 if hero_card_opacity == 0.0 && (@s.hero_card_opacity.to_s.strip.empty? rescue true)
accent_rgb = hex_to_rgb(accent)
stat_icon_rgb = hex_to_rgb(stat_icon)
stat_icon_bg_val = stat_icon_bg || "rgba(#{stat_icon_rgb}, 0.1)"
stat_counter_val = stat_counter || "var(--cl-text-strong)"
space_card_bg_val = space_card_bg || "var(--cl-card)"
topic_card_bg_val = topic_card_bg || "var(--cl-card)"
# Hero card bg: convert hex + opacity to rgba
hero_card_dark_rgb = hero_card_dark ? hex_to_rgb(hero_card_dark) : "12, 12, 25"
hero_card_light_rgb = hero_card_light ? hex_to_rgb(hero_card_light) : "255, 255, 255"
hero_card_dark_val = "rgba(#{hero_card_dark_rgb}, #{hero_card_opacity})"
hero_card_light_val = "rgba(#{hero_card_light_rgb}, #{hero_card_opacity})"
about_bg_extra = about_bg_img ? ", url('#{about_bg_img}') center/cover no-repeat" : ""
video_btn_line = video_btn_bg ? "\n --cl-video-btn-bg: #{video_btn_bg};" : ""
"\n"
end
# Per-section dark/light background overrides
def section_backgrounds
css = +""
sections = [
["#cl-hero", safe_hex(:hero_bg_dark), safe_hex(:hero_bg_light)],
["#cl-stats-row", safe_hex(:stats_bg_dark), safe_hex(:stats_bg_light)],
["#cl-about", safe_hex(:about_bg_dark), safe_hex(:about_bg_light)],
["#cl-topics", safe_hex(:topics_bg_dark), safe_hex(:topics_bg_light)],
["#cl-groups", safe_hex(:groups_bg_dark), safe_hex(:groups_bg_light)],
["#cl-app-cta", safe_hex(:app_cta_bg_dark), safe_hex(:app_cta_bg_light)],
["#cl-footer", safe_hex(:footer_bg_dark), safe_hex(:footer_bg_light)],
]
sections.each do |sel, dark_bg, light_bg|
next unless dark_bg || light_bg
if dark_bg
css << ":root #{sel}, [data-theme=\"dark\"] #{sel} { background: #{dark_bg}; }\n"
end
if light_bg
css << "[data-theme=\"light\"] #{sel} { background: #{light_bg}; }\n"
css << "@media (prefers-color-scheme: light) { :root:not([data-theme=\"dark\"]) #{sel} { background: #{light_bg}; } }\n"
end
end
css.present? ? "\n" : ""
end
private
# Safe accessor — returns nil if the setting doesn't exist
def safe_hex(setting_name)
hex(@s.public_send(setting_name))
rescue
nil
end
end
end