diff --git a/Planet/Entities/MyPlanetModel.swift b/Planet/Entities/MyPlanetModel.swift index 908e3373..42264550 100644 --- a/Planet/Entities/MyPlanetModel.swift +++ b/Planet/Entities/MyPlanetModel.swift @@ -271,6 +271,25 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl } return [:] } + func templateSettingsAndFilters() -> [String: String] { + if let data = try? Data(contentsOf: templateSettingsPath) { + if let json = try? JSONSerialization.jsonObject(with: data, options: []) { + if var dict = json as? [String: String] { + for (key, value) in dict { + if key.hasSuffix("Color") { + let targetColor = CustomColor(hex: value) + let baseColor = CustomColor(hex: "#000000") + let solver = Solver(target: targetColor, baseColor: baseColor) + let result = solver.solve() + dict[key + "Filter"] = result.filter + } + } + return dict + } + } + } + return [:] + } func updateTemplateSettings(settings: [String: String]) { do { // Read current settings diff --git a/Planet/TemplateBrowser/ColorFilter.swift b/Planet/TemplateBrowser/ColorFilter.swift index bf7a389b..e40dae6c 100644 --- a/Planet/TemplateBrowser/ColorFilter.swift +++ b/Planet/TemplateBrowser/ColorFilter.swift @@ -14,6 +14,22 @@ class CustomColor { var g: Double var b: Double + init(hex: String) { + let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted) + if hex.count == 6 { + var rgb: UInt64 = 0 + Scanner(string: hex).scanHexInt64(&rgb) + self.r = Double((rgb & 0xFF0000) >> 16) + self.g = Double((rgb & 0x00FF00) >> 8) + self.b = Double(rgb & 0x0000FF) + } + else { + self.r = 0 + self.g = 0 + self.b = 0 + } + } + init(r: Double, g: Double, b: Double) { self.r = 0 self.g = 0 @@ -282,6 +298,6 @@ class Solver { return "\(round(filters[idx] * multiplier))" } return - "filter: invert(\(fmt(idx: 0))%) sepia(\(fmt(idx: 1))%) saturate(\(fmt(idx: 2))%) hue-rotate(\(fmt(idx: 3, multiplier: 3.6))deg) brightness(\(fmt(idx: 4))%) contrast(\(fmt(idx: 5))%);" + "invert(\(fmt(idx: 0))%) sepia(\(fmt(idx: 1))%) saturate(\(fmt(idx: 2))%) hue-rotate(\(fmt(idx: 3, multiplier: 3.6))deg) brightness(\(fmt(idx: 4))%) contrast(\(fmt(idx: 5))%)" } } diff --git a/Planet/TemplateBrowser/Template.swift b/Planet/TemplateBrowser/Template.swift index 09cc0c75..cb58c71c 100644 --- a/Planet/TemplateBrowser/Template.swift +++ b/Planet/TemplateBrowser/Template.swift @@ -233,7 +233,7 @@ class Template: Codable, Identifiable { "planet_ipns": article.planet.ipns, "assets_prefix": "../", "template_settings": self.settings, - "user_settings": planet.templateSettings(), + "user_settings": planet.templateSettingsAndFilters(), "article_id": article.id.uuidString, "article": article.publicArticle, "article_type": article.articleType?.rawValue ?? 0, @@ -285,7 +285,7 @@ class Template: Codable, Identifiable { var contextForRendering: [String: Any] = [ "assets_prefix": "./", "template_settings": self.settings, - "user_settings": myPlanet.templateSettings(), + "user_settings": myPlanet.templateSettingsAndFilters(), "page_title": planet.name, "page_description": planet.about, "page_description_html": pageAboutHTML, @@ -320,7 +320,7 @@ class Template: Codable, Identifiable { var contextForRendering: [String: Any] = [ "assets_prefix": "./", "template_settings": self.settings, - "user_settings": myPlanet.templateSettings(), + "user_settings": myPlanet.templateSettingsAndFilters(), "page_title": "\(planet.name) - Tags", "page_description": planet.about, "page_description_html": pageAboutHTML, @@ -350,7 +350,7 @@ class Template: Codable, Identifiable { var contextForRendering: [String: Any] = [ "assets_prefix": "./", "template_settings": self.settings, - "user_settings": myPlanet.templateSettings(), + "user_settings": myPlanet.templateSettingsAndFilters(), "page_title": "\(planet.name) - Archive", "page_description": planet.about, "page_description_html": pageAboutHTML,