Skip to content

Fetches the most dominant and prominent colors from an image.

License

Notifications You must be signed in to change notification settings

FelixHerrmann/UIImageColors

 
 

Repository files navigation

Xcode Build

UIImageColors

iTunes style color fetcher for UIImage and NSImage. It fetches the most dominant and prominent colors.

Installation

Add the following to the dependencies of your Package.swift:

.package(url: "https://github.com/jathu/UIImageColors.git", from: "x.x.x")

Add UIImageColors to your Podfile:

pod 'UIImageColors'

Add UIImageColors to your Cartfile:

github "jathu/UIImageColors"

Manual

Copy the UIImageColors folder into your project.

For Objective-C projects copy the entire Sources folder.

Examples

iOS (Swift)
import UIImageColors

let image = UIImage(named: "example.png")

// Synchronous
let colors = image.getColors()

// Asynchronous(completion-handler)
image.getColors { colors in
  
}

// Asynchronous(async/await)
let colors = await image.colors()
iOS (Objective-C)
@import UIImageColorsObjc;

UIImage *image = [UIImage imageNamed:@"example.png"];

// Synchronous
UIImageColors *colors = [image getColorsWithQuality:UIImageColorsScaleQualityHigh];

// Asynchronous
[image getColorsWithQuality:UIImageColorsScaleQualityHigh completion:^(UIImageColors * _Nullable colors) {
    
}];
macOS (Swift)
import UIImageColors

let image = NSImage(named: "example.png")

// Synchronous
let colors = image.getColors()

// Asynchronous(completion-handler)
image.getColors { colors in
  
}

// Asynchronous(async/await)
let colors = await image.colors()
macOS (Objective-C)
@import UIImageColorsObjc;

NSImage *image = [NSImage imageNamed:@"example.png"];

// Synchronous
NSImageColors *colors = [image getColorsWithQuality:UIImageColorsScaleQualityHigh];

// Asynchronous
[image getColorsWithQuality:UIImageColorsScaleQualityHigh completion:^(NSImageColors * _Nullable colors) {
    
}];

Colors-object

Colors is an object that contains four different color-properties and is used as the return type.

Property Description
background The most common, non-black/white color.
primary The most common color that is contrasting with the background.
secondary The second most common color that is contrasting with the background. Also must distinguish itself from the primary color.
detail The third most common color that is contrasting with the background. Also must distinguish itself from the primary and secondary color.

In Swift Colors is a struct and an extension of UIImage/NSImage; in Objective-C there is a UIImageColors/NSImageColors class which is a concrete subclass of NSObject.

ScaleQuality-enum

ScaleQuality is an enum with four different qualities. The qualities refer to how much the original image is scaled down. Higher qualities will give better results at the cost of performance.

Value Quality
low 50 pixel
medium 100 pixel
high 250 pixel
full no scaling
custom (Swift only) given value

All methods provide a quality parameter (which is set to .high by default in Swift).

let colors = image.getColors(quality: .low)
let asyncColors = await image.colors(quality: .custom(10))
image.getColors(quality: .full) { colors in /*...*/ }
UIImageColors *colors = [image getColorsWithQuality:UIImageColorsScaleQualityLow];
[image getColorsWithQuality:UIImageColorsScaleQualityFull completion:^(NSImageColors * _Nullable colors) { /*...*/ }];

License

The license is provided in the project folder. This is based on Panic's OS X ColorArt.


June 2015 - Toronto

About

Fetches the most dominant and prominent colors from an image.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 69.4%
  • Objective-C 29.2%
  • Ruby 1.4%