GithubHelp home page GithubHelp logo

dendmitriev / dominantcolors Goto Github PK

View Code? Open in Web Editor NEW
41.0 2.0 8.0 15.63 MB

Extractor dominant colors from image

License: MIT License

Swift 100.00%
colors palette swift swiftpackagemanager color-palette library

dominantcolors's Introduction

DominantColors

A library for extracting color from an image.

Features

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.

LittleMissSunshine Strip

ComeTogether Strip

The_Weeknd_-_Starboy Strip

How to use

Standard settings

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)

Custom settings

Next examples are given for CGColor, but this is also true for UIColor and NSColor

Algorithm settings

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.

Options settings

Get colors with options:

let cgColors = try? DominantColors.dominantColors(image: cgImage, options: [.excludeBlack, .excludeGray, .excludeWhite])

Sorting settings

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.

Average colors

Get the average color by dividing the image into segments horizontally:

let cgColors = try? DominantColors.averageColors(image: cgImage, count: 8)

Cluster colors

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)

Contrast colors

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 Снимок экрана 2024-05-13 в 08 50 04

Example

  1. Open the DominantColors.xcworkspace file.
  2. 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.

macOS

iOS

Installation

Swift Package Manager

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"))
]

How the algorithm works

The original image is converted according to the specified quality.

Low

One color is taken from each generated pixel.

Source Resize Pixellate
1 image_resize_low image_pixellate_low

Fair

One color is taken from each generated pixel.

Source Resize Pixellate
1 image_resize_low image_pixellate_low

High

One color is taken from each generated pixel.

Source Resize Pixellate
1 image_resize_low image_pixellate_low

Best

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.

Contributing

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.

License

See the license. The library uses the code of the original library ColorKit, which is rewritten from Objective-C to Swift and supplemented.

dominantcolors's People

Contributors

dendmitriev avatar milanvarady avatar sindresorhus avatar vvisionnn 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

Watchers

 avatar  avatar

dominantcolors's Issues

EXC_BAD_ACCESS in ExtractColors

Sometimes the application crashes. I think the last time it was with this image.

v4_179298-329992

Capture d’écran 2024-05-12 à 15 46 30

My code is:

            let averageColor = try DominantColors.averageColors(image: image, count: 1, sorting: .frequency)
            let dominantColors = try DominantColors.dominantColors(image: image, quality: .best, algorithm: .CIE76, maxCount: 4, options: [.excludeWhite, .excludeBlack], sorting: .frequency, deltaColors: 10, resultLog: false, timeLog: false)

What can I do?

Thanks!

SPM Package.swift missing platforms key

Minimum deployment version for each supported platforms is not set.

So there are compilation errors for iOS.

Showing Recent Issues
/Users/patrickbodet/Library/Developer/Xcode/DerivedData/ShoutRadiosSwift-crcuihvbcexsvcgzdwnxbugtvcxv/SourcePackages/checkouts/DominantColors/Sources/DominantColors/GradientColors.swift:16:30: Type 'CGColor' has no member 'clear'

/Users/patrickbodet/Library/Developer/Xcode/DerivedData/ShoutRadiosSwift-crcuihvbcexsvcgzdwnxbugtvcxv/SourcePackages/checkouts/DominantColors/Sources/DominantColors/GradientColors.swift:18:29: Type 'CGColor' has no member 'clear'

/Users/patrickbodet/Library/Developer/Xcode/DerivedData/ShoutRadiosSwift-crcuihvbcexsvcgzdwnxbugtvcxv/SourcePackages/checkouts/DominantColors/Sources/DominantColors/GradientColors.swift:42:27: 'init(red:green:blue:alpha:)' is only available in iOS 13.0 or newer

/Users/patrickbodet/Library/Developer/Xcode/DerivedData/ShoutRadiosSwift-crcuihvbcexsvcgzdwnxbugtvcxv/SourcePackages/checkouts/DominantColors/Sources/DominantColors/GradientColors.swift:58:24: Type 'CGColor' has no member 'clear'

/Users/patrickbodet/Library/Developer/Xcode/DerivedData/ShoutRadiosSwift-crcuihvbcexsvcgzdwnxbugtvcxv/SourcePackages/checkouts/DominantColors/Sources/DominantColors/GradientColors.swift:62:30: Type 'CGColor' has no member 'clear'

/Users/patrickbodet/Library/Developer/Xcode/DerivedData/ShoutRadiosSwift-crcuihvbcexsvcgzdwnxbugtvcxv/SourcePackages/checkouts/DominantColors/Sources/DominantColors/GradientColors.swift:64:29: Type 'CGColor' has no member 'clear'

/Users/patrickbodet/Library/Developer/Xcode/DerivedData/ShoutRadiosSwift-crcuihvbcexsvcgzdwnxbugtvcxv/SourcePackages/checkouts/DominantColors/Sources/DominantColors/GradientColors.swift:89:27: 'init(red:green:blue:alpha:)' is only available in iOS 13.0 or newer

Thanks

Bad argument name

Version 1.1.6

In PreviewiOS.swift, in refreshColors(from uiImage: UIImage?), calling the DominantColors function with the time argument (line 186) instead of timeLog causes a compilation error.

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.