A library for extracting color from an image.
- Features
- How to use
- Contrast colors
- Average colors
- Cluster colors
- Installation
- Example
- How the algorithm works
- Contributing
- License
The DominantColors makes it easy to find the dominant colors of the image. It returns a color palette of the most common colors in the image.
A quick way to get colors from an image where the image type is CGImage
, UIImage
or NSImage
:
let dominantColors = try? image.dominantColors(max: 6) // Array of UIColor, NSColor or CGColor
If you need more settings, then call the method directly from the library according to the standard settings:
// Get the CGColors according to the standard settings:
let cgColors = try? DominantColors.dominantColors(image: cgImage, maxCount: 6)
// Get the UIColors according to the standard settings
let uiColors = try? DominantColors.dominantColors(uiImage: uiImage, maxCount: 6)
// Get the NSColors according to the standard settings:
let nsColors = try? DominantColors.dominantColors(nsImage: nsImage, maxCount: 6)
// Get the `Color.Resolved` according to the standard settings:
let colorsResolved = try? DominantColors.dominantColorsResolved(image: cgImage)
Next examples are given for CGColor
, but this is also true for UIColor
and NSColor
Get colors by selecting algorithm. Extract colors by specifying the color difference formula:
let cgColors = try? DominantColors.dominantColors(image: cgImage, algorithm: .CIE76)
If you need more accurate results, then there is maximum quality (the default is fair
):
let cgColors = try? DominantColors.dominantColors(image: cgImage, quality: .best, algorithm: .CIE76)
To quickly extract colors, use:
let cgColors = try? DominantColors.dominantColors(image: cgImage, quality: .fair)
In this case, a pixelization algorithm is used for the original image.
Get colors with options:
let cgColors = try? DominantColors.dominantColors(image: cgImage, options: [.excludeBlack, .excludeGray, .excludeWhite])
For the desired sequence and colors, specify the sorting type:
let cgColors = try? DominantColors.dominantColors(image: cgImage, maxCount: 6, sorting: .darkness)
The default is frequency
, which sorts colors in descending order of their number of pixels with that color.
Get the average color by dividing the image into segments horizontally:
let cgColors = try? DominantColors.averageColors(image: cgImage, count: 8)
Finds the dominant colors of an image by using a k-means clustering algorithm:
let cgColors = try? DominantColors.kMeansClusteringColors(image: cgImage, quality: .fair, count: 10)
In order to obtain colors for displaying text or accompanying content based on the palette of the ordered image, you must use ContrastColors
:
let dominantColors = try image.dominantColors()
let contrastColors = ContrastColors(colors: dominantColors.map({ $0.cgColor }))
let backgroundColor = contrastColors?.background
let primaryColor = contrastColors?.primary
let secondaryColor = contrastColors?.secondary
You can get this result used backgroundColor
for background, primaryColor
for title and secondaryColor
for subtitle:
- Open the DominantColors.xcworkspace file.
- Select the iOS project target will preview the package change via iOS preview, same for selecting the macOS target, the code change can be previewed on macOS.
- PreviewMacOS in package,
- App MacOSPreview in example folder.
- PreviewiOS in package,
- App iOSDominantColorsPreview in example folder.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding DominantColors as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/DenDmitriev/DominantColors.git", .upToNextMajor(from: "1.2.0"))
]
The original image is converted according to the specified quality.
One color is taken from each generated pixel.
Source | Resize | Pixellate |
One color is taken from each generated pixel.
Source | Resize | Pixellate |
One color is taken from each generated pixel.
Source | Resize | Pixellate |
Each pixel color is taken without changing the image.
Next, the colors are sorted by ColorShade
and color normality is calculated using the number of pixels of the same color and normal saturation and brightness values. After sorting, each shade bin connects the colors by calculating сolor difference. This happens cyclically until the required number of flowers is in the basket. The next step is to connect the colors between the baskets in the same way until you have the required number of colors. Well, the result is displayed.
Contribution to the DominantColors is always welcome. To get information about errors and feature requests, open the issue. To contribute to the codebase, just send a pull request.
See the license. The library uses the code of the original library ColorKit, which is rewritten from Objective-C to Swift and supplemented.