SwiftColorSampler is a UIImage extension for sampling prominent colors from an image. I built this to mimic the cover art color palette generation in iTunes 11. See this StackOverflow question for more details.
SwiftColorSampler is available to install with Carthage. To integrate SwiftColorSampler into your Xcode project using Carthage, specify it in your Cartfile
:
github "mhmiles/SwiftColorSampler" ~> 0.1.0
If you prefer not to use Carthage, you can integrate SwiftColorSampler into your project manually.
- Open up Terminal,
cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
- Add SwiftColorSampler as a git submodule by running the following command:
$ git submodule add https://github.com/mhmiles/SwiftColorSampler.git
-
Open the new
SwiftColorSampler
folder, and drag theSwiftColorSampler.xcodeproj
into the Project Navigator of your application's Xcode project.It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.
-
Select the
SwiftColorSampler.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target. -
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
-
In the tab bar at the top of that window, open the "General" panel.
-
Click on the
+
button under the "Embedded Binaries" section. -
Select the
SwiftColorSampler.framework
. -
And that's it!
The
SwiftColorSampler.framework
is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
UIImage.sampleColors(count: Int, colorDepth: Int = 4)
Reduces the full color palette an image to a reduce palette of size count
. colorDepth
is an optional parameter that affects the precision of the quantization with a maximum of 8 and a default of 4. A higher colorDepth
increases precision at the cost of performance. Values nearer to 8 are only recommended for images with a very low color count and where high precision is desired.
UIImage.sampleColors(count: Int, rect: CGRect, colorDepth: Int = 4)
Applies sampleColors to a subregion of an image bounded by rect
.
All pull requests will be considered. Feel free to contact me with any questions or suggestions.
- Thanks to Dan Bloomberg and Leptonica for this incredibly informative PDF on Octree Color Quantization.
- Thanks to the Alamofire Software Foundation for the much of the formatting and content of this readme.
SwiftColorSampler is released under the MIT license.