ColorKitis your companion to work with colors on iOS.
ColorKitmakes it easy to find the dominant colors of an image. It returns a color palette of the most common colors on the image.
letdominantColors=tryimage.dominantColors()
By default,ColorKituses an iterative process to determine the dominant colors of an image. But it also supports doing so via ak-mean clustering algorithm.Choose whichever is more appropriate for your use case.
ColorKitlets you generate color palettes from a collection of colors. It will automatically ensure that the best colors are picked based on a few configurable parameters like contrast ratio.
This feature is particularly powerful when combined with the dominant color calculation.
letcolors=tryimage.dominantColors()
letpalette=ColorPalette(orderedColors:colors,ignoreContrastRatio:true)
The following examples use the palette to dynamically match the color of the text and background to the album covers.
To compute the average color of an image, simply call theaverageColor
function on aUIImage
instance.
letaverageColor=tryimage.averageColor()
Perceptual color difference / comparaison is a common problem of color science.
It simply consists of calculating how different two colors look from each other, to the human eye. This is commonly referenced as the DeltaE.
ColorKitmakes it a breaze to compare two colors.
letcolorDifference=UIColor.green.difference(from:.white)// 120.34
While this may seem trivial, simply using the RGB color model often yields non-accurate results for human perception. This is because RGB is not perceptually uniform.
Here is an example highlighting the limitations of using the RGB color model to compare colors.
As you can see, the difference between the two greens (left) is considered greater than the difference between the pink and gray colors (right). In other words, the pink and gray are considered to look more similar than the two greens by the algorithm.
This obviously does not match the expectation of the human eye.
Thankfully,ColorKitprovides algorithms that make it possible to compare colors just like the human eye would:CIE76,CIE94andCIEDE2000.
letcolorDifference=UIColor.green.difference(from:.white,using:.CIE94)
Here is the same example as above, using theCIE94algorithm.
TheCIE94algorithm successfuly realizes that the two greens (left) look closer from each other than the pink and gray (right) do.
More information about color difference can be foundhere.
To calculate the contrast ratio between two colors, simply use thecontrastRatio
function.
letcontrastRatio=UIColor.green.contrastRatio(with:UIColor.white)
The contrast ratio is particularly important when displaying text. To ensure that it's readable by everyone,ColorKitmakes it easy for you to follow the accessibility guidelines set byWCAG 2.
ColorKitassists you when translating a color from a color space to another.
They're simply supported as extensions onUIColor
.
CIELAB,XYZandCMYKare supported.
There is a lot more thatColorKitis capable of. Here is a short list of examples:
- Working with Hex color codes
lethexValue=UIColor.green.hex
letcolor=UIColor(hex:"eb4034")
- Generating random colors
letrandomColor=UIColor.random()
- Calculating the relative luminance of a color
letrelativeLuminance=UIColor.green.relativeLuminance
- Generating complementary colors
letcomplementaryColor=UIColor.green.complementaryColor
TheSwift Package Manageris the easiest way to install and manageColorKitas a dependecy.
Simply addColorKitto your dependencies in yourPackage.swift
file:
dependencies:[
.package(url:"https://github /Boris-Em/ColorKit.git")
]
Alternatively, you can also use XCode to addColorKitto your existing project, by usingFile > Swift Packages > Add Package Dependency...
.
ColorKitcan also be added to your project manually. Download theColorKitproject from Github, then drag and drop the folderColorKit/ColorKit
into your XCode project.
Use the iOS sample project included in this repository to find comprehensive examples of the different features ofColorKit.
Contributions toColorKitare always welcome!
For bugs and feature requests, open anissue.
To contribute to the code base, simply submit apull request.
See theLicense.You are free to make changes and use this in either personal or commercial projects. Attribution is not required, but highly appreciated. A little "Thanks!" (or something to that affect) is always welcome. If you useColorKitin one of your projects, please let us know!