             return nil
         }
         
+        let edgeColorSet = NSCountedSet()
+        
+        for row in 0 ..< height {
+            let leftEdgeIndex = row * bpr
+            let leftEdgeColor = CARGBColor(r: CGFloat(data[leftEdgeIndex]) / 255,
+                                           g: CGFloat(data[leftEdgeIndex+1]) / 255,
+                                           b: CGFloat(data[leftEdgeIndex+2]) / 255)
+            edgeColorSet.add(leftEdgeColor)
+            
+            let rightEdgeIndex = row * bpr + (width-1) * bytesPerPixel
+            let rightEdgeColor = CARGBColor(r: CGFloat(data[rightEdgeIndex]) / 255,
+                                            g: CGFloat(data[rightEdgeIndex+1]) / 255,
+                                            b: CGFloat(data[rightEdgeIndex+2]) / 255)
+            edgeColorSet.add(rightEdgeColor)
+        }
+        for col in 0 ..< width {
+            let topEdgeIndex = col * bytesPerPixel
+            let topEdgeColor = CARGBColor(r: CGFloat(data[topEdgeIndex]) / 255,
+                                          g: CGFloat(data[topEdgeIndex+1]) / 255,
+                                          b: CGFloat(data[topEdgeIndex+2]) / 255)
+            edgeColorSet.add(topEdgeColor)
+            
+            let bottomEdgeIndex = (height-1) * bpr + col * bytesPerPixel
+            let bottomEdgeColor = CARGBColor(r: CGFloat(data[bottomEdgeIndex]) / 255,
+                                             g: CGFloat(data[bottomEdgeIndex+1]) / 255,
+                                             b: CGFloat(data[bottomEdgeIndex+2]) / 255)
+            edgeColorSet.add(bottomEdgeColor)
+        }
+        
+        let edgeColors = edgeColorSet.objectEnumerator().allObjects.map() {
+            CACountedRGBColor(color: ($0 as! CARGBColor), count: edgeColorSet.count(for: $0))
+        }
+        
+        return edgeColors
+    }
+    
+    func findColors(isDarkBackground: Bool) -> [CACountedRGBColor]? {
+        let width = image.width
+        let height = image.height
+        
+        let bpr = image.bytesPerRow
+        let bpp = image.bitsPerPixel
+        let bpc = image.bitsPerComponent
+        let bytesPerPixel = bpp / bpc
+        
+        guard let data = image.dataProvider?.data as? Data else {
+            return nil
+        }
+        
         let colorSet = NSCountedSet()
-        let edgeColorSet = NSCountedSet()
         
         for row in 0 ..< height {
             for col in 0 ..< width {

        }
        
        backgroundColor = background.cgColor
        primaryColor = textColors.primary?.cgColor ?? fallback
        secondaryColor = textColors.secondary?.cgColor ?? fallback
        detailColor = textColors.detail?.cgColor ?? fallback
    }
    
    public func analyze(completionHandler: () -> Void) {

    }
    
    public func analyze() {
        guard let edgeColors = findEdgeColors(),
            let background = findEdgeColor(in: edgeColors) else {
            return
        }
        
        let colors = findColors(isDarkBackground: background.isDark)
        let textColors = colors.flatMap() { findTextColor(in: $0, background: background) }
        
        let fallback:CGColor
        if background.isDark {
