Skip to content

Commit

Permalink
Add new conversions for color
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed May 30, 2024
1 parent 75adbae commit e4471b6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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`.

Expand Down
16 changes: 16 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ impl From<Color> for rgb::RGB16 {
}
}

#[cfg(feature = "rgb")]
impl From<Color> 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<rgb::RGB16> for Color {
fn from(value: rgb::RGB16) -> Self {
Expand All @@ -69,6 +77,14 @@ impl From<rgb::RGB16> for Color {
}
}

#[cfg(feature = "anstyle")]
impl From<Color> 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

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
//! </details>
//!
//! ## 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]
Expand Down

0 comments on commit e4471b6

Please sign in to comment.