|
| 1 | +# Kolor Agent |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Kolor is a Swift library for working with various color spaces and performing color operations. It allows easy conversion between color spaces, color distance calculations, palette generation, image processing, and color grouping. It includes a large collection of predefined colors useful for design and scientific tasks. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +* **Color Spaces:** Supports sRGB, Display P3, Rec. 2020, ProPhoto RGB, linear RGB, HSV, HSL, HWB, HSI, Okhsv/Okhsl, OkLCh, HPLuv, HSLuv, XYZ, Lab-like, LCh-like, ACES, CMYK, and more. |
| 10 | +* **Conversions:** Convert colors between different color spaces if possible. |
| 11 | +* **Color Distance:** Use `distance(from:)` or deltaE functions for Uniform Color Spaces (UCS) and sRGB. |
| 12 | +* **Palette Generation:** For cylindrical color spaces (Hue channel), generate palettes via `paletteGen(bySteps: Int, saturation: Double, value: Double)`. |
| 13 | +* **Image Processing:** Convert, analyze, and group image colors quickly and easily. |
| 14 | +* **Predefined Color Collections:** A large collection of artistic and scientific colors. |
| 15 | + |
| 16 | +## Design Principles |
| 17 | + |
| 18 | +* Simple and flexible logic for color manipulation. |
| 19 | +* sRGB stores values per channel as `UInt8` (0…255), other color spaces use normalized `Double` (0…1), except cylindrical color spaces where Hue is 0…360. |
| 20 | +* Built-in extensions for `CGImage` and `Image` classes to simplify image operations. |
| 21 | + |
| 22 | +## Usage Examples |
| 23 | + |
| 24 | +```swift |
| 25 | +// Converting a color from sRGB to HSL |
| 26 | +let hslColor = sRGBColor.red.toHSL() |
| 27 | + |
| 28 | +// Generating a palette in HSV |
| 29 | +let palette = hsvColor.paletteGen(bySteps: 12, saturation: 0.8, value: 0.9) |
| 30 | + |
| 31 | +// Calculating distance between two colors |
| 32 | +let distance = color1.distance(from: color2) |
| 33 | + |
| 34 | +// Convert NSImage to array of pixels |
| 35 | +let image = NSImage(named: "cat").toCGImage() |
| 36 | +let pixels = image.toSRGB() |
| 37 | + |
| 38 | +// Quantanize image |
| 39 | +let counts = pixels.makeQuantizedCounts(levels: 4) |
| 40 | +let hist = Histogram(from: counts, levels: 4, totalPixels: pixels.count) |
| 41 | + |
| 42 | +// Identifies the most significant colors |
| 43 | +let colors = extractColorsWithRatio(from: image, maxDepth: 5) |
| 44 | + |
| 45 | +// Posterize image |
| 46 | +let quantized = pixels.map { $0.toQuantized(by: 4) } |
| 47 | +let posterized = CGImage.initFrom(rgb: quantized, width: image.width, height: image.height) |
| 48 | + |
| 49 | +``` |
| 50 | + |
| 51 | +## Notes |
| 52 | + |
| 53 | +* Components and formulas are adapted from multiple sources and rewritten in Swift. |
| 54 | +* Other components translated from C, C++, C#, Python, and JavaScript libraries. |
| 55 | +* Library targets Swift 5.6+. |
0 commit comments