Git Product home page Git Product logo

colorkit's Introduction

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 by the algorithm.
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!

colorkit's People

Contributors

0xleif avatar boris-em avatar pepas-everly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

colorkit's Issues

Cocoapods release

For those who can't use swift package manager it would be really handy if this great library will be available from cocoapods.

dominantColors function crash for particular image

Hey!
Found a bug for the following function:

extension UIImage {
 func getDominantColors(with minCount: Int = 0) -> [UIColor]? {
        var resultColors: [UIColor]?
        // Try With PNG Data
        if let pngData = pngData(),
           let pngImage = UIImage(data: pngData),
           let colors = try? pngImage.dominantColors(with: .best, algorithm: .iterative) {
            if colors.count >= minCount {
                return colors
            } else {
                resultColors = colors
            }
        }
        // Try With JPEG Data
        if let jpegData = jpegData(compressionQuality: 1),
           let jpegImage = UIImage(data: jpegData),
           let colors = try? jpegImage.dominantColors(with: .best, algorithm: .iterative) {
            if colors.count >= minCount {
                return colors
            } else {
                resultColors = colors
            }
        }
        // Try With Original Image
        if let colors = try? dominantColors(with: .best, algorithm: .iterative) {
            if colors.count >= minCount {
                return colors
            } else {
                resultColors = colors
            }
        }
        if let colors = try? dominantColors() {
            if colors.count >= minCount {
                return colors
            } else {
                resultColors = colors
            }
        }
        return resultColors
    }
}

When the following image is added as input parameter, crash inside ColorKit happened:
Снимок экрана 2024-01-16 в 16 12 20

Please find the image here

Image -
IMG_6111

Could this issue be fixed? Looks like the safe check for dictionary should be added. Thanks in advance!

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.

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.

iOS 17 issues getting dominantColors

I'm testing my app with the iOS 17 public beta and I get totally unexpected dominant colors, whereas on my device with iOS 16 I still see them fine.
It could be a bug of the beta but it could also be that there's something fundamentally different in UIImage now. Can you guys check that?

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.

macOS support

First of all, thanks for this great package! I would love to use it on macOS as well but noticed that it only supports iOS. Are there any plans to make this work for macOS (and/or tvOS) too?

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.

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.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.