From bbff8f4155a3fd1bb80ffda2aa8b982417cc9aa3 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Tue, 15 Jun 2021 17:04:36 +0400 Subject: [PATCH] Fix pattern theme preview --- .../Sources/FetchCachedRepresentations.swift | 19 +++++++++++++++---- .../Sources/WallpaperResources.swift | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/submodules/TelegramUI/Sources/FetchCachedRepresentations.swift b/submodules/TelegramUI/Sources/FetchCachedRepresentations.swift index 9280f95e5a..995c330a25 100644 --- a/submodules/TelegramUI/Sources/FetchCachedRepresentations.swift +++ b/submodules/TelegramUI/Sources/FetchCachedRepresentations.swift @@ -503,6 +503,8 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource, colorImage = generateImage(size, contextGenerator: { size, c in let rect = CGRect(origin: CGPoint(), size: size) c.setBlendMode(.copy) + + let averageBackgroundColor = UIColor.average(of: colors) if colors.count == 1, let color = colors.first { c.setFillColor(color.cgColor) @@ -511,11 +513,11 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource, let drawingRect = rect let image = GradientBackgroundNode.generatePreview(size: CGSize(width: 60.0, height: 60.0), colors: colors) c.translateBy(x: drawingRect.midX, y: drawingRect.midY) - c.scaleBy(x: 1.0, y: -1.0) + c.scaleBy(x: 1.0, y: 1.0) c.translateBy(x: -drawingRect.midX, y: -drawingRect.midY) c.draw(image.cgImage!, in: drawingRect) c.translateBy(x: drawingRect.midX, y: drawingRect.midY) - c.scaleBy(x: 1.0, y: -1.0) + c.scaleBy(x: 1.0, y: 1.0) c.translateBy(x: -drawingRect.midX, y: -drawingRect.midY) } else { let gradientColors = colors.map { $0.cgColor } as CFArray @@ -546,7 +548,16 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource, c.setBlendMode(.softLight) } - if colors.count == 1, let color = colors.first { + let isLight = averageBackgroundColor.hsb.b >= 0.3 + if isLight { + c.setFillColor(UIColor(white: 0.0, alpha: abs(intensity)).cgColor) + c.fill(rect) + } else { + c.setFillColor(UIColor(white: 1.0, alpha: abs(intensity)).cgColor) + c.fill(rect) + } + + /*if colors.count == 1, let color = colors.first { c.setFillColor(patternColor(for: color, intensity: intensity).cgColor) c.fill(rect) } else { @@ -565,7 +576,7 @@ private func fetchCachedPatternWallpaperRepresentation(resource: MediaResource, c.translateBy(x: -rect.width / 2.0, y: -rect.height / 2.0) c.drawLinearGradient(gradient, start: CGPoint(x: 0.0, y: 0.0), end: CGPoint(x: 0.0, y: rect.height), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation]) - } + }*/ }, scale: 1.0) } diff --git a/submodules/WallpaperResources/Sources/WallpaperResources.swift b/submodules/WallpaperResources/Sources/WallpaperResources.swift index f39da26c29..4aa60389fd 100644 --- a/submodules/WallpaperResources/Sources/WallpaperResources.swift +++ b/submodules/WallpaperResources/Sources/WallpaperResources.swift @@ -1077,7 +1077,7 @@ public func themeImage(account: Account, accountManager: AccountManager, source: if wallpaper.wallpaper.isPattern, !file.settings.colors.isEmpty, let intensity = file.settings.intensity { return accountManager.mediaBox.cachedResourceRepresentation(file.file.resource, representation: CachedPatternWallpaperRepresentation(colors: file.settings.colors, intensity: intensity, rotation: file.settings.rotation), complete: true, fetch: true) |> mapToSignal { data in - if data.complete, let data = try? Data(contentsOf: URL(fileURLWithPath: data.path)), let image = UIImage(data: data) { + if data.complete, let imageData = try? Data(contentsOf: URL(fileURLWithPath: data.path)), let image = UIImage(data: imageData) { return .single((theme, image, thumbnailData)) } else { return .complete()