title: Getting main colors of a UIImage in Swift
body: <p>I am trying to get the main colors of a UIImage in Swift, and have tried to port <a href="https://stackoverflow.com/questions/13694618/objective-c-getting-least-used-and-most-used-color-in-a-image/29266983#29266983">this code</a>. Unfortunately the code keeps returning the same colors. I saw <a href="https://stackoverflow.com/a/32445855/5182628">the answer provided here</a> also keeps returning the same color. I have avoided the use of a CIFilter, since it only returns the average color in my research.</p>
  
  <p>Below the ported code. I have set the CGContext Data to nil, since Swift can handle memory on it's on and in my testing it gave a lot of memory errors.</p>
  
  <pre><code>func mainColors(image:UIImage, detail: Int) -&gt; [UIColor] {
              //COLOR PROCESS STEP 1:
              //Determine the detail.
                  var dimension = 10
                  var flexibility = 2
                  var range = 60
  
          //Low detail.
              if detail == 0 {
                  dimension = 4
                  flexibility = 1
                  range = 100
              }
          //High detail.
              else if detail == 2 {
                  dimension = 100
                  flexibility = 10
                  range = 20
              }
  
          //COLOR PROCESS STEP 2:
          //Determine the colors in the image.
  
          //Create an array to store the colors.
              var colors = Array&lt;Array&lt;CGFloat&gt;&gt;()
  
          //Get the bitmap data of the image.
              let imageRef = image.cgImage
          //Variable to store the color space, RGB in this case.
              let colorSpace = CGColorSpaceCreateDeviceRGB()
          //Additional CGContext data.
              let bytesPerPixel = 4
              let bytesPerRow = bytesPerPixel * dimension
              let bitsPerComponent = 8
          //Create the context. Data uses the memory pointer created above, the width and height determine the dimensions of the bitmap, the space is for the colorspace, the bitmap specifies the alpha channel.
              let context = CGContext(data: nil, width: dimension, height: dimension, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue)!
          //Draw the image.
              let rect = CGRect(x: 0, y: 0, width: dimension, height: dimension)
              context.draw(imageRef!, in: rect)
  
          //Iterate through the raw data in order to create a UIColor.
              var x = 0
              var y = 0
  
              for _ in 0..&lt;(dimension * dimension) {
                  let index = (bytesPerRow * y) + x * bytesPerPixel
                  let red = CGFloat(index)
                  let green = CGFloat(index + 1)
                  let blue = CGFloat(index + 2)
                  let alpha = CGFloat(index + 3)
  
                  let color = [red, green, blue, alpha]
                  colors.append(color)
  
                  y += 1
                  if y == dimension {
                      y = 0
                      x += 1
                  }
              }
  
          //Deallocate the mutable pointer.
              //free(rawData)
  
          //COLOR PROCESS STEP 3:
          //Add some color flexibility.
  
          //Create an array containing the previous colored items and create another one for the flexible colors.
              var copiedColors = colors
              var flexibleColors = Array&lt;String&gt;()
  
          //Iterate through the copied colors in order to create an improved UIColor.
              let flexFactor = flexibility * 2 + 1
              let factor = flexFactor * flexFactor * 3
  
              for n in 0..&lt;(dimension * dimension) {
                  let pixelColors = copiedColors[n]
  
                  var reds = Array&lt;CGFloat&gt;()
                  var greens = Array&lt;CGFloat&gt;()
                  var blues = Array&lt;CGFloat&gt;()
  
                  for p in 0..&lt;3 {
                      let rgb = pixelColors[p]
  
                      for f in -flexibility...flexibility {
                          var newRGB = rgb + CGFloat(f)
  
                          if newRGB &lt; 0 {
                              newRGB = 0
                          }
  
                          switch p {
                              case 0:
                                  reds.append(newRGB)
                              case 1:
                                  greens.append(newRGB)
                              case 2:
                                  blues.append(newRGB)
                              default:
                                  print("Error! Loop out of range! \(p)")
                          }
                      }
                  }
  
                  var r = 0
                  var g = 0
                  var b = 0
  
                  for _ in 0..&lt;factor {
                      let red = reds[r]
                      let green = greens[g]
                      let blue = blues[b]
  
                      let rgbString = "\(red),\(green),\(blue)"
                      flexibleColors.append(rgbString)
  
                      b += 1
                      if b == flexFactor {
                          b = 0
                          g += 1
                      }
                      if g == flexFactor {
                          g = 0
                          r += 1
                      }
                  }
              }
  
          //COLOR PROCESS STEP 4:
          //Distinguish the colors. Orders the flexible colors by their occurence and then keeps them if they are sufficiently disimilar.
  
          //Dictionary to store all the colors.
              let colorCounter = NSMutableDictionary()
  
          //Check the number of times item is in array.
              let countedSet = NSCountedSet(array: flexibleColors)
  
              for item in countedSet {
                  let item = item as! String
  
                  let count = countedSet.count(for: item)
                  let value = NSNumber(integerLiteral: count)
                  colorCounter.setValue(value, forKey: item)
              }
  
          //Sort keys from highest occurence to lowest.
              let orderedKeys = colorCounter.keysSortedByValue(comparator: {
                  (obj1, obj2) in
                  let x = obj1 as! NSNumber
                  let y = obj2 as! NSNumber
                  return x.compare(y)
              })
  
          //Check if the color is similar to another one already included.
              var ranges = Array&lt;String&gt;()
  
              for key in orderedKeys as! [String] {
                  let rgb = key.components(separatedBy: ",")
                  let r = NSString(string: rgb[0]).integerValue
                  let g = NSString(string: rgb[1]).integerValue
                  let b = NSString(string: rgb[2]).integerValue
  
                  var exclude = false
  
                  for rangedkey in ranges {
                      let rangedRGB = rangedkey.components(separatedBy: ",")
  
                      let ranged_r = NSString(string: rangedRGB[0]).integerValue
                      let ranged_g = NSString(string: rangedRGB[1]).integerValue
                      let ranged_b = NSString(string: rangedRGB[2]).integerValue
  
                      if r &gt;= ranged_r - range &amp;&amp; r &lt;= ranged_r + range {
                          if g &gt;= ranged_g - range &amp;&amp; g &lt;= ranged_g + range {
                              if b &gt;= ranged_b - range &amp;&amp; b &lt;= ranged_b + range {
                                  exclude = true
                              }
                          }
                      }
                  }
  
                  if exclude == false {
                      ranges.append(key)
                  }
              }
  
          //Create the colors and fill them.
              var mainColors = Array&lt;UIColor&gt;()
  
              for key in ranges {
                  let rgb = key.components(separatedBy: ",")
                  let r = NSString(string: rgb[0]).floatValue
                  let g = NSString(string: rgb[1]).floatValue
                  let b = NSString(string: rgb[2]).floatValue
  
                  let finalColor = UIColor(red: CGFloat((r / 255)), green: CGFloat((g / 255)), blue: CGFloat((b / 255)), alpha: CGFloat(1.0))
  
                  mainColors.append(finalColor)
              }
  
              return mainColors
      }
  </code></pre>