color-name-list
Version:
long list of color names
59 lines (42 loc) • 1.71 kB
Markdown
Swift support is provided by
[](https://github.com/yonilevy/swift-color-names).
It bundles this dataset and adds closest-named-color search in the perceptual
[](https://bottosson.github.io/posts/oklab/) space.
Add the package with Swift Package Manager:
```swift
.package(url: "https://github.com/yonilevy/swift-color-names.git", from: "1.0.0")
```
```swift
.target(name: "MyApp", dependencies: [
.product(name: "ColorNames", package: "swift-color-names"),
])
```
Pick which slice of the dataset to load — `.full`, `.bestOf`, or `.short`
(the `.bestOf` names limited to 12 characters or fewer):
```swift
import ColorNames
let colorNames = ColorNames(.short)
```
```swift
// Closest name to an sRGB value…
let fromRgb = colorNames.find(rgb: RGB(red255: 224, green255: 224, blue255: 255))
fromRgb?.name // "Velvet Scarf"
// …or straight from a hex string:
let fromHex = colorNames.find(hex: "#facfea")
fromHex?.name // "Puff of Pink"
// The result carries name, sRGB, precomputed OKLab, and hex — note the hex is
// the matched color's, not your input:
fromHex?.hex // "#FFCBEE"
// Exact lookups (no distance search):
colorNames.color(named: "Red")?.hex // "#FF0000"
colorNames.color(hex: "#000000")?.name // "Black"
```
Search runs by brute force over the chosen list in OKLab space, so numeric
closeness matches perceived closeness. `find(…)` returns `nil` only when the
database is empty (or the hex string is invalid).
See the upstream project for more details and updates:
[yonilevy/swift-color-names](https://github.com/yonilevy/swift-color-names)