Skip to content

Commit

Permalink
Use generated filters for SVG color
Browse files Browse the repository at this point in the history
  • Loading branch information
livid committed Sep 10, 2024
1 parent 18d52e1 commit 8f571de
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
19 changes: 19 additions & 0 deletions Planet/Entities/MyPlanetModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion Planet/TemplateBrowser/ColorFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))%)"
}
}
8 changes: 4 additions & 4 deletions Planet/TemplateBrowser/Template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8f571de

Please sign in to comment.