2018-05-27 21:46:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
class ColorScheme < ActiveRecord::Base
|
2019-12-09 19:48:27 -05:00
|
|
|
# rubocop:disable Layout/HashAlignment
|
2018-12-04 04:48:16 -05:00
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
CUSTOM_SCHEMES = {
|
2018-07-24 09:00:20 -04:00
|
|
|
Dark: {
|
2017-04-12 10:52:52 -04:00
|
|
|
"primary" => "dddddd",
|
|
|
|
"secondary" => "222222",
|
2023-06-14 12:14:53 -04:00
|
|
|
"tertiary" => "099dd7",
|
2017-04-12 10:52:52 -04:00
|
|
|
"quaternary" => "c14924",
|
|
|
|
"header_background" => "111111",
|
2017-10-03 13:01:05 -04:00
|
|
|
"header_primary" => "dddddd",
|
2017-04-12 10:52:52 -04:00
|
|
|
"highlight" => "a87137",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "2c2c2c",
|
|
|
|
"hover" => "313131",
|
2017-04-12 10:52:52 -04:00
|
|
|
"danger" => "e45735",
|
|
|
|
"success" => "1ca551",
|
|
|
|
"love" => "fa6c8d",
|
2018-07-24 09:00:20 -04:00
|
|
|
},
|
|
|
|
# By @itsbhanusharma
|
|
|
|
Neutral: {
|
|
|
|
"primary" => "000000",
|
|
|
|
"secondary" => "ffffff",
|
|
|
|
"tertiary" => "51839b",
|
|
|
|
"quaternary" => "b85e48",
|
|
|
|
"header_background" => "333333",
|
|
|
|
"header_primary" => "f3f3f3",
|
|
|
|
"highlight" => "ecec70",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "e6e6e6",
|
|
|
|
"hover" => "f0f0f0",
|
2018-07-24 09:00:20 -04:00
|
|
|
"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",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "272727",
|
|
|
|
"hover" => "2F2F30",
|
2018-07-24 09:00:20 -04:00
|
|
|
"danger" => "e45735",
|
|
|
|
"success" => "fdd459",
|
|
|
|
"love" => "fdd459",
|
|
|
|
},
|
2018-08-08 16:01:21 -04:00
|
|
|
# By @rafafotes
|
2018-07-24 09:00:20 -04:00
|
|
|
"Shades of Blue": {
|
|
|
|
"primary" => "203243",
|
|
|
|
"secondary" => "eef4f7",
|
|
|
|
"tertiary" => "416376",
|
|
|
|
"quaternary" => "5e99b9",
|
|
|
|
"header_background" => "86bddb",
|
2020-12-02 10:22:30 -05:00
|
|
|
"header_primary" => "203243",
|
2018-07-24 09:00:20 -04:00
|
|
|
"highlight" => "86bddb",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "bee0f2",
|
|
|
|
"hover" => "d2efff",
|
2018-07-24 09:00:20 -04:00
|
|
|
"danger" => "bf3c3c",
|
|
|
|
"success" => "70db82",
|
|
|
|
"love" => "fc94cb",
|
|
|
|
},
|
|
|
|
# By @mikechristopher
|
|
|
|
Latte: {
|
2018-07-25 15:12:01 -04:00
|
|
|
"primary" => "f2e5d7",
|
2018-07-24 09:00:20 -04:00
|
|
|
"secondary" => "262322",
|
|
|
|
"tertiary" => "f7f2ed",
|
|
|
|
"quaternary" => "d7c9aa",
|
|
|
|
"header_background" => "d7c9aa",
|
|
|
|
"header_primary" => "262322",
|
|
|
|
"highlight" => "d7c9aa",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "3e2a14",
|
|
|
|
"hover" => "4c3319",
|
2018-07-24 09:00:20 -04:00
|
|
|
"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",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "f5eaea",
|
|
|
|
"hover" => "f9f3f3",
|
2018-07-24 09:00:20 -04:00
|
|
|
"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",
|
2023-01-27 09:50:36 -05:00
|
|
|
"highlight" => "bd36a3",
|
|
|
|
"selected" => "2a1620",
|
|
|
|
"hover" => "331b27",
|
2018-07-24 09:00:20 -04:00
|
|
|
"danger" => "6c3e63",
|
|
|
|
"success" => "d9b2bb",
|
|
|
|
"love" => "d9b2bb",
|
2020-10-15 14:05:48 -04:00
|
|
|
},
|
|
|
|
WCAG: {
|
|
|
|
"primary" => "000000",
|
|
|
|
"primary-medium" => "696969",
|
|
|
|
"primary-low-mid" => "909090",
|
|
|
|
"secondary" => "ffffff",
|
|
|
|
"tertiary" => "3369FF",
|
|
|
|
"quaternary" => "3369FF",
|
|
|
|
"header_background" => "ffffff",
|
|
|
|
"header_primary" => "000000",
|
2023-01-27 09:50:36 -05:00
|
|
|
"highlight" => "ffff00",
|
2020-10-15 14:05:48 -04:00
|
|
|
"highlight-high" => "0036E6",
|
|
|
|
"highlight-medium" => "e0e9ff",
|
|
|
|
"highlight-low" => "e0e9ff",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "E2E9FE",
|
|
|
|
"hover" => "F0F4FE",
|
2020-10-15 14:05:48 -04:00
|
|
|
"danger" => "BB1122",
|
|
|
|
"success" => "3d854d",
|
|
|
|
"love" => "9D256B",
|
|
|
|
},
|
|
|
|
"WCAG Dark": {
|
|
|
|
"primary" => "ffffff",
|
|
|
|
"primary-medium" => "999999",
|
|
|
|
"primary-low-mid" => "888888",
|
|
|
|
"secondary" => "0c0c0c",
|
|
|
|
"tertiary" => "759AFF",
|
|
|
|
"quaternary" => "759AFF",
|
|
|
|
"header_background" => "000000",
|
|
|
|
"header_primary" => "ffffff",
|
|
|
|
"highlight" => "3369FF",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "0d2569",
|
|
|
|
"hover" => "002382",
|
2020-10-15 14:05:48 -04:00
|
|
|
"danger" => "BB1122",
|
|
|
|
"success" => "3d854d",
|
|
|
|
"love" => "9D256B",
|
2022-07-08 17:46:32 -04:00
|
|
|
},
|
|
|
|
# By @zenorocha
|
|
|
|
Dracula: {
|
|
|
|
"primary_very_low" => "373A47",
|
|
|
|
"primary_low" => "414350",
|
|
|
|
"primary_low_mid" => "8C8D94",
|
|
|
|
"primary_medium" => "A3A4AA",
|
|
|
|
"primary_high" => "CCCCCF",
|
|
|
|
"primary" => "f2f2f2",
|
2022-11-18 14:28:13 -05:00
|
|
|
"primary-50" => "3F414E",
|
|
|
|
"primary-100" => "535460",
|
|
|
|
"primary-200" => "666972",
|
|
|
|
"primary-300" => "7A7C84",
|
|
|
|
"primary-400" => "8D8F96",
|
|
|
|
"primary-500" => "A2A3A9",
|
|
|
|
"primary-600" => "B6B7BC",
|
|
|
|
"primary-700" => "C7C7C7",
|
|
|
|
"primary-800" => "DEDFE0",
|
|
|
|
"primary-900" => "F5F5F5",
|
2022-07-08 17:46:32 -04:00
|
|
|
"secondary_low" => "CCCCCF",
|
|
|
|
"secondary_medium" => "91939A",
|
|
|
|
"secondary_high" => "6A6C76",
|
|
|
|
"secondary_very_high" => "3D404C",
|
|
|
|
"secondary" => "2d303e",
|
|
|
|
"tertiary_low" => "4A4463",
|
|
|
|
"tertiary_medium" => "6E5D92",
|
|
|
|
"tertiary" => "bd93f9",
|
|
|
|
"tertiary_high" => "9275C1",
|
|
|
|
"quaternary_low" => "6AA8BA",
|
|
|
|
"quaternary" => "8be9fd",
|
|
|
|
"header_background" => "373A47",
|
|
|
|
"header_primary" => "f2f2f2",
|
|
|
|
"highlight_low" => "686D55",
|
2022-11-18 14:28:13 -05:00
|
|
|
"highlight_medium" => "52592B",
|
2022-07-08 17:46:32 -04:00
|
|
|
"highlight_high" => "C0C879",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "4A4463",
|
|
|
|
"hover" => "61597f",
|
2022-07-08 17:46:32 -04:00
|
|
|
"danger_low" => "957279",
|
|
|
|
"danger" => "ff5555",
|
|
|
|
"success_low" => "386D50",
|
|
|
|
"success_medium" => "44B366",
|
|
|
|
"success" => "50fa7b",
|
|
|
|
"love_low" => "6C4667",
|
|
|
|
"love" => "ff79c6",
|
|
|
|
},
|
|
|
|
# By @altercation
|
|
|
|
"Solarized Light": {
|
|
|
|
"primary_very_low" => "F0ECD7",
|
|
|
|
"primary_low" => "D6D8C7",
|
|
|
|
"primary_low_mid" => "A4AFA5",
|
|
|
|
"primary_medium" => "7E918C",
|
|
|
|
"primary_high" => "4C6869",
|
|
|
|
"primary" => "002B36",
|
2022-11-16 12:33:23 -05:00
|
|
|
"primary-50" => "F0EBDA",
|
|
|
|
"primary-100" => "DAD8CA",
|
|
|
|
"primary-200" => "B2B9B3",
|
|
|
|
"primary-300" => "839496",
|
|
|
|
"primary-400" => "76898C",
|
|
|
|
"primary-500" => "697F83",
|
|
|
|
"primary-600" => "627A7E",
|
|
|
|
"primary-700" => "556F74",
|
|
|
|
"primary-800" => "415F66",
|
|
|
|
"primary-900" => "21454E",
|
2022-07-08 17:46:32 -04:00
|
|
|
"secondary_low" => "325458",
|
|
|
|
"secondary_medium" => "6C8280",
|
|
|
|
"secondary_high" => "97A59D",
|
|
|
|
"secondary_very_high" => "E8E6D3",
|
|
|
|
"secondary" => "FCF6E1",
|
|
|
|
"tertiary_low" => "D6E6DE",
|
|
|
|
"tertiary_medium" => "7EBFD7",
|
|
|
|
"tertiary" => "0088cc",
|
|
|
|
"tertiary_high" => "329ED0",
|
|
|
|
"quaternary" => "e45735",
|
|
|
|
"header_background" => "FCF6E1",
|
|
|
|
"header_primary" => "002B36",
|
|
|
|
"highlight_low" => "FDF9AD",
|
|
|
|
"highlight_medium" => "E3D0A3",
|
2022-11-18 14:28:13 -05:00
|
|
|
"highlight" => "F2F481",
|
2022-07-08 17:46:32 -04:00
|
|
|
"highlight_high" => "BCAA7F",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "E8E6D3",
|
|
|
|
"hover" => "F0EBDA",
|
2022-07-08 17:46:32 -04:00
|
|
|
"danger_low" => "F8D9C2",
|
|
|
|
"danger" => "e45735",
|
|
|
|
"success_low" => "CFE5B9",
|
|
|
|
"success_medium" => "4CB544",
|
|
|
|
"success" => "009900",
|
|
|
|
"love_low" => "FCDDD2",
|
|
|
|
"love" => "fa6c8d",
|
|
|
|
},
|
|
|
|
# By @altercation
|
|
|
|
"Solarized Dark": {
|
|
|
|
"primary_very_low" => "0D353F",
|
|
|
|
"primary_low" => "193F47",
|
|
|
|
"primary_low_mid" => "798C88",
|
|
|
|
"primary_medium" => "97A59D",
|
|
|
|
"primary_high" => "B5BDB1",
|
|
|
|
"primary" => "FCF6E1",
|
2022-11-16 12:33:23 -05:00
|
|
|
"primary-50" => "21454E",
|
|
|
|
"primary-100" => "415F66",
|
|
|
|
"primary-200" => "556F74",
|
|
|
|
"primary-300" => "627A7E",
|
|
|
|
"primary-400" => "697F83",
|
|
|
|
"primary-500" => "76898C",
|
|
|
|
"primary-600" => "839496",
|
|
|
|
"primary-700" => "B2B9B3",
|
|
|
|
"primary-800" => "DAD8CA",
|
|
|
|
"primary-900" => "F0EBDA",
|
2022-07-08 17:46:32 -04:00
|
|
|
"secondary_low" => "B5BDB1",
|
|
|
|
"secondary_medium" => "81938D",
|
|
|
|
"secondary_high" => "4E6A6B",
|
|
|
|
"secondary_very_high" => "143B44",
|
|
|
|
"secondary" => "002B36",
|
|
|
|
"tertiary_low" => "003E54",
|
|
|
|
"tertiary_medium" => "00557A",
|
2023-06-14 12:14:53 -04:00
|
|
|
"tertiary" => "1a97d5",
|
2022-07-08 17:46:32 -04:00
|
|
|
"tertiary_high" => "006C9F",
|
|
|
|
"quaternary_low" => "944835",
|
|
|
|
"quaternary" => "e45735",
|
|
|
|
"header_background" => "002B36",
|
|
|
|
"header_primary" => "FCF6E1",
|
|
|
|
"highlight_low" => "4D6B3D",
|
|
|
|
"highlight_medium" => "464C33",
|
2022-11-18 14:28:13 -05:00
|
|
|
"highlight" => "F2F481",
|
2022-07-08 17:46:32 -04:00
|
|
|
"highlight_high" => "BFCA47",
|
2023-01-27 09:50:36 -05:00
|
|
|
"selected" => "143B44",
|
|
|
|
"hover" => "21454E",
|
2022-07-08 17:46:32 -04:00
|
|
|
"danger_low" => "443836",
|
|
|
|
"danger_medium" => "944835",
|
|
|
|
"danger" => "e45735",
|
|
|
|
"success_low" => "004C26",
|
|
|
|
"success_medium" => "007313",
|
|
|
|
"success" => "009900",
|
|
|
|
"love_low" => "4B3F50",
|
|
|
|
"love" => "fa6c8d",
|
2017-04-12 10:52:52 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:48:27 -05:00
|
|
|
# rubocop:enable Layout/HashAlignment
|
2018-12-04 04:48:16 -05:00
|
|
|
|
2019-05-09 05:22:28 -04:00
|
|
|
LIGHT_THEME_ID = "Light"
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def self.base_color_scheme_colors
|
2021-08-25 17:10:12 -04:00
|
|
|
base_with_hash = []
|
2018-12-04 04:48:16 -05:00
|
|
|
|
2021-08-25 17:10:12 -04:00
|
|
|
base_colors.each { |name, color| base_with_hash << { name: name, hex: "#{color}" } }
|
2016-09-02 11:42:14 -04:00
|
|
|
|
2019-05-09 05:22:28 -04:00
|
|
|
list = [{ id: LIGHT_THEME_ID, colors: base_with_hash }]
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
CUSTOM_SCHEMES.each do |k, v|
|
2021-08-25 17:10:12 -04:00
|
|
|
colors = []
|
|
|
|
v.each { |name, color| colors << { name: name, hex: "#{color}" } }
|
|
|
|
list.push(id: k.to_s, colors: colors)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
2018-12-04 04:48:16 -05:00
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
list
|
2016-09-02 11:42:14 -04:00
|
|
|
end
|
|
|
|
|
2015-09-23 02:47:17 -04:00
|
|
|
def self.hex_cache
|
|
|
|
@hex_cache ||= DistributedCache.new("scheme_hex_for_name")
|
|
|
|
end
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
attr_accessor :is_base
|
2021-05-06 14:26:58 -04:00
|
|
|
attr_accessor :skip_publish
|
2014-05-02 17:46:03 -04:00
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
has_many :color_scheme_colors, -> { order("id ASC") }, dependent: :destroy
|
|
|
|
|
|
|
|
alias_method :colors, :color_scheme_colors
|
|
|
|
|
2018-03-15 03:26:54 -04:00
|
|
|
before_save :bump_version
|
2021-05-06 14:26:58 -04:00
|
|
|
after_save_commit :publish_discourse_stylesheet, unless: :skip_publish
|
|
|
|
after_save_commit :dump_caches
|
2020-08-28 10:36:52 -04:00
|
|
|
after_destroy :dump_caches
|
2017-04-17 15:56:13 -04:00
|
|
|
belongs_to :theme
|
2014-04-16 09:49:06 -04:00
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
validates_associated :color_scheme_colors
|
|
|
|
|
|
|
|
BASE_COLORS_FILE = "#{Rails.root}/app/assets/stylesheets/common/foundation/colors.scss"
|
2019-08-12 06:02:38 -04:00
|
|
|
COLOR_TRANSFORMATION_FILE =
|
|
|
|
"#{Rails.root}/app/assets/stylesheets/common/foundation/color_transformations.scss"
|
2014-05-02 17:46:03 -04:00
|
|
|
|
|
|
|
@mutex = Mutex.new
|
|
|
|
|
|
|
|
def self.base_colors
|
2019-02-26 09:22:02 -05:00
|
|
|
return @base_colors if @base_colors
|
2014-05-02 17:46:03 -04:00
|
|
|
@mutex.synchronize do
|
|
|
|
return @base_colors if @base_colors
|
2019-02-26 09:22:02 -05:00
|
|
|
base_colors = {}
|
2016-09-02 11:42:14 -04:00
|
|
|
File
|
|
|
|
.readlines(BASE_COLORS_FILE)
|
|
|
|
.each do |line|
|
2014-05-02 17:46:03 -04:00
|
|
|
matches = /\$([\w]+):\s*#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(?:[;]|\s)/.match(line.strip)
|
2019-02-26 09:22:02 -05:00
|
|
|
base_colors[matches[1]] = matches[2] if matches
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
2019-02-26 09:22:02 -05:00
|
|
|
@base_colors = base_colors
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
|
|
|
@base_colors
|
|
|
|
end
|
|
|
|
|
2019-08-12 06:02:38 -04: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 10:52:52 -04:00
|
|
|
def self.base_color_schemes
|
|
|
|
base_color_scheme_colors.map do |hash|
|
2018-07-24 09:00:20 -04:00
|
|
|
scheme =
|
|
|
|
new(
|
|
|
|
name: I18n.t("color_schemes.#{hash[:id].downcase.gsub(" ", "_")}"),
|
|
|
|
base_scheme_id: hash[:id],
|
|
|
|
)
|
2021-08-25 17:10:12 -04:00
|
|
|
scheme.colors = hash[:colors].map { |k| { name: k[:name], hex: k[:hex] } }
|
2017-04-12 10:52:52 -04:00
|
|
|
scheme.is_base = true
|
|
|
|
scheme
|
|
|
|
end
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
def self.base
|
2014-06-03 12:36:34 -04:00
|
|
|
return @base_color_scheme if @base_color_scheme
|
2017-04-12 10:52:52 -04:00
|
|
|
@base_color_scheme = new(name: I18n.t("color_schemes.base_theme_name"))
|
2014-06-03 12:36:34 -04: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 08:29:51 -04:00
|
|
|
def self.is_base?(scheme_name)
|
|
|
|
base_color_scheme_colors.map { |c| c[:id] }.include?(scheme_name)
|
|
|
|
end
|
|
|
|
|
2014-06-03 12:36:34 -04: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 11:31:16 -04:00
|
|
|
new_color_scheme.via_wizard = true if params[:via_wizard]
|
|
|
|
new_color_scheme.base_scheme_id = params[:base_scheme_id]
|
2022-07-08 17:46:32 -04:00
|
|
|
new_color_scheme.user_selectable = true
|
2017-05-03 11:31:16 -04:00
|
|
|
|
|
|
|
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 12:36:34 -04: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 11:31:16 -04:00
|
|
|
end if params[:colors]
|
2014-06-03 12:36:34 -04:00
|
|
|
|
|
|
|
new_color_scheme.colors = colors
|
2021-05-06 14:26:58 -04:00
|
|
|
new_color_scheme.skip_publish if params[:skip_publish]
|
2014-06-03 12:36:34 -04:00
|
|
|
new_color_scheme.save
|
|
|
|
new_color_scheme
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2018-09-11 21:04:58 -04: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
|
2023-03-27 10:22:53 -04:00
|
|
|
(enabled_color_scheme || base).colors.find { |c| c.name == name }.try(:hex)
|
2017-01-12 15:43:09 -05:00
|
|
|
end
|
|
|
|
|
2018-09-11 21:04:58 -04:00
|
|
|
def self.hex_for_name(name, scheme_id = nil)
|
2023-03-27 10:22:53 -04:00
|
|
|
hex_cache.defer_get_set(scheme_id ? name + "_#{scheme_id}" : name) do
|
|
|
|
lookup_hex_for_name(name, scheme_id)
|
|
|
|
end
|
2014-07-17 12:31:37 -04:00
|
|
|
end
|
2014-05-02 17:46:03 -04:00
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
def colors=(arr)
|
|
|
|
@colors_by_name = nil
|
2014-05-02 17:46:03 -04:00
|
|
|
arr.each { |c| self.color_scheme_colors << ColorSchemeColor.new(name: c[:name], hex: c[:hex]) }
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def colors_by_name
|
|
|
|
@colors_by_name ||=
|
|
|
|
self
|
|
|
|
.colors
|
|
|
|
.inject({}) do |sum, c|
|
|
|
|
sum[c.name] = c
|
|
|
|
sum
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
2018-12-04 04:48:16 -05:00
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
def clear_colors_cache
|
|
|
|
@colors_by_name = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def colors_hashes
|
2014-05-02 17:46:03 -04:00
|
|
|
color_scheme_colors.map { |c| { name: c.name, hex: c.hex } }
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def base_colors
|
|
|
|
colors = nil
|
|
|
|
colors = CUSTOM_SCHEMES[base_scheme_id.to_sym] if base_scheme_id && base_scheme_id != "Light"
|
|
|
|
colors || ColorScheme.base_colors
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def resolved_colors
|
2023-02-06 13:24:12 -05:00
|
|
|
from_base = ColorScheme.base_colors
|
|
|
|
from_custom_scheme = base_colors
|
2023-02-01 04:55:21 -05:00
|
|
|
from_db = colors.map { |c| [c.name, c.hex] }.to_h
|
|
|
|
|
2023-02-06 13:24:12 -05:00
|
|
|
resolved = from_base.merge(from_custom_scheme).except("hover", "selected").merge(from_db)
|
2023-02-01 04:55:21 -05:00
|
|
|
|
|
|
|
# Equivalent to primary-100 in light mode, or primary-low in dark mode
|
|
|
|
resolved["hover"] ||= ColorMath.dark_light_diff(
|
|
|
|
resolved["primary"],
|
|
|
|
resolved["secondary"],
|
|
|
|
0.94,
|
|
|
|
-0.78,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Equivalent to primary-low in light mode, or primary-100 in dark mode
|
|
|
|
resolved["selected"] ||= ColorMath.dark_light_diff(
|
|
|
|
resolved["primary"],
|
|
|
|
resolved["secondary"],
|
|
|
|
0.9,
|
|
|
|
-0.8,
|
|
|
|
)
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
resolved
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2014-11-10 23:32:44 -05:00
|
|
|
def publish_discourse_stylesheet
|
2021-05-06 14:26:58 -04:00
|
|
|
self.class.publish_discourse_stylesheets!(self.id) if self.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.publish_discourse_stylesheets!(id = nil)
|
|
|
|
Stylesheet::Manager.clear_color_scheme_cache!
|
|
|
|
|
|
|
|
theme_ids = []
|
|
|
|
if id
|
|
|
|
theme_ids = Theme.where(color_scheme_id: id).pluck(:id)
|
|
|
|
else
|
|
|
|
theme_ids = Theme.all.pluck(:id)
|
|
|
|
end
|
|
|
|
if theme_ids.present?
|
|
|
|
Stylesheet::Manager.cache.clear
|
2021-06-15 02:57:17 -04:00
|
|
|
|
2021-05-06 14:26:58 -04:00
|
|
|
Theme.notify_theme_change(
|
|
|
|
theme_ids,
|
|
|
|
with_scheme: true,
|
|
|
|
clear_manager_cache: false,
|
|
|
|
all_themes: true,
|
|
|
|
)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
2014-11-10 23:32:44 -05:00
|
|
|
end
|
|
|
|
|
2020-08-28 10:36:52 -04:00
|
|
|
def dump_caches
|
2015-09-23 02:47:17 -04:00
|
|
|
self.class.hex_cache.clear
|
2020-08-28 10:36:52 -04:00
|
|
|
ApplicationSerializer.expire_cache_fragment!("user_color_schemes")
|
2015-09-23 02:47:17 -04:00
|
|
|
end
|
|
|
|
|
2018-03-15 03:26:54 -04:00
|
|
|
def bump_version
|
|
|
|
self.version += 1 if self.id
|
|
|
|
end
|
|
|
|
|
2020-08-20 14:23:18 -04:00
|
|
|
def is_dark?
|
2021-10-28 21:03:22 -04:00
|
|
|
return if colors.to_a.empty?
|
2020-08-28 10:36:52 -04:00
|
|
|
|
2023-02-01 04:55:21 -05:00
|
|
|
primary_b = ColorMath.brightness(resolved_colors["primary"])
|
|
|
|
secondary_b = ColorMath.brightness(resolved_colors["secondary"])
|
2020-08-20 14:23:18 -04:00
|
|
|
|
|
|
|
primary_b > secondary_b
|
|
|
|
end
|
|
|
|
|
2020-10-15 14:05:48 -04:00
|
|
|
def is_wcag?
|
|
|
|
base_scheme_id&.start_with?("WCAG")
|
|
|
|
end
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
2014-05-08 02:45:49 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: color_schemes
|
|
|
|
#
|
2020-09-01 04:00:36 -04:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string not null
|
|
|
|
# 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
|
|
|
|
# theme_id :integer
|
|
|
|
# user_selectable :boolean default(FALSE), not null
|
2014-05-08 02:45:49 -04:00
|
|
|
#
|