UIImageColors

repository·master·Indexed 25 days ago

https://github.com/jathu/uiimagecolors

A Swift library for iOS, tvOS, and macOS that extracts dominant and prominent colors from a UIImage or NSImage. It provides synchronous and asynchronous methods to retrieve a UIImageColors struct containing background, primary, secondary, and detail colors, with configurable extraction quality via the UIImageColorsQuality enum.

Tokens
774
Snippets
3
Records
5
Agent score
35%

What's inside UIImageColors

  1. Install UIImageColors

    master

    You can install UIImageColors using Manual, CocoaPods, or Carthage.

    Manual

    Copy UIImageColors.swift directly into your project.

    CocoaPods

    Add the following to your Podfile:

    pod 'UIImageColors'

    Carthage

    Add the following to your Cartfile:

    gitub "jathu/UIImageColors"
    pod 'UIImageColors'
  2. Extract colors from an image

    master

    Use getColors() to extract dominant and prominent colors from a UIImage (iOS/tvOS) or NSImage (macOS). You can use the synchronous method for immediate results or the asynchronous method with a completion handler to avoid blocking the main thread.

    By default, the extraction uses high quality.

    // Asynchronous example
    let image = UIImage(named: "yeezus.png")
    
    image.getColors { colors in
      backgroundView.backgroundColor = colors.background
      mainLabel.textColor = colors.primary
      secondaryLabel.textColor = colors.secondary
      detailLabel.textColor = colors.detail
    }
    
    // Synchronous example
    let colors = UIImage(named: "yeezus.png").getColors()
    
    backgroundView.backgroundColor = colors.background
    mainLabel.textColor = colors.primary
    secondaryLabel.textColor = colors.secondary
    detailLabel.textColor = colors.detail
  3. Configure UIImageColorsQuality

    master

    The UIImageColorsQuality enum controls the downscaling of the original image to balance performance and color accuracy. The default quality is .high.

    Qualities:

    • .lowest: 50px scale. Fastest performance, lowest quality.
    • .low: 100px scale.
    • .high: 250px scale. Default.
    • .highest: No downscaling. Best quality, but very slow.
    public enum UIImageColorsQuality: CGFloat {
        case lowest = 50 // 50px
        case low = 100 // 100px
        case high = 250 // 250px
        case highest = 0 // No scale
    }
  4. Reference Image Methods

    master

    The following methods are available on UIImage and NSImage instances to retrieve UIImageColors:

    • getColors() -> UIImageColors?
    • getColors(quality: ImageColorsQuality) -> UIImageColors?
    • getColors(_ completion: (UIImageColors?) -> Void) -> Void
    • getColors(quality: UIImageColorsQuality, _ completion: (UIImageColors?) -> Void) -> Void
  5. UIImageColors struct

    master

    The UIImageColors struct contains four color properties used to theme UI elements based on the image content. On iOS/tvOS these are UIColor, and on macOS these are NSColor.

    Properties:

    • background: The background color.
    • primary: The primary color.
    • secondary: The secondary color.
    • detail: The detail color.