Swift package · v0.1.2

KLAdaptivePalette

Derive a palette from the image region visible in a host container.

A Core Graphics palette analyzer that accounts for the host container, content mode, and crop anchor. It returns colors, ranked candidates, contrast-aware foregrounds, and a deterministic cache key.

Platforms
iOS 17+ · macOS 14+
Toolchain
Swift 6.0+
License
MIT
Dependencies
None

Functions

  • Aspect-fill and aspect-fit visible-region analysis
  • Ranked perceptual color candidates
  • Foreground selection from actual background luminance
  • Per-instance in-memory palette cache

Not included

It analyzes CGImage pixels. Image loading, task scheduling, persistence, and host view composition remain the integrating application's responsibility.

Install with SwiftPM

Package.swiftSwift
.package(url: "https://github.com/KoenLee1023/KLAdaptivePalette.git", from: "0.1.2")

Usage

Integration exampleSwift
let request = KLPaletteRequest(
    image: image,
    cacheID: mediaID,
    containerSize: size
)
let palette = KLPaletteAnalyzer().analyze(request)
KLPaletteAnalyzer

Analyzes a KLPaletteRequest and produces a deterministic cache key for equal pixels and display geometry.

KLPaletteRequest

Carries a CGImage, cache ID, container size, content mode, and normalized crop anchor.

KLAdaptivePalette

Returns source, candidate, tint, background, and foreground colors for the visible region.

Demo apps

PaletteInspector

Inspects the visible crop, candidates, confidence, and cache key while changing display geometry.

AdaptiveSurface

Shows light and dark surfaces using foregrounds selected from the returned background colors.