From 731925998fb219d74f387d23eb8792c5113f2bf6 Mon Sep 17 00:00:00 2001 From: Christoffer Winterkvist Date: Tue, 8 Nov 2016 15:54:54 +0100 Subject: [PATCH 1/3] Fix unwrapping crash --- Source/Mac/NSImage+Hue.swift | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Source/Mac/NSImage+Hue.swift b/Source/Mac/NSImage+Hue.swift index 966a53a..743e03c 100644 --- a/Source/Mac/NSImage+Hue.swift +++ b/Source/Mac/NSImage+Hue.swift @@ -24,20 +24,25 @@ extension NSImage { } public func colors(_ scaleDownSize: CGSize? = nil) -> (background: NSColor, primary: NSColor, secondary: NSColor, detail: NSColor) { - let cgImage: CGImage + let cgImage: CGImage? if let scaleDownSize = scaleDownSize { let context = NSGraphicsContext.current() - cgImage = resize(scaleDownSize).cgImage(forProposedRect: nil, context: context, hints: nil)! + cgImage = resize(scaleDownSize).cgImage(forProposedRect: nil, context: context, hints: nil) } else { let context = NSGraphicsContext.current() let ratio = size.width / size.height let r_width: CGFloat = 250 - cgImage = resize(CGSize(width: r_width, height: r_width / ratio)).cgImage(forProposedRect: nil, context: context, hints: nil)! + cgImage = resize(CGSize(width: r_width, height: r_width / ratio)).cgImage(forProposedRect: nil, context: context, hints: nil) } - let width = cgImage.width - let height = cgImage.height + guard let resolvedImage = cgImage else { return (background: NSColor.clear, + primary: NSColor.clear, + secondary: NSColor.clear, + detail: NSColor.clear) } + + let width = resolvedImage.width + let height = resolvedImage.height let bytesPerPixel = 4 let bytesPerRow = width * bytesPerPixel let bitsPerComponent = 8 @@ -48,7 +53,7 @@ extension NSImage { let raw = malloc(bytesPerRow * height) let bitmapInfo = CGImageAlphaInfo.premultipliedFirst.rawValue let context = CGContext(data: raw, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) - context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height))) + context?.draw(resolvedImage, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height))) let rawData = context?.data?.assumingMemoryBound(to: UInt8.self) let imageBackgroundColors = NSCountedSet(capacity: height) let imageColors = NSCountedSet(capacity: width * height) From c8933abff56c58eed26afebb606bf604314d5986 Mon Sep 17 00:00:00 2001 From: Christoffer Winterkvist Date: Tue, 8 Nov 2016 15:55:07 +0100 Subject: [PATCH 2/3] Check that size is above zero --- Source/Mac/NSImage+Hue.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Mac/NSImage+Hue.swift b/Source/Mac/NSImage+Hue.swift index 743e03c..c6fdd8a 100644 --- a/Source/Mac/NSImage+Hue.swift +++ b/Source/Mac/NSImage+Hue.swift @@ -13,6 +13,8 @@ class CountedColor { extension NSImage { fileprivate func resize(_ newSize: CGSize) -> NSImage { + guard newSize.width > 0.0 && newSize.height > 0.0 else { return NSImage() } + let scaledImage = NSImage(size: newSize) scaledImage.lockFocus() let ctx = NSGraphicsContext.current() From b734eae944215fba9e315729223c5c267dad7de1 Mon Sep 17 00:00:00 2001 From: Christoffer Winterkvist Date: Tue, 8 Nov 2016 15:55:16 +0100 Subject: [PATCH 3/3] Remove whitespace and fix indentaiton --- Source/Mac/NSImage+Hue.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Mac/NSImage+Hue.swift b/Source/Mac/NSImage+Hue.swift index c6fdd8a..af75fd3 100644 --- a/Source/Mac/NSImage+Hue.swift +++ b/Source/Mac/NSImage+Hue.swift @@ -21,7 +21,7 @@ extension NSImage { ctx?.imageInterpolation = .high draw(in: NSMakeRect(0, 0, newSize.width, newSize.height), from: NSRect.zero, operation: .copy, fraction: 1) scaledImage.unlockFocus() - + return scaledImage } @@ -87,7 +87,7 @@ extension NSImage { imageColors.add(color) } } - + var sortedColors = [CountedColor]() for color in imageBackgroundColors { @@ -140,18 +140,18 @@ extension NSImage { if primaryColor == nil && color.isContrastingWith(imageBackgroundColor) { - primaryColor = color + primaryColor = color } else if secondaryColor == nil && primaryColor != nil && primaryColor!.isDistinctFrom(color) && color.isContrastingWith(imageBackgroundColor) { - secondaryColor = color + secondaryColor = color } else if secondaryColor != nil && (secondaryColor!.isDistinctFrom(color) && primaryColor!.isDistinctFrom(color) && color.isContrastingWith(imageBackgroundColor)) { - detailColor = color - break + detailColor = color + break } }