nearest-color 0.1.0

Find the nearest color from a palette by RGB distance, for hex / rgb() / named CSS colors. A faithful port of the nearest-color npm package. Zero dependencies.
Documentation
# nearest-color

[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)

[![crates.io](https://img.shields.io/crates/v/nearest-color.svg)](https://crates.io/crates/nearest-color)
[![docs.rs](https://docs.rs/nearest-color/badge.svg)](https://docs.rs/nearest-color)
[![CI](https://github.com/trananhtung/nearest-color/actions/workflows/ci.yml/badge.svg)](https://github.com/trananhtung/nearest-color/actions/workflows/ci.yml)
[![license](https://img.shields.io/crates/l/nearest-color.svg)](#license)

**Find the nearest color from a palette.**

Given a target color and a list of available colors, `nearest-color` returns the closest
one by Euclidean distance in RGB space — handy for snapping arbitrary colors to a theme
palette, naming colors, or quantizing. Colors are given as hex (`#f00`, `#ff0000`),
`rgb(…)` strings, or any of the 17 standard CSS color names.

A faithful Rust port of the [`nearest-color`](https://www.npmjs.com/package/nearest-color)
npm package.

- **Zero dependencies**
- Differential-tested against the reference `nearest-color` implementation

## Install

```toml
[dependencies]
nearest-color = "0.1"
```

## Usage

Build a reusable matcher from a **named** palette and get rich match info:

```rust
use nearest_color::{Matcher, Rgb};

let palette = Matcher::from_named(&[
    ("maroon", "#800"),
    ("light yellow", "rgb(255, 255, 51)"),
    ("pale blue", "#def"),
    ("white", "fff"),
]).unwrap();

let m = palette.nearest("#f00").unwrap().unwrap();
assert_eq!(m.name.as_deref(), Some("maroon"));
assert_eq!(m.value, "#800");
assert_eq!(m.rgb, Rgb { r: 136, g: 0, b: 0 });
// `m.distance` is the Euclidean RGB distance (≈119.0 here).
```

From an **unnamed** palette (or the built-in rainbow defaults), the match's `name` is
`None` and `value` is the nearest color's source:

```rust
use nearest_color::{Matcher, nearest_color};

let bg = Matcher::from_colors(&["#eee", "#444"]).unwrap();
assert_eq!(bg.nearest("#fff").unwrap().unwrap().value, "#eee");
assert_eq!(bg.nearest("#000").unwrap().unwrap().value, "#444");

// `nearest_color` uses the default rainbow palette.
assert_eq!(nearest_color("#f88").unwrap().value, "#f80");
```

Combine palettes with [`Matcher::or`]:

```rust
use nearest_color::Matcher;
let a = Matcher::from_named(&[("maroon", "#800")]).unwrap();
let b = Matcher::from_colors(&["#444"]).unwrap();
let any = a.or(b);
assert_eq!(any.nearest("#888").unwrap().unwrap().value, "#444");
```

## Color parsing

[`parse_color`] accepts:

- **Hex**`#fff`, `fff`, `#04fbc8` (3 or 6 digits, with or without `#`, any case).
- **`rgb(…)`**`rgb(3, 10, 100)` or `rgb(50%, 0%, 50%)` (percentages are rounded).
- **Standard CSS names** — the 17 in [`STANDARD_COLORS`] `red`, `aqua`, `gray`, .

Anything else returns an error (the matcher builders and `nearest` propagate it).

## Contributors ✨

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
  <tbody>
    <tr>
      <td align="center" valign="top" width="14.28%"><a href="https://github.com/trananhtung"><img src="https://avatars.githubusercontent.com/u/30992229?v=4?s=100" width="100px;" alt="Tung Tran"/><br /><sub><b>Tung Tran</b></sub></a><br /><a href="https://github.com/trananhtung/./commits?author=trananhtung" title="Code">💻</a> <a href="#maintenance-trananhtung" title="Maintenance">🚧</a></td>
    </tr>
  </tbody>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

## License

Licensed under either of [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE) at your option.