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:
![Снимок экрана 2024-05-13 в 08 49 30](https://private-user-images.githubusercontent.com/65191747/329939842-90195830-5679-47e6-8ffc-004e482f017f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjM3NDU0MTIsIm5iZiI6MTcyMzc0NTExMiwicGF0aCI6Ii82NTE5MTc0Ny8zMjk5Mzk4NDItOTAxOTU4MzAtNTY3OS00N2U2LThmZmMtMDA0ZTQ4MmYwMTdmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE1VDE4MDUxMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY3M2JkNDlhODZhNjk2NzA0NDhlMzNlY2U2NTE4ODU2NDYxMTljNWQ3MDQ4NmI4MTYzZmEwNjQ3ZmQ3MjI0NzAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.klEvr24POlAM_T0Zj4G8W0Adk7IQhsWOgsB-ENsa-0U)
![Снимок экрана 2024-05-13 в 08 50 04](https://private-user-images.githubusercontent.com/65191747/329939867-6fa57bc5-83bf-4676-be0a-9e8ace623cc4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjM3NDU0MTIsIm5iZiI6MTcyMzc0NTExMiwicGF0aCI6Ii82NTE5MTc0Ny8zMjk5Mzk4NjctNmZhNTdiYzUtODNiZi00Njc2LWJlMGEtOWU4YWNlNjIzY2M0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE1VDE4MDUxMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQyYzJjZTJjYzE3YjYzYjRjOGU4MTQ0YzA1MjhmMWQ0OTY1MGFiOTBmNjVhZjEzYWFiZGJiMjJhMjE4OTIwOWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.WAHOpskkfKcHJ_Ty8VEiMubNgHM5H_kIvTTvo3248Iw)
- 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.