diff --git a/Cargo.lock b/Cargo.lock index 7fb2a9f..a720593 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -261,6 +261,7 @@ dependencies = [ name = "terminal-colorsaurus" version = "0.4.2" dependencies = [ + "anstyle", "libc", "memchr", "mio", diff --git a/Cargo.toml b/Cargo.toml index 1576eca..26199fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ exclude = [".github", ".gitignore", "*.sh", "benchmark/**/*"] [dependencies] rgb = { version = "0.8.37", optional = true } +anstyle = { version = "1.0.7", optional = true } [target.'cfg(unix)'.dependencies] memchr = "2.7.1" @@ -39,7 +40,7 @@ unwrap_used = "deny" use_debug = "warn" [package.metadata.docs.rs] -features = ["docs", "rgb"] +features = ["docs", "rgb", "anstyle"] rustdoc-args = ["--cfg", "docsrs"] [workspace] diff --git a/changelog.md b/changelog.md index a586693..64647ab 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,7 @@ # Changelog ## 0.4.2 +* ✨ Add optional dependency on `anstyle` to enable conversions from `Color` to `anstyle::RgbColor`. +* ✨ Add conversion from `Color` to `rgb::RGB8`. * Add `keywords` to package metadata. * Remove dependency on `thiserror`. diff --git a/src/color.rs b/src/color.rs index 7e30866..8623f10 100644 --- a/src/color.rs +++ b/src/color.rs @@ -58,6 +58,14 @@ impl From for rgb::RGB16 { } } +#[cfg(feature = "rgb")] +impl From for rgb::RGB8 { + fn from(value: Color) -> Self { + let (r, g, b) = value.scale_to_8bit(); + rgb::RGB8 { r, g, b } + } +} + #[cfg(feature = "rgb")] impl From for Color { fn from(value: rgb::RGB16) -> Self { @@ -69,6 +77,14 @@ impl From for Color { } } +#[cfg(feature = "anstyle")] +impl From for anstyle::RgbColor { + fn from(value: Color) -> Self { + let (r, g, b) = value.scale_to_8bit(); + anstyle::RgbColor(r, g, b) + } +} + // Implementation of determining the perceived lightness // follows this excellent answer: https://stackoverflow.com/a/56678483 diff --git a/src/lib.rs b/src/lib.rs index bf6e673..7792c0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,7 +75,8 @@ //! //! //! ## Optional Dependencies -//! * [`rgb`] — Enable this feature to convert between [`Color`] and [`rgb::RGB16`]. +//! * [`rgb`] — Enable this feature to convert between [`Color`] and [`rgb::RGB16`] / [`rgb::RGB8`]. +//! * [`anstyle`] — Enable this feature to convert [`Color`] to [`anstyle::RgbColor`]. //! //! ## Comparison with Other Crates //! ### [termbg]