Skip to content

Commit 2c60028

Browse files
committed
🔥 Remove public xparsecolor from colorsaurus
1 parent b49e903 commit 2c60028

File tree

7 files changed

+35
-307
lines changed

7 files changed

+35
-307
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/terminal-colorsaurus/Cargo.toml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,24 @@ edition = "2021"
1111
rust-version = "1.70.0" # Search for `FIXME(msrv)` when bumping.
1212
exclude = [".github", ".gitignore", "*.sh", "benchmark/**/*", "doc/issues.md", "deny.toml"]
1313

14-
[features]
15-
default = ["query"]
16-
query = ["dep:memchr", "dep:mio", "dep:terminal-trx", "dep:libc", "dep:windows-sys"]
17-
1814
[dependencies]
1915
rgb = { version = "0.8.37", optional = true }
2016
anstyle = { version = "1.0.7", optional = true }
2117
cfg-if = "1.0.0"
18+
xterm-color = { path = "../xterm-color", version = "1.0" }
2219

2320
[target.'cfg(any(unix, windows))'.dependencies]
24-
memchr = { version = "2.7.1", optional = true }
25-
terminal-trx = { version = "0.2.3", optional = true }
21+
memchr = "2.7.1"
22+
terminal-trx = "0.2.3"
2623

2724
[target.'cfg(unix)'.dependencies]
28-
mio = { version = "1", features = ["os-ext"], default-features = false, optional = true }
25+
mio = { version = "1", features = ["os-ext"], default-features = false }
2926

3027
[target.'cfg(target_os = "macos")'.dependencies]
31-
libc = { version = "0.2.151", optional = true }
28+
libc = "0.2.151"
3229

3330
[target.'cfg(windows)'.dependencies]
34-
windows-sys = { version = "0.59.0", features = ["Win32_System_Threading"], optional = true } # Keep this in sync with terminal-trx's version to avoid duplicate deps.
31+
windows-sys = { version = "0.59.0", features = ["Win32_System_Threading"] } # Keep this in sync with terminal-trx's version to avoid duplicate deps.
3532

3633
[lints]
3734
workspace = true

crates/terminal-colorsaurus/src/color.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Color {
2121
/// let is_dark = color.perceived_lightness() <= 50;
2222
/// ```
2323
pub fn perceived_lightness(&self) -> u8 {
24-
luminance_to_perceived_lightness(luminance(self))
24+
(self.perceived_lightness_f32() * 100.) as u8
2525
}
2626

2727
/// Converts the color to 8 bit precision per channel by scaling each channel.
@@ -41,6 +41,11 @@ impl Color {
4141
scale_to_u8(self.b),
4242
)
4343
}
44+
45+
pub(crate) fn perceived_lightness_f32(&self) -> f32 {
46+
let color = xterm_color::Color::rgb(self.r, self.g, self.b);
47+
color.perceived_lightness()
48+
}
4449
}
4550

4651
fn scale_to_u8(channel: u16) -> u8 {
@@ -85,34 +90,6 @@ impl From<Color> for anstyle::RgbColor {
8590
}
8691
}
8792

88-
// Implementation of determining the perceived lightness
89-
// follows this excellent answer: https://stackoverflow.com/a/56678483
90-
91-
fn srgb_to_lin(channel: f64) -> f64 {
92-
if channel < 0.04045 {
93-
channel / 12.92
94-
} else {
95-
((channel + 0.055) / 1.055).powf(2.4)
96-
}
97-
}
98-
99-
// Luminance (Y)
100-
fn luminance(color: &Color) -> f64 {
101-
let r = f64::from(color.r) / f64::from(u16::MAX);
102-
let g = f64::from(color.g) / f64::from(u16::MAX);
103-
let b = f64::from(color.b) / f64::from(u16::MAX);
104-
0.2126 * srgb_to_lin(r) + 0.7152 * srgb_to_lin(g) + 0.0722 * srgb_to_lin(b)
105-
}
106-
107-
// Perceptual lightness (L*)
108-
fn luminance_to_perceived_lightness(luminance: f64) -> u8 {
109-
if luminance < 216. / 24389. {
110-
(luminance * (24389. / 27.)) as u8
111-
} else {
112-
(luminance.powf(1. / 3.) * 116. - 16.) as u8
113-
}
114-
}
115-
11693
#[cfg(test)]
11794
mod tests {
11895
use super::*;

crates/terminal-colorsaurus/src/color_scheme_tests.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ mod dark {
5454
#[test]
5555
fn fg_and_bg_both_dark() {
5656
for (foreground, background) in [(DARK_GRAY, DARKER_GRAY), (DARKER_GRAY, BLACK)] {
57-
assert!(foreground.perceived_lightness() < PERCEPTUAL_MIDDLE_GRAY);
58-
assert!(background.perceived_lightness() < PERCEPTUAL_MIDDLE_GRAY);
59-
assert!(foreground.perceived_lightness() != background.perceived_lightness());
57+
assert!(foreground.perceived_lightness_f32() < 0.5);
58+
assert!(background.perceived_lightness_f32() < 0.5);
59+
assert!(foreground.perceived_lightness_f32() != background.perceived_lightness_f32());
6060

6161
let palette = ColorPalette {
6262
foreground,
@@ -93,9 +93,12 @@ mod light {
9393
#[test]
9494
fn fg_and_bg_both_light() {
9595
for (foreground, background) in [(LIGHT_GRAY, LIGHTER_GRAY), (LIGHTER_GRAY, WHITE)] {
96-
assert!(foreground.perceived_lightness() > PERCEPTUAL_MIDDLE_GRAY);
97-
assert!(background.perceived_lightness() > PERCEPTUAL_MIDDLE_GRAY);
98-
assert!(foreground.perceived_lightness() != background.perceived_lightness());
96+
assert!(foreground.perceived_lightness_f32() > 0.5);
97+
assert!(background.perceived_lightness_f32() > 0.5);
98+
assert!(
99+
(foreground.perceived_lightness_f32() - background.perceived_lightness_f32()).abs()
100+
>= f32::EPSILON
101+
);
99102

100103
let palette = ColorPalette {
101104
foreground,

crates/terminal-colorsaurus/src/lib.rs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,6 @@ mod color;
4545
mod error;
4646
mod fmt;
4747

48-
/// Low-level utilities for parsing responses to `OSC 10` / `OSC 11` queries.
49-
///
50-
/// **Hint:** If you are only using this module, then you should probably disable
51-
/// the default features to avoid unnecessary dependencies:
52-
///
53-
/// ```toml
54-
/// [dependencies]
55-
/// terminal-colorsaurus = { version = "...", default-features = false }
56-
/// ```
57-
pub mod parse {
58-
pub use crate::xparsecolor::xparsecolor;
59-
}
60-
61-
mod xparsecolor;
62-
63-
#[cfg(feature = "query")]
6448
cfg_if! {
6549
if #[cfg(all(any(unix, windows), not(terminal_colorsaurus_test_unsupported)))] {
6650
mod io;
@@ -101,7 +85,6 @@ pub use color::*;
10185

10286
/// The color palette i.e. foreground and background colors of the terminal.
10387
/// Retrieved by calling [`color_palette`].
104-
#[cfg(feature = "query")]
10588
#[derive(Debug, Clone, PartialEq, Eq)]
10689
#[non_exhaustive]
10790
pub struct ColorPalette {
@@ -115,7 +98,6 @@ pub struct ColorPalette {
11598
///
11699
/// The easiest way to retrieve the color scheme
117100
/// is by calling [`color_scheme`].
118-
#[cfg(feature = "query")]
119101
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
120102
#[allow(clippy::exhaustive_enums)]
121103
#[doc(alias = "Theme")]
@@ -127,18 +109,14 @@ pub enum ColorScheme {
127109
Light,
128110
}
129111

130-
#[cfg(feature = "query")]
131-
const PERCEPTUAL_MIDDLE_GRAY: u8 = 50;
132-
133-
#[cfg(feature = "query")]
134112
impl ColorPalette {
135113
/// Determines if the terminal uses a dark or light background.
136114
pub fn color_scheme(&self) -> ColorScheme {
137-
let fg = self.foreground.perceived_lightness();
138-
let bg = self.background.perceived_lightness();
115+
let fg = self.foreground.perceived_lightness_f32();
116+
let bg = self.background.perceived_lightness_f32();
139117
if bg < fg {
140118
ColorScheme::Dark
141-
} else if bg > fg || bg > PERCEPTUAL_MIDDLE_GRAY {
119+
} else if bg > fg || bg > 0.5 {
142120
ColorScheme::Light
143121
} else {
144122
ColorScheme::Dark
@@ -152,7 +130,6 @@ pub use error::Error;
152130

153131
/// Options to be used with [`foreground_color`] and [`background_color`].
154132
/// You should almost always use the unchanged [`QueryOptions::default`] value.
155-
#[cfg(feature = "query")]
156133
#[derive(Debug, Clone, PartialEq, Eq)]
157134
#[non_exhaustive]
158135
pub struct QueryOptions {
@@ -168,7 +145,6 @@ pub struct QueryOptions {
168145
pub timeout: std::time::Duration,
169146
}
170147

171-
#[cfg(feature = "query")]
172148
impl Default for QueryOptions {
173149
fn default() -> Self {
174150
Self {
@@ -178,23 +154,20 @@ impl Default for QueryOptions {
178154
}
179155

180156
/// Detects if the terminal is dark or light.
181-
#[cfg(feature = "query")]
182157
#[doc = include_str!("../doc/caveats.md")]
183158
#[doc(alias = "theme")]
184159
pub fn color_scheme(options: QueryOptions) -> Result<ColorScheme> {
185160
color_palette(options).map(|p| p.color_scheme())
186161
}
187162

188163
/// Queries the terminal for it's color scheme (foreground and background color).
189-
#[cfg(feature = "query")]
190164
#[doc = include_str!("../doc/caveats.md")]
191165
pub fn color_palette(options: QueryOptions) -> Result<ColorPalette> {
192166
imp::color_palette(options)
193167
}
194168

195169
/// Queries the terminal for it's foreground color. \
196170
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
197-
#[cfg(feature = "query")]
198171
#[doc = include_str!("../doc/caveats.md")]
199172
#[doc(alias = "fg")]
200173
pub fn foreground_color(options: QueryOptions) -> Result<Color> {
@@ -203,7 +176,6 @@ pub fn foreground_color(options: QueryOptions) -> Result<Color> {
203176

204177
/// Queries the terminal for it's background color. \
205178
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
206-
#[cfg(feature = "query")]
207179
#[doc = include_str!("../doc/caveats.md")]
208180
#[doc(alias = "bg")]
209181
pub fn background_color(options: QueryOptions) -> Result<Color> {

0 commit comments

Comments
 (0)