2018-05-27 20:46:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-16 08:49:06 -05:00
|
|
|
class ColorScheme < ActiveRecord::Base
|
|
|
|
|
2019-12-09 18:48:27 -06:00
|
|
|
# rubocop:disable Layout/HashAlignment
|
2018-12-04 03:48:16 -06:00
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
CUSTOM_SCHEMES = {
|
2018-07-24 08:00:20 -05:00
|
|
|
'Dark': {
|
2017-04-12 09:52:52 -05:00
|
|
|
"primary" => 'dddddd',
|
|
|
|
"secondary" => '222222',
|
|
|
|
"tertiary" => '0f82af',
|
|
|
|
"quaternary" => 'c14924',
|
|
|
|
"header_background" => '111111',
|
2017-10-03 12:01:05 -05:00
|
|
|
"header_primary" => 'dddddd',
|
2017-04-12 09:52:52 -05:00
|
|
|
"highlight" => 'a87137',
|
|
|
|
"danger" => 'e45735',
|
|
|
|
"success" => '1ca551',
|
|
|
|
"love" => 'fa6c8d'
|
2018-07-24 08:00:20 -05:00
|
|
|
},
|
|
|
|
# By @itsbhanusharma
|
|
|
|
'Neutral': {
|
|
|
|
"primary" => '000000',
|
|
|
|
"secondary" => 'ffffff',
|
|
|
|
"tertiary" => '51839b',
|
|
|
|
"quaternary" => 'b85e48',
|
|
|
|
"header_background" => '333333',
|
|
|
|
"header_primary" => 'f3f3f3',
|
|
|
|
"highlight" => 'ecec70',
|
|
|
|
"danger" => 'b85e48',
|
|
|
|
"success" => '518751',
|
|
|
|
"love" => 'fa6c8d'
|
|
|
|
},
|
|
|
|
# By @Flower_Child
|
|
|
|
'Grey Amber': {
|
|
|
|
"primary" => 'd9d9d9',
|
|
|
|
"secondary" => '3d4147',
|
|
|
|
"tertiary" => 'fdd459',
|
|
|
|
"quaternary" => 'fdd459',
|
|
|
|
"header_background" => '36393e',
|
|
|
|
"header_primary" => 'd9d9d9',
|
|
|
|
"highlight" => 'fdd459',
|
|
|
|
"danger" => 'e45735',
|
|
|
|
"success" => 'fdd459',
|
|
|
|
"love" => 'fdd459'
|
|
|
|
},
|
2018-08-08 15:01:21 -05:00
|
|
|
# By @rafafotes
|
2018-07-24 08:00:20 -05:00
|
|
|
'Shades of Blue': {
|
|
|
|
"primary" => '203243',
|
|
|
|
"secondary" => 'eef4f7',
|
|
|
|
"tertiary" => '416376',
|
|
|
|
"quaternary" => '5e99b9',
|
|
|
|
"header_background" => '86bddb',
|
|
|
|
"header_primary" => 'ffffff',
|
|
|
|
"highlight" => '86bddb',
|
|
|
|
"danger" => 'bf3c3c',
|
|
|
|
"success" => '70db82',
|
|
|
|
"love" => 'fc94cb'
|
|
|
|
},
|
|
|
|
# By @mikechristopher
|
|
|
|
'Latte': {
|
2018-07-25 14:12:01 -05:00
|
|
|
"primary" => 'f2e5d7',
|
2018-07-24 08:00:20 -05:00
|
|
|
"secondary" => '262322',
|
|
|
|
"tertiary" => 'f7f2ed',
|
|
|
|
"quaternary" => 'd7c9aa',
|
|
|
|
"header_background" => 'd7c9aa',
|
|
|
|
"header_primary" => '262322',
|
|
|
|
"highlight" => 'd7c9aa',
|
|
|
|
"danger" => 'db9584',
|
|
|
|
"success" => '78be78',
|
|
|
|
"love" => '8f6201'
|
|
|
|
},
|
|
|
|
# By @Flower_Child
|
|
|
|
'Summer': {
|
|
|
|
"primary" => '874342',
|
|
|
|
"secondary" => 'fffff4',
|
|
|
|
"tertiary" => 'fe9896',
|
|
|
|
"quaternary" => 'fcc9d0',
|
|
|
|
"header_background" => '96ccbf',
|
|
|
|
"header_primary" => 'fff1e7',
|
|
|
|
"highlight" => 'f3c07f',
|
|
|
|
"danger" => 'cfebdc',
|
|
|
|
"success" => 'fcb4b5',
|
|
|
|
"love" => 'f3c07f'
|
|
|
|
},
|
|
|
|
# By @Flower_Child
|
|
|
|
'Dark Rose': {
|
|
|
|
"primary" => 'ca9cb2',
|
|
|
|
"secondary" => '3a2a37',
|
|
|
|
"tertiary" => 'fdd459',
|
|
|
|
"quaternary" => '7e566a',
|
|
|
|
"header_background" => 'a97189',
|
|
|
|
"header_primary" => 'd9b2bb',
|
|
|
|
"highlight" => '6c3e63',
|
|
|
|
"danger" => '6c3e63',
|
|
|
|
"success" => 'd9b2bb',
|
|
|
|
"love" => 'd9b2bb'
|
2017-04-12 09:52:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:48:27 -06:00
|
|
|
# rubocop:enable Layout/HashAlignment
|
2018-12-04 03:48:16 -06:00
|
|
|
|
2019-05-09 04:22:28 -05:00
|
|
|
LIGHT_THEME_ID = 'Light'
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def self.base_color_scheme_colors
|
2016-09-02 10:42:14 -05:00
|
|
|
base_with_hash = {}
|
2018-12-04 03:48:16 -06:00
|
|
|
|
2016-09-02 10:42:14 -05:00
|
|
|
base_colors.each do |name, color|
|
2017-04-12 09:52:52 -05:00
|
|
|
base_with_hash[name] = "#{color}"
|
2016-09-02 10:42:14 -05:00
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
list = [
|
2019-05-09 04:22:28 -05:00
|
|
|
{ id: LIGHT_THEME_ID, colors: base_with_hash }
|
2016-09-02 10:42:14 -05:00
|
|
|
]
|
2017-04-12 09:52:52 -05:00
|
|
|
|
|
|
|
CUSTOM_SCHEMES.each do |k, v|
|
|
|
|
list.push(id: k.to_s, colors: v)
|
|
|
|
end
|
2018-12-04 03:48:16 -06:00
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
list
|
2016-09-02 10:42:14 -05:00
|
|
|
end
|
|
|
|
|
2015-09-23 01:47:17 -05:00
|
|
|
def self.hex_cache
|
|
|
|
@hex_cache ||= DistributedCache.new("scheme_hex_for_name")
|
|
|
|
end
|
|
|
|
|
2014-05-02 16:46:03 -05:00
|
|
|
attr_accessor :is_base
|
|
|
|
|
2014-04-16 08:49:06 -05:00
|
|
|
has_many :color_scheme_colors, -> { order('id ASC') }, dependent: :destroy
|
|
|
|
|
|
|
|
alias_method :colors, :color_scheme_colors
|
|
|
|
|
2018-03-15 02:26:54 -05:00
|
|
|
before_save :bump_version
|
2014-11-10 22:32:44 -06:00
|
|
|
after_save :publish_discourse_stylesheet
|
2015-09-23 01:47:17 -05:00
|
|
|
after_save :dump_hex_cache
|
|
|
|
after_destroy :dump_hex_cache
|
2017-04-17 14:56:13 -05:00
|
|
|
belongs_to :theme
|
2014-04-16 08:49:06 -05:00
|
|
|
|
2014-05-02 16:46:03 -05:00
|
|
|
validates_associated :color_scheme_colors
|
|
|
|
|
|
|
|
BASE_COLORS_FILE = "#{Rails.root}/app/assets/stylesheets/common/foundation/colors.scss"
|
2019-08-12 05:02:38 -05:00
|
|
|
COLOR_TRANSFORMATION_FILE = "#{Rails.root}/app/assets/stylesheets/common/foundation/color_transformations.scss"
|
2014-05-02 16:46:03 -05:00
|
|
|
|
|
|
|
@mutex = Mutex.new
|
|
|
|
|
|
|
|
def self.base_colors
|
2019-02-26 08:22:02 -06:00
|
|
|
return @base_colors if @base_colors
|
2014-05-02 16:46:03 -05:00
|
|
|
@mutex.synchronize do
|
|
|
|
return @base_colors if @base_colors
|
2019-02-26 08:22:02 -06:00
|
|
|
base_colors = {}
|
2016-09-02 10:42:14 -05:00
|
|
|
File.readlines(BASE_COLORS_FILE).each do |line|
|
2014-05-02 16:46:03 -05:00
|
|
|
matches = /\$([\w]+):\s*#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(?:[;]|\s)/.match(line.strip)
|
2019-02-26 08:22:02 -06:00
|
|
|
base_colors[matches[1]] = matches[2] if matches
|
2014-05-02 16:46:03 -05:00
|
|
|
end
|
2019-02-26 08:22:02 -06:00
|
|
|
@base_colors = base_colors
|
2014-05-02 16:46:03 -05:00
|
|
|
end
|
|
|
|
@base_colors
|
|
|
|
end
|
|
|
|
|
2019-08-12 05:02:38 -05:00
|
|
|
def self.color_transformation_variables
|
|
|
|
return @transformation_variables if @transformation_variables
|
|
|
|
@mutex.synchronize do
|
|
|
|
return @transformation_variables if @transformation_variables
|
|
|
|
transformation_variables = []
|
|
|
|
File.readlines(COLOR_TRANSFORMATION_FILE).each do |line|
|
|
|
|
matches = /\$([\w\-_]+):.*/.match(line.strip)
|
|
|
|
transformation_variables.append(matches[1]) if matches
|
|
|
|
end
|
|
|
|
@transformation_variables = transformation_variables
|
|
|
|
end
|
|
|
|
@transformation_variables
|
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def self.base_color_schemes
|
|
|
|
base_color_scheme_colors.map do |hash|
|
2018-07-24 08:00:20 -05:00
|
|
|
scheme = new(name: I18n.t("color_schemes.#{hash[:id].downcase.gsub(' ', '_')}"), base_scheme_id: hash[:id])
|
2017-04-12 09:52:52 -05:00
|
|
|
scheme.colors = hash[:colors].map { |k, v| { name: k.to_s, hex: v.sub("#", "") } }
|
|
|
|
scheme.is_base = true
|
|
|
|
scheme
|
|
|
|
end
|
2014-04-16 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
2014-05-02 16:46:03 -05:00
|
|
|
def self.base
|
2014-06-03 11:36:34 -05:00
|
|
|
return @base_color_scheme if @base_color_scheme
|
2017-04-12 09:52:52 -05:00
|
|
|
@base_color_scheme = new(name: I18n.t('color_schemes.base_theme_name'))
|
2014-06-03 11:36:34 -05:00
|
|
|
@base_color_scheme.colors = base_colors.map { |name, hex| { name: name, hex: hex } }
|
|
|
|
@base_color_scheme.is_base = true
|
|
|
|
@base_color_scheme
|
|
|
|
end
|
|
|
|
|
2019-10-25 07:29:51 -05:00
|
|
|
def self.is_base?(scheme_name)
|
|
|
|
base_color_scheme_colors.map { |c| c[:id] }.include?(scheme_name)
|
|
|
|
end
|
|
|
|
|
2014-06-03 11:36:34 -05:00
|
|
|
# create_from_base will create a new ColorScheme that overrides Discourse's base color scheme with the given colors.
|
|
|
|
def self.create_from_base(params)
|
|
|
|
new_color_scheme = new(name: params[:name])
|
2017-05-03 10:31:16 -05:00
|
|
|
new_color_scheme.via_wizard = true if params[:via_wizard]
|
|
|
|
new_color_scheme.base_scheme_id = params[:base_scheme_id]
|
|
|
|
|
|
|
|
colors = CUSTOM_SCHEMES[params[:base_scheme_id].to_sym]&.map do |name, hex|
|
|
|
|
{ name: name, hex: hex }
|
|
|
|
end if params[:base_scheme_id]
|
|
|
|
colors ||= base.colors_hashes
|
2014-06-03 11:36:34 -05:00
|
|
|
|
|
|
|
# Override base values
|
|
|
|
params[:colors].each do |name, hex|
|
|
|
|
c = colors.find { |x| x[:name].to_s == name.to_s }
|
|
|
|
c[:hex] = hex
|
2017-05-03 10:31:16 -05:00
|
|
|
end if params[:colors]
|
2014-06-03 11:36:34 -05:00
|
|
|
|
|
|
|
new_color_scheme.colors = colors
|
|
|
|
new_color_scheme.save
|
|
|
|
new_color_scheme
|
2014-04-16 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
2018-09-11 20:04:58 -05:00
|
|
|
def self.lookup_hex_for_name(name, scheme_id = nil)
|
|
|
|
enabled_color_scheme = find_by(id: scheme_id) if scheme_id
|
|
|
|
enabled_color_scheme ||= Theme.where(id: SiteSetting.default_theme_id).first&.color_scheme
|
2018-05-27 20:46:00 -05:00
|
|
|
(enabled_color_scheme || base).colors.find { |c| c.name == name }.try(:hex) || "nil"
|
2017-01-12 14:43:09 -06:00
|
|
|
end
|
|
|
|
|
2018-09-11 20:04:58 -05:00
|
|
|
def self.hex_for_name(name, scheme_id = nil)
|
|
|
|
cache_key = scheme_id ? name + "_#{scheme_id}" : name
|
|
|
|
hex_cache[cache_key] ||= lookup_hex_for_name(name, scheme_id)
|
|
|
|
hex_cache[cache_key] == "nil" ? nil : hex_cache[cache_key]
|
2014-07-17 11:31:37 -05:00
|
|
|
end
|
2014-05-02 16:46:03 -05:00
|
|
|
|
2014-04-16 08:49:06 -05:00
|
|
|
def colors=(arr)
|
|
|
|
@colors_by_name = nil
|
|
|
|
arr.each do |c|
|
2014-05-02 16:46:03 -05:00
|
|
|
self.color_scheme_colors << ColorSchemeColor.new(name: c[:name], hex: c[:hex])
|
2014-04-16 08:49:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def colors_by_name
|
|
|
|
@colors_by_name ||= self.colors.inject({}) { |sum, c| sum[c.name] = c; sum; }
|
|
|
|
end
|
2018-12-04 03:48:16 -06:00
|
|
|
|
2014-04-16 08:49:06 -05:00
|
|
|
def clear_colors_cache
|
|
|
|
@colors_by_name = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def colors_hashes
|
|
|
|
color_scheme_colors.map do |c|
|
2014-05-02 16:46:03 -05:00
|
|
|
{ name: c.name, hex: c.hex }
|
2014-04-16 08:49:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def base_colors
|
|
|
|
colors = nil
|
2018-07-24 08:00:20 -05:00
|
|
|
if base_scheme_id && base_scheme_id != "Light"
|
2017-04-12 09:52:52 -05:00
|
|
|
colors = CUSTOM_SCHEMES[base_scheme_id.to_sym]
|
|
|
|
end
|
|
|
|
colors || ColorScheme.base_colors
|
2014-04-16 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def resolved_colors
|
|
|
|
resolved = ColorScheme.base_colors.dup
|
2018-07-24 08:00:20 -05:00
|
|
|
if base_scheme_id && base_scheme_id != "Light"
|
2017-04-12 09:52:52 -05:00
|
|
|
if scheme = CUSTOM_SCHEMES[base_scheme_id.to_sym]
|
|
|
|
scheme.each do |name, value|
|
|
|
|
resolved[name] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
colors.each do |c|
|
|
|
|
resolved[c.name] = c.hex
|
|
|
|
end
|
|
|
|
resolved
|
2014-04-16 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
2014-11-10 22:32:44 -06:00
|
|
|
def publish_discourse_stylesheet
|
2017-04-12 09:52:52 -05:00
|
|
|
if self.id
|
2018-08-07 23:46:34 -05:00
|
|
|
theme_ids = Theme.where(color_scheme_id: self.id).pluck(:id)
|
|
|
|
if theme_ids.present?
|
2017-04-12 09:52:52 -05:00
|
|
|
Stylesheet::Manager.cache.clear
|
2018-08-07 23:46:34 -05:00
|
|
|
Theme.notify_theme_change(
|
|
|
|
theme_ids,
|
|
|
|
with_scheme: true,
|
|
|
|
clear_manager_cache: false,
|
|
|
|
all_themes: true
|
|
|
|
)
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
end
|
2014-11-10 22:32:44 -06:00
|
|
|
end
|
|
|
|
|
2015-09-23 01:47:17 -05:00
|
|
|
def dump_hex_cache
|
|
|
|
self.class.hex_cache.clear
|
|
|
|
end
|
|
|
|
|
2018-03-15 02:26:54 -05:00
|
|
|
def bump_version
|
|
|
|
if self.id
|
|
|
|
self.version += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-16 08:49:06 -05:00
|
|
|
end
|
2014-05-08 01:45:49 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: color_schemes
|
|
|
|
#
|
2017-04-12 09:52:52 -05:00
|
|
|
# id :integer not null, primary key
|
2019-01-11 13:29:56 -06:00
|
|
|
# name :string not null
|
2017-04-12 09:52:52 -05:00
|
|
|
# version :integer default(1), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# via_wizard :boolean default(FALSE), not null
|
|
|
|
# base_scheme_id :string
|
2017-04-20 14:47:25 -05:00
|
|
|
# theme_id :integer
|
2014-05-08 01:45:49 -05:00
|
|
|
#
|