ColorKit makes it easy to find the dominant colors of an image

Overview

ColorKit

ColorKit is your companion to work with colors on iOS.

Build MIT License Swift 5.1


Features

Dominant Colors

ColorKit makes it easy to find the dominant colors of an image. It returns a color palette of the most common colors on the image.

let dominantColors = try image.dominantColors()

By default, ColorKit uses an iterative process to determine the dominant colors of an image. But it also supports doing so via a k-mean clustering algorithm. Choose whichever is more appropriate for your use case.


Color Palette

ColorKit lets 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.

let colors = try image.dominantColors()
let palette = 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.


Average Color

To compute the average color of an image, simply call the averageColor function on a UIImage instance.

let averageColor = try image.averageColor()

Color Difference (DeltaE)

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.

ColorKit makes it a breaze to compare two colors.

let colorDifference = 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.
This obviously does not match the expectation of the human eye.

Thankfully, ColorKit provides algorithms that make it possible to compare colors just like the human eye would: CIE76, CIE94 and CIEDE2000.

let colorDifference = UIColor.green.difference(from: .white, using: .CIE94) 

Here is the same example as above, using the CIE94 algorithm.

The CIE94 algorithm 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 found here.


Contrast Ratio

To calculate the contrast ratio between two colors, simply use the contrastRatio function.

let contrastRatio = UIColor.green.contrastRatio(with: UIColor.white)

The contrast ratio is particularly important when displaying text. To ensure that it's readable by everyone, ColorKit makes it easy for you to follow the accessibility guidelines set by WCAG 2.


Color Space Conversions

ColorKit assists you when translating a color from a color space to another. They're simply supported as extensions on UIColor.
CIELAB, XYZ and CMYK are supported.


More

There is a lot more that ColorKit is capable of. Here is a short list of examples:

  • Working with Hex color codes
let hexValue = UIColor.green.hex
let color = UIColor(hex: "eb4034")
  • Generating random colors
let randomColor = UIColor.random()
  • Calculating the relative luminance of a color
let relativeLuminance = UIColor.green.relativeLuminance
  • Generating complementary colors
let complementaryColor = UIColor.green.complementaryColor

Installation

Swift Package Manager

The Swift Package Manager is the easiest way to install and manage ColorKit as a dependecy.
Simply add ColorKit to your dependencies in your Package.swift file:

dependencies: [
    .package(url: "https://github.com/Boris-Em/ColorKit.git")
]

Alternatively, you can also use XCode to add ColorKit to your existing project, by using File > Swift Packages > Add Package Dependency....

Manually

ColorKit can also be added to your project manually. Download the ColorKit project from Github, then drag and drop the folder ColorKit/ColorKit into your XCode project.


Sample Project

Use the iOS sample project included in this repository to find comprehensive examples of the different features of ColorKit.


Contributing

Contributions to ColorKit are always welcome!
For bugs and feature requests, open an issue.
To contribute to the code base, simply submit a pull request.


License

See the License. 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 use ColorKit in one of your projects, please let us know!

Comments
  • EXC_BAD_ACCESS when using dominantColors() with default arguments

    EXC_BAD_ACCESS when using dominantColors() with default arguments

    When trying to fetch dominant colors from some images an exception is raised.

    Screen Shot 2022-04-21 at 09 57 23

    File: DominantColors.swift line 159

    This is the image that's causing crash IMG_3836

    In my code I just called the dominantColors() method with default arguments.

    opened by wiencheck 2
  • Bug on complimentaryColor() ?

    Bug on complimentaryColor() ?

    I think this might be a bug? Here's my code.

    let greenComp:UIColor = UIColor.green.complementaryColor //works let greenCompComp:UIColor = greenComp.complimentaryColor //error: value of type 'UIColor' has no member 'complimentaryColor'

    I know that UIColor does have member complimentaryColor cause it just used it in line 1. Not sure if I did something wrong.

    bug 
    opened by DandyLyons 2
  • Fixed crash related to unwrapping nil value when creating ColorPalette

    Fixed crash related to unwrapping nil value when creating ColorPalette

    With certain images the primary/backgroundColor values could not be obtained which led to crash when trying to unwrap those values.

    In such cases debugger prints such message: -[CIContext render:toBitmap:rowBytes:bounds:format:colorSpace:] unsupported colorspace.

    opened by wiencheck 1
  • kMeansClustering algorithm: Fatal error: Unexpectedly found nil while unwrapping an Optional value

    kMeansClustering algorithm: Fatal error: Unexpectedly found nil while unwrapping an Optional value

    Hey, first of all thanks for this amazing, impressive library.

    I'd like to report that when using .kMeansClustering algorithm, I often get a crash from unwrapping nil value. It happens at line 112 in file ColorPalette.swift

    The image I'm feeding to the algorithm is a transparent PNG (an asset file) so that's probably the cause of it. I know it doesn't make sense to pull colors from such image but the code should fail gracefully in such cases.

    opened by wiencheck 1
  • Task/actions checkout v3

    Task/actions checkout v3

    To fix the warning we get when running workflows: Node.js 12 actions are deprecated. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/. Please update the following actions to use Node.js 16

    opened by roxannelandry 0
  • Add Localization Support for `name()` Function

    Add Localization Support for `name()` Function

    The name() function introduced recently was only supporting English. It is now setup to use Apple's localization system. This PR also adds the first localized language: French.

    Fixes #11.

    opened by Boris-Em 0
  • Localize Color Names

    Localize Color Names

    Via #10, we introduced a new function called name() that gives the English name of a UIColor instance.

    Localizing those names makes sense as a next step.

    enhancement 
    opened by Boris-Em 0
  • Add `name` function

    Add `name` function

    This commit simply adds a new function name() to UIColor instances via an extension. This function is useful to get a readable english name of the current color.

    Fixes #6.

    opened by Boris-Em 0
  • Added missing comma to README.md

    Added missing comma to README.md

    Expected

    let colors = try image.dominantColors()
    let palette = ColorPalette(orderedColors: colors, ignoreContrastRatio: true)
    

    Actual

    let colors = try image.dominantColors()
    let palette = ColorPalette(orderedColors: colors ignoreContrastRatio: true)
    
    opened by 0xLeif 0
  • Color Names for Accessibility

    Color Names for Accessibility

    A useful feature for accessibility is to be able to attach a readable color name to UIColor instances. For example UIColor.red or UIColor(red: 1.0, green: 0.0, blue: 0.0) should read something like "red".

    A trivial implementation of this could be to work with a predefined color palette and just compare it to the colors using the existing deltaE.

    enhancement good first issue 
    opened by Boris-Em 0
  • ColorKit, when using dominantColors(algorithm: .kMeansClustering) memory error

    ColorKit, when using dominantColors(algorithm: .kMeansClustering) memory error

    I'm using this function on each image of a video feed, and after a few seconds/tens of second, my program stops with this error, EXC_BAD_ACCESS (code=1, address=0xd833deece4d0). I assume it comes from this function and precisely kMeansClustering, because I don't encounter this issue using .iterative algorithm

    opened by Alexandre-BARBIER 3
  • I can't get dominant colors from pictures taken with iPhone

    I can't get dominant colors from pictures taken with iPhone

    Hello,

    first of all I wanted to say thank you for this awesome kit.

    I'm using ColorKit to generate a color palette of pictures that I manually add from my photo library. Everything work good but I only have problems with pictures of my library that were not taken with my iPhone.

    If I want to get the colors of pictures taken with my iPhone or a screenshot which was taken with my iPhone the function .dominantColors does not work.

    Is it a format thing? or maybe I missed something.

    If I send the picture per WhatsApp and then I download the same picture I can get the colors but not from the original. Photos taken with an external camera and then imported to the iPhone have also no problem at all. Only Iphone photos.

    I appreciate your help, thank you very much!

    Pablo.

    UPDATE I think that the problem is that ColorKit can't use .dominantColors() with pictures taken with the iPhone because they are HEIF/HEIC files. I still can't solve the problem but I'm trying to convert the image to JPEG. I don't know if that is useful because I thought UIImage also supports HEIF format.

    opened by papueh 3
  • Color Histogram

    Color Histogram

    Generating color histograms out of images.

    Here is some good resource from OpenCV as to what type of histogram we could generate as well as how to represent them.

    enhancement 
    opened by Boris-Em 0
Releases(v1.0.0)
Owner
Boris Emorine
Boris Emorine
All wikipedia colors implemented as easy to use UIColor extension 🌈

UIColor-WikiColors All wikipedia colors implemented as an easy to use UIColor extension. Have you ever tried using UIColor.lightBlue just to be welcom

Szymon Maślanka 58 Jul 30, 2022
Yet another extension to manipulate colors easily in Swift and SwiftUI

DynamicColor provides powerful methods to manipulate colors in an easy way in Swift and SwiftUI. Requirements • Usage • Installation • Contribution •

Yannick Loriot 2.8k Dec 30, 2022
A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.

Installation Drag the included Colours.h and Colours.m files into your project. They are located in the top-level directory. You can see a demo of how

Ben Gordon 3.1k Dec 28, 2022
Manage Colors, Integrate Night/Multiple Themes. (Unmaintained)

Easily integrate and high performance Providing UIKit and CoreAnimation category Read colour customisation from file Support different themes Generate

Draven 3.6k Dec 15, 2022
A pure Swift library that allows you to easily convert SwiftUI Colors to Hex String and vice versa.

iOS · macOS · watchOS · tvOS A pure Swift library that allows you to easily convert SwiftUI Colors to Hex String and vice versa. There is also support

Novem 3 Nov 20, 2022
Save the color of your moments. Remember your moments with colors you name.🎨

Pixel Palette ?? What was the color of the sunset yesterday? Or the color you would like to use in your vlog? Save the color of your moments with Pixe

Sue Cho 21 Aug 7, 2022
A pure Swift library for using ANSI codes. Basically makes command-line coloring and styling very easy!

Colors A pure Swift library for using ANSI codes. Basically makes command-line coloring and styling very easy! Note: Colors master requires Xcode 7.3

Chad Scira 27 Jun 3, 2021
UIGradient - A simple and powerful library for using gradient layer, image, color

UIGradient is now available on CocoaPods. Simply add the following to your project Podfile, and you'll be good to go.

Đinh Quang Hiếu 247 Dec 1, 2022
A powerful and easy to use live mesh gradient renderer for iOS.

MeshKit A powerful and easy to use live mesh gradient renderer for iOS. This project wouldn't be possible without the awesome work from Moving Parts a

Ethan Lipnik 51 Jan 1, 2023
ColorKit makes it easy to find the dominant colors of an image

ColorKit ColorKit is your companion to work with colors on iOS. Features Installation Sample Project Contributing License Features Dominant Colors Col

Boris Emorine 570 Jan 5, 2023
Fetches the most dominant and prominent colors from an image.

UIImageColors iTunes style color fetcher for UIImage and NSImage. It fetches the most dominant and prominent colors. Installation Manual Copy UIImageC

null 3.2k Jan 1, 2023
Create complementary gradients generated from dominant and prominent colors in supplied image. Inspired by Grade.js

ComplimentaryGradientView Create complementary gradients generated from dominant and prominent colors in supplied image. Inspired by Grade.js ❤️ .all

George Kye 728 Dec 17, 2022
Color framework for Swift & Objective-C (Gradient colors, hexcode support, colors from images & more).

Swift 3 To use the Swift 3 version, add this to your Podfile (until 2.2 or higher is released): pod 'ChameleonFramework/Swift', :git => 'https://githu

Vicc Alexander 12.5k Dec 27, 2022
Random-Colors-iOS - Random colors generator app with auto layout

Random Colors Random colors generator app with auto layout Demo demo.mp4 Depende

Adem Özcan 8 Mar 23, 2022
Simple SwiftUI application that allows users to find the resistance value of any four-band resistor by inputting the colors of the bands on the resistors.

Resistor-Finder Simple SwiftUI application that allows users to find the resistance value of any four-band resistor by inputting the colors of the ban

Aaditya Shah 0 Dec 23, 2021
Xcode Plugin helps you find missing methods in your class header, protocols, and super class, also makes fast inserting.

FastStub-Xcode Life is short, why waste it on meaningless typing? What is it? A code generating feature borrowed from Android Studio. FastStub automat

mrpeak 509 Jun 29, 2022
All wikipedia colors implemented as easy to use UIColor extension 🌈

UIColor-WikiColors All wikipedia colors implemented as an easy to use UIColor extension. Have you ever tried using UIColor.lightBlue just to be welcom

Szymon Maślanka 58 Jul 30, 2022
Yet another extension to manipulate colors easily in Swift and SwiftUI

DynamicColor provides powerful methods to manipulate colors in an easy way in Swift and SwiftUI. Requirements • Usage • Installation • Contribution •

Yannick Loriot 2.8k Dec 30, 2022
A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.

Installation Drag the included Colours.h and Colours.m files into your project. They are located in the top-level directory. You can see a demo of how

Ben Gordon 3.1k Dec 28, 2022