A library for extracting color from an image.
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.
![Example1](https://private-user-images.githubusercontent.com/65191747/269075441-8d3cfe77-125a-4e61-be32-17d23fafae57.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI1MjM0ODksIm5iZiI6MTcyMjUyMzE4OSwicGF0aCI6Ii82NTE5MTc0Ny8yNjkwNzU0NDEtOGQzY2ZlNzctMTI1YS00ZTYxLWJlMzItMTdkMjNmYWZhZTU3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDE0Mzk0OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI0OWQyMThlZmQwYmZhYjg5ZWQ4MGJlOWYxYzdhMDdjYjI0N2NkZGJjZjU4YjQ0YzNlNmIwMWNlZjAwYTVjOTcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0._ve_LYSK9VDSY_0vnLKIVIaGiWXai8oC8G-APwY9czo)
![Example3](https://private-user-images.githubusercontent.com/65191747/269075589-75c11db0-1c0a-4c2e-99b0-a9033dd2b3b5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI1MjM0ODksIm5iZiI6MTcyMjUyMzE4OSwicGF0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z0zMDAmWC1BbXotU2lnbmF0dXJlPTA5MDRkYzFjMTQ2NzM4NmU2OGMyNzU5NTQyYjY0ZjU5ZGFlZTIwODdiNzQzNmM5N2JkMjE5ZTNhNmZiOGIzYzImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.5DOYPQu9tKA2OVNX_GpSL7teC857qD_M--h5BXdqWJU)
![Example4](https://private-user-images.githubusercontent.com/65191747/269075614-198e2e29-24be-4301-9900-b87d8c8b8042.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI1MjM0ODksIm5iZiI6MTcyMjUyMzE4OSwicGF0aCI6Ii82NTE5MTc0Ny8yNjkwNzU2MTQtMTk4ZTJlMjktMjRiZS00MzAxLTk5MDAtYjg3ZDhjOGI4MDQyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDE0Mzk0OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWU2ZTczZTA0ZjAzNGIzODEwODU3YTIzMDBjNzk4MDg3MmJjNTdmYWRlZDdiZTMxNzg4N2YyZWJiNzk4MTA1NTEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.q0dTFZMLduz5Ed9jiwnx9baxNUW-hPoxmkEXEo_xEJA)
Get the colors according to the standard settings:
let cgColors = try DominantColors.dominantColors(image: cgImage)
Get colors by selecting algorithm. Extract colors by specifying the color difference formula:
let formula: DeltaEFormula = .CIE94
let algorithm: DominantColorAlgorithm = .iterative(formula: formula)
let cgColors = try DominantColors.dominantColors(image: cgImage, algorithm: algorithm)
Get colors with options:
let flags: [DominantColors.Flag] = [.excludeBlack]
let cgColors = try DominantColors.dominantColors(image: cgImage, flags: flags)
Get the average color by dividing the image into segments horizontally:
let count: UInt8 = 8
let cgColors = try DominantColors.dominantColors(image: cgImage, algorithm: .areaAverage(count: count))
Use the macOS example ImageColors project included in this repository to find examples of various DominantColors features.
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.0.5"))
]
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.