2022-11-14 15:32:54 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-07-29 09:54:33 -07:00
|
|
|
module ThemeHelper
|
2020-05-06 04:36:43 -07:00
|
|
|
ATTRIBUTE_MAP = {
|
2023-01-29 12:13:38 -08:00
|
|
|
"primary_color" => %w[primary primary-rgb],
|
|
|
|
"primary_text" => "primary-text",
|
|
|
|
"danger_color" => "danger",
|
|
|
|
"danger_text" => "danger-text",
|
|
|
|
"warning_color" => "warning",
|
|
|
|
"warning_text" => "warning-text",
|
|
|
|
"info_color" => "info",
|
|
|
|
"info_text" => "info-text",
|
|
|
|
"success_color" => "success",
|
|
|
|
"success_text" => "success-text",
|
|
|
|
"dark_color" => "dark",
|
|
|
|
"dark_text" => "dark-text",
|
|
|
|
"light_color" => "light",
|
|
|
|
"light_text" => "light-text",
|
|
|
|
"raised_background" => %w[raised-bg raised-bg-rgb],
|
|
|
|
"raised_text" => "raised-text",
|
|
|
|
"raised_accent" => %w[raised-accent raised-accent-rgb],
|
|
|
|
"raised_accent_text" => "raised-accent-text",
|
|
|
|
"background_color" => "background",
|
|
|
|
"body_text" => "body-text",
|
|
|
|
"input_color" => "input-bg",
|
|
|
|
"input_text" => "input-text",
|
|
|
|
"input_placeholder" => "input-placeholder",
|
|
|
|
"muted_text" => "muted-text"
|
2020-05-06 04:36:43 -07:00
|
|
|
}.freeze
|
2020-05-03 08:28:41 -07:00
|
|
|
|
2020-05-06 04:36:43 -07:00
|
|
|
def render_theme
|
2020-05-03 08:28:41 -07:00
|
|
|
theme = get_active_theme
|
|
|
|
|
2020-05-06 04:36:43 -07:00
|
|
|
return unless theme
|
2020-05-03 08:28:41 -07:00
|
|
|
|
|
|
|
body = ":root {\n"
|
|
|
|
|
2020-05-06 04:36:43 -07:00
|
|
|
theme.attributes.each do |k, v|
|
|
|
|
next unless ATTRIBUTE_MAP.key?(k)
|
2020-05-03 08:28:41 -07:00
|
|
|
|
2023-01-12 10:35:26 -08:00
|
|
|
Array(ATTRIBUTE_MAP[k]).each do |var|
|
|
|
|
body += "\t--#{var}: #{get_color_for_key(var, v)};\n"
|
2020-05-06 04:36:43 -07:00
|
|
|
end
|
2020-05-03 08:28:41 -07:00
|
|
|
end
|
2023-01-12 09:30:55 -08:00
|
|
|
body += "\t--turbolinks-progress-color: ##{lighten(theme.primary_color)}\n}"
|
2020-05-06 04:36:43 -07:00
|
|
|
|
2020-05-08 17:07:58 -07:00
|
|
|
content_tag(:style, body)
|
2020-05-03 08:28:41 -07:00
|
|
|
end
|
|
|
|
|
2023-01-12 09:30:55 -08:00
|
|
|
def get_color_for_key(key, color)
|
|
|
|
hex = get_hex_color_from_theme_value(color)
|
|
|
|
|
|
|
|
if key.include?("text") || key.include?("placeholder") || key.include?("rgb")
|
|
|
|
get_decimal_triplet_from_hex(hex)
|
|
|
|
else
|
|
|
|
"##{hex}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-19 12:37:04 -07:00
|
|
|
def theme_color
|
|
|
|
theme = get_active_theme
|
|
|
|
if theme
|
2021-08-12 16:23:07 -07:00
|
|
|
theme.theme_color
|
2021-06-19 12:37:04 -07:00
|
|
|
else
|
2022-11-14 15:32:54 -08:00
|
|
|
"#5e35b1"
|
2021-06-19 12:37:04 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-05 15:36:20 -07:00
|
|
|
def mobile_theme_color
|
|
|
|
theme = get_active_theme
|
|
|
|
if theme
|
2021-08-12 16:23:07 -07:00
|
|
|
theme.mobile_theme_color
|
2021-08-05 15:36:20 -07:00
|
|
|
else
|
2022-11-14 15:32:54 -08:00
|
|
|
"#f0edf4"
|
2021-08-05 15:36:20 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-03 08:28:41 -07:00
|
|
|
def get_active_theme
|
2020-05-05 11:17:49 -07:00
|
|
|
if @user&.theme
|
2020-05-03 08:28:41 -07:00
|
|
|
if user_signed_in?
|
2020-05-05 11:17:49 -07:00
|
|
|
if current_user&.show_foreign_themes?
|
|
|
|
@user.theme
|
|
|
|
else
|
|
|
|
current_user&.theme
|
|
|
|
end
|
2020-05-03 08:28:41 -07:00
|
|
|
else
|
|
|
|
@user.theme
|
|
|
|
end
|
2021-12-29 16:41:27 -08:00
|
|
|
elsif @answer&.user&.theme
|
|
|
|
if user_signed_in?
|
|
|
|
if current_user&.show_foreign_themes?
|
|
|
|
@answer.user.theme
|
|
|
|
else
|
|
|
|
current_user&.theme
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@answer.user.theme
|
|
|
|
end
|
2020-05-05 11:17:49 -07:00
|
|
|
elsif current_user&.theme
|
|
|
|
current_user.theme
|
2020-05-03 08:28:41 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_hex_color_from_theme_value(value)
|
2022-11-14 15:32:54 -08:00
|
|
|
"0000000#{value.to_s(16)}"[-6, 6]
|
2020-05-03 08:28:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_decimal_triplet_from_hex(value)
|
2022-11-14 15:32:54 -08:00
|
|
|
hexes = value.split(/(.{2})/).reject(&:empty?)
|
|
|
|
hexes.map(&:hex).join(", ")
|
2020-05-03 08:28:41 -07:00
|
|
|
end
|
2021-04-04 07:23:11 -07:00
|
|
|
|
|
|
|
def rgb_values_from_hex(value)
|
|
|
|
[
|
|
|
|
(value & 0xFF0000) >> 16, # R
|
|
|
|
(value & 0x00FF00) >> 8, # G
|
|
|
|
value & 0x0000FF # B
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def rgb_to_hex(rgb_values)
|
2022-11-14 15:32:54 -08:00
|
|
|
rgb_values.map.with_index { |v, i| v << ((2 - i) * 8) }.reduce(&:+).to_s(16)
|
2021-04-04 07:23:11 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def lighten(value, amount = 0.25)
|
2022-11-14 15:32:54 -08:00
|
|
|
rgb_to_hex(rgb_values_from_hex(value).map { |v| [(v + (255 * amount)).round, 255].min })
|
2021-04-04 07:23:11 -07:00
|
|
|
end
|
2015-07-29 09:54:33 -07:00
|
|
|
end
|