Skip to content

Commit 2da6c65

Browse files
committed
💥 Add alpha, rename fields
1 parent 0c02829 commit 2da6c65

File tree

7 files changed

+87
-63
lines changed

7 files changed

+87
-63
lines changed

crates/pycolorsaurus/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ impl Color {
176176
#[new]
177177
fn new(red: u8, green: u8, blue: u8) -> Self {
178178
Self(imp::Color {
179-
r: scale_to_u16(red),
180-
g: scale_to_u16(green),
181-
b: scale_to_u16(blue),
179+
red: scale_to_u16(red),
180+
green: scale_to_u16(green),
181+
blue: scale_to_u16(blue),
182+
alpha: u16::MAX,
182183
})
183184
}
184185

@@ -218,7 +219,7 @@ impl Color {
218219

219220
#[pyo3(name = "__repr__")]
220221
fn repr(&self, python: Python<'_>) -> PyResult<String> {
221-
let (r, g, b) = self.0.scale_to_8bit();
222+
let (r, g, b, _) = self.0.scale_to_8bit();
222223
let ty = type_name::<Self>(&python)?;
223224
Ok(format!("<{ty} #{r:02x}{g:02x}{b:02x}>"))
224225
}

crates/terminal-colorsaurus/examples/bg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use terminal_colorsaurus::{background_color, Error, QueryOptions};
55
fn main() -> Result<(), display::DisplayAsDebug<Error>> {
66
let bg = background_color(QueryOptions::default())?;
77
let bg_8bit = bg.scale_to_8bit();
8-
println!("rgb16({}, {}, {})", bg.r, bg.g, bg.b);
8+
println!("rgb16({}, {}, {})", bg.red, bg.green, bg.blue);
99
println!("rgb8({}, {}, {})", bg_8bit.0, bg_8bit.1, bg_8bit.2);
1010
Ok(())
1111
}

crates/terminal-colorsaurus/examples/fg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use terminal_colorsaurus::{foreground_color, Error, QueryOptions};
55
fn main() -> Result<(), display::DisplayAsDebug<Error>> {
66
let fg = foreground_color(QueryOptions::default())?;
77
let fg_8bit = fg.scale_to_8bit();
8-
println!("rgb16({}, {}, {})", fg.r, fg.g, fg.b);
8+
println!("rgb16({}, {}, {})", fg.red, fg.green, fg.blue);
99
println!("rgb8({}, {}, {})", fg_8bit.0, fg_8bit.1, fg_8bit.2);
1010
Ok(())
1111
}

crates/terminal-colorsaurus/src/color.rs

+66-23
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
#[allow(clippy::exhaustive_structs)]
55
pub struct Color {
66
/// Red
7-
pub r: u16,
7+
pub red: u16,
88
/// Green
9-
pub g: u16,
9+
pub green: u16,
1010
/// Blue
11-
pub b: u16,
11+
pub blue: u16,
12+
/// Can almost always be ignored as it is rarely set to
13+
/// something other than the default (`0xffff`).
14+
pub alpha: u16,
1215
}
1316

1417
impl Color {
@@ -22,29 +25,47 @@ impl Color {
2225
/// let is_dark = color.perceived_lightness() <= 0.5;
2326
/// ```
2427
pub fn perceived_lightness(&self) -> f32 {
25-
let color = xterm_color::Color::rgb(self.r, self.g, self.b);
28+
let color = xterm_color::Color {
29+
red: self.red,
30+
green: self.green,
31+
blue: self.blue,
32+
alpha: self.alpha,
33+
};
2634
color.perceived_lightness()
2735
}
2836

2937
/// Converts the color to 8 bit precision per channel by scaling each channel.
3038
///
3139
/// ```
3240
/// # use terminal_colorsaurus::Color;
33-
/// let white = Color { r: u16::MAX, g: u16::MAX, b: u16::MAX };
34-
/// assert_eq!((u8::MAX, u8::MAX, u8::MAX), white.scale_to_8bit());
41+
/// let white = Color { red: u16::MAX, green: u16::MAX, blue: u16::MAX, alpha: u16::MAX };
42+
/// assert_eq!((u8::MAX, u8::MAX, u8::MAX, u8::MAX), white.scale_to_8bit());
3543
///
36-
/// let black = Color { r: 0, g: 0, b: 0 };
37-
/// assert_eq!((0, 0, 0), black.scale_to_8bit());
44+
/// let black = Color { red: 0, green: 0, blue: 0, alpha: u16::MAX };
45+
/// assert_eq!((0, 0, 0, u8::MAX), black.scale_to_8bit());
3846
/// ```
39-
pub fn scale_to_8bit(&self) -> (u8, u8, u8) {
47+
pub fn scale_to_8bit(&self) -> (u8, u8, u8, u8) {
4048
(
41-
scale_to_u8(self.r),
42-
scale_to_u8(self.g),
43-
scale_to_u8(self.b),
49+
scale_to_u8(self.red),
50+
scale_to_u8(self.green),
51+
scale_to_u8(self.blue),
52+
scale_to_u8(self.alpha),
4453
)
4554
}
4655
}
4756

57+
#[cfg(test)]
58+
impl Color {
59+
pub(crate) const fn rgb(red: u16, green: u16, blue: u16) -> Self {
60+
Self {
61+
red,
62+
green,
63+
blue,
64+
alpha: u16::MAX,
65+
}
66+
}
67+
}
68+
4869
fn scale_to_u8(channel: u16) -> u8 {
4970
(channel as u32 * (u8::MAX as u32) / (u16::MAX as u32)) as u8
5071
}
@@ -53,36 +74,57 @@ fn scale_to_u8(channel: u16) -> u8 {
5374
impl From<Color> for rgb::RGB16 {
5475
fn from(value: Color) -> Self {
5576
rgb::RGB16 {
56-
r: value.r,
57-
g: value.g,
58-
b: value.b,
77+
r: value.red,
78+
g: value.green,
79+
b: value.blue,
80+
}
81+
}
82+
}
83+
84+
#[cfg(feature = "rgb")]
85+
impl From<Color> for rgb::RGBA16 {
86+
fn from(value: Color) -> Self {
87+
rgb::RGBA16 {
88+
r: value.red,
89+
g: value.green,
90+
b: value.blue,
91+
a: value.alpha,
5992
}
6093
}
6194
}
6295

6396
#[cfg(feature = "rgb")]
6497
impl From<Color> for rgb::RGB8 {
6598
fn from(value: Color) -> Self {
66-
let (r, g, b) = value.scale_to_8bit();
99+
let (r, g, b, _) = value.scale_to_8bit();
67100
rgb::RGB8 { r, g, b }
68101
}
69102
}
70103

104+
#[cfg(feature = "rgb")]
105+
impl From<Color> for rgb::RGBA8 {
106+
fn from(value: Color) -> Self {
107+
let (r, g, b, a) = value.scale_to_8bit();
108+
rgb::RGBA8 { r, g, b, a }
109+
}
110+
}
111+
71112
#[cfg(feature = "rgb")]
72113
impl From<rgb::RGB16> for Color {
73114
fn from(value: rgb::RGB16) -> Self {
74115
Color {
75-
r: value.r,
76-
g: value.g,
77-
b: value.b,
116+
red: value.r,
117+
green: value.g,
118+
blue: value.b,
119+
alpha: u16::MAX,
78120
}
79121
}
80122
}
81123

82124
#[cfg(feature = "anstyle")]
83125
impl From<Color> for anstyle::RgbColor {
84126
fn from(value: Color) -> Self {
85-
let (r, g, b) = value.scale_to_8bit();
127+
let (r, g, b, _) = value.scale_to_8bit();
86128
anstyle::RgbColor(r, g, b)
87129
}
88130
}
@@ -100,9 +142,10 @@ mod tests {
100142
#[test]
101143
fn white_has_perceived_lightness_100() {
102144
let white = Color {
103-
r: u16::MAX,
104-
g: u16::MAX,
105-
b: u16::MAX,
145+
red: u16::MAX,
146+
green: u16::MAX,
147+
blue: u16::MAX,
148+
alpha: u16::MAX,
106149
};
107150
assert_eq!(1.0, white.perceived_lightness())
108151
}

crates/terminal-colorsaurus/src/color_scheme_tests.rs

+6-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
use super::*;
22
use ColorScheme::*;
33

4-
const BLACK: Color = Color { r: 0, g: 0, b: 0 };
5-
const WHITE: Color = Color {
6-
r: u16::MAX,
7-
g: u16::MAX,
8-
b: u16::MAX,
9-
};
10-
const DARK_GRAY: Color = Color {
11-
r: 0x44ff,
12-
g: 0x44ff,
13-
b: 0x44ff,
14-
};
15-
const DARKER_GRAY: Color = Color {
16-
r: 0x22ff,
17-
g: 0x22ff,
18-
b: 0x22ff,
19-
};
20-
const LIGHT_GRAY: Color = Color {
21-
r: 0xccff,
22-
g: 0xccff,
23-
b: 0xccff,
24-
};
25-
const LIGHTER_GRAY: Color = Color {
26-
r: 0xeeff,
27-
g: 0xeeff,
28-
b: 0xeeff,
29-
};
4+
const BLACK: Color = Color::rgb(0, 0, 0);
5+
const WHITE: Color = Color::rgb(u16::MAX, u16::MAX, u16::MAX);
6+
const DARK_GRAY: Color = Color::rgb(0x44ff, 0x44ff, 0x44ff);
7+
const DARKER_GRAY: Color = Color::rgb(0x22ff, 0x22ff, 0x22ff);
8+
const LIGHT_GRAY: Color = Color::rgb(0xccff, 0xccff, 0xccff);
9+
const LIGHTER_GRAY: Color = Color::rgb(0xeeff, 0xeeff, 0xeeff);
3010

3111
mod dark {
3212
use super::*;

crates/terminal-colorsaurus/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//! use terminal_colorsaurus::{foreground_color, QueryOptions};
3333
//!
3434
//! let fg = foreground_color(QueryOptions::default()).unwrap();
35-
//! println!("rgb({}, {}, {})", fg.r, fg.g, fg.b);
35+
//! println!("rgb({}, {}, {})", fg.red, fg.green, fg.blue);
3636
//! ```
3737
//!
3838
//! ## Optional Dependencies

crates/terminal-colorsaurus/src/xterm.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ fn parse_response(response: Vec<u8>, prefix: &[u8]) -> Result<Color> {
7979
}
8080

8181
fn xparsecolor(input: &[u8]) -> Option<Color> {
82-
let xterm_color::Color {
83-
red: r,
84-
green: g,
85-
blue: b,
86-
..
87-
} = xterm_color::Color::parse(input).ok()?;
88-
Some(Color { r, g, b })
82+
let color = xterm_color::Color::parse(input).ok()?;
83+
Some(Color {
84+
red: color.red,
85+
green: color.green,
86+
blue: color.blue,
87+
alpha: color.alpha,
88+
})
8989
}
9090

9191
type Reader<'a> = BufReader<TermReader<RawModeGuard<'a>>>;

0 commit comments

Comments
 (0)