From 63dd5d63c16744cfab77cbb329efb57ed1659da7 Mon Sep 17 00:00:00 2001 From: Igor Kulman Date: Mon, 7 Nov 2022 10:29:36 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BPotential=20fix=20for=20resizing=20?= =?UTF-8?q?artifacts=20generated=20by=20wrong=20image=20size=20reporting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ChangeMenuBarColor/Commands/Abstract/Command.swift | 1 + .../Extensions/NSImage+Extensions.swift | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift b/Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift index c801e49..4491cb1 100644 --- a/Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift +++ b/Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift @@ -57,6 +57,7 @@ class Command { return nil } + wallpaper.adjustSize() Log.debug("Using currently set macOS wallpaper \(path)") return wallpaper diff --git a/Sources/ChangeMenuBarColor/Extensions/NSImage+Extensions.swift b/Sources/ChangeMenuBarColor/Extensions/NSImage+Extensions.swift index ec0afbb..bea248c 100644 --- a/Sources/ChangeMenuBarColor/Extensions/NSImage+Extensions.swift +++ b/Sources/ChangeMenuBarColor/Extensions/NSImage+Extensions.swift @@ -110,4 +110,13 @@ extension NSImage { // Return nil in case anything fails. return nil } + + // Images loaded from file sometimes do not report the size correctly, see https://stackoverflow.com/questions/9264051/nsimage-size-not-real-size-with-some-pictures + // This can lead to artifcats produces bby resizing operations + func adjustSize() { + // use the biggest sizes from all the representations https://stackoverflow.com/a/38523158/581164 + size = representations.reduce(size) { size, representation in + return CGSize(width: max(size.width, CGFloat(representation.pixelsWide)), height: max(size.height, CGFloat(representation.pixelsHigh))) + } + } }