1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// notcurses::color::channels
//
//!
//
use crate::{
color::{Alpha, Channel, Rgb},
sys::{c_api::NcChannels_u64, NcChannels},
};
/// A pair of both foreground and background [`Channel`]s.
#[derive(Clone, Copy, Default, PartialEq, Eq)]
#[repr(transparent)]
pub struct Channels {
pub nc: NcChannels,
}
mod core_impls {
use super::*;
use core::fmt;
impl fmt::Display for Channels {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (fg, bg) = self.into();
write!(f, "[{fg}, {bg}]")
}
}
impl fmt::Debug for Channels {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (fg, bg) = self.into();
write!(f, "Channels {{ fg: {fg:?}, bg: {bg:?} }}")
}
}
impl From<Channels> for NcChannels {
fn from(c: Channels) -> NcChannels {
c.nc
}
}
impl From<NcChannels> for Channels {
fn from(nc: NcChannels) -> Channels {
Self { nc }
}
}
impl From<NcChannels_u64> for Channels {
fn from(nc_u64: NcChannels_u64) -> Channels {
NcChannels::from(nc_u64).into()
}
}
/// Converts a `Channels` into a (fg, bg) `Channel` tuple.
impl From<Channels> for (Channel, Channel) {
fn from(c: Channels) -> (Channel, Channel) {
(c.fg(), c.bg())
}
}
impl From<&Channels> for (Channel, Channel) {
fn from(c: &Channels) -> (Channel, Channel) {
(c.fg(), c.bg())
}
}
/// Helper for `impl From<{int} | ({int}, …) | [{int; …}]> for Channels`.
macro_rules! impl_from_int_tuple_array {
($( $int:ty ),+) => {
$( impl_from_int_tuple_array![single: $int]; )+
};
(single: $i:ty) => {
/* different fg & bg channels */
impl From<($i, $i, $i, $i, $i, $i)> for Channels {
/// New `Channels` from different foreground & background (r, g, b, r, g, b).
///
/// Performs a saturating cast to `[u8; 3], [u8; 3]`.
fn from(tuple: ($i, $i, $i, $i, $i, $i)) -> Channels {
let fg_arr_u8 = [
tuple.0.clamp(0, <$i>::MAX) as u8,
tuple.1.clamp(0, <$i>::MAX) as u8,
tuple.2.clamp(0, <$i>::MAX) as u8,
];
let bg_arr_u8 = [
tuple.3.clamp(0, <$i>::MAX) as u8,
tuple.4.clamp(0, <$i>::MAX) as u8,
tuple.5.clamp(0, <$i>::MAX) as u8,
];
Self::from_rgb(fg_arr_u8, bg_arr_u8)
}
}
impl From<(($i, $i, $i), ($i, $i, $i))> for Channels {
/// New `Channels` from different foreground & background ((r, g, b), (r, g, b)).
///
/// Performs a saturating cast to `[u8; 3], [u8; 3]`.
fn from(tuple: (($i, $i, $i), ($i, $i, $i))) -> Channels {
Self::from((
tuple.0.0,
tuple.0.1,
tuple.0.2,
tuple.1.0,
tuple.1.1,
tuple.1.2,
))
}
}
impl From<[$i; 6]> for Channels {
/// New `Channels` from different foreground & background [r, g, b, r, g, b]).
///
/// Performs a saturating cast to `[u8; 3], [u8; 3]`.
fn from(arr: [$i; 6]) -> Channels {
Self::from((
arr[0],
arr[1],
arr[2],
arr[3],
arr[4],
arr[5],
))
}
}
impl From<[[$i; 3]; 2]> for Channels {
/// New `Channels` from different foreground & background [[r, g, b], [r, g, b]]).
///
/// Performs a saturating cast to `[u8; 3], [u8; 3]`.
fn from(arr: [[$i; 3]; 2]) -> Channels {
Self::from((
arr[0][0],
arr[0][1],
arr[0][2],
arr[1][0],
arr[1][1],
arr[1][2],
))
}
}
/* same bg & fg channels */
impl From<($i, $i, $i)> for Channels {
/// New `Channels` from same foreground & background (r, g, b).
///
/// Performs a saturating cast to `[u8; 3], [u8; 3]`.
fn from(tuple: ($i, $i, $i)) -> Channels {
let arr_u8 = [
tuple.0.clamp(0, <$i>::MAX) as u8,
tuple.1.clamp(0, <$i>::MAX) as u8,
tuple.2.clamp(0, <$i>::MAX) as u8,
];
Self::from_rgb_both(arr_u8)
}
}
impl From<[$i; 3]> for Channels {
/// New `Channels` from same foreground & background (r, g, b).
///
/// Performs a saturating cast to `[u8; 3], [u8; 3]`.
fn from(arr: [$i; 3]) -> Channels {
Self::from((arr[0], arr[1], arr[2]))
}
}
/* from impl Into<Channels> */
impl From<($i, $i)> for Channels {
/// New `Channels` from a pair of (fg, bg) impl Into<`Channel`>'s.
fn from(tuple: ($i, $i)) -> Channels {
Channels::combine(tuple.0, tuple.1)
}
}
impl From<[$i; 2]> for Channels {
/// New `Channels` from a pair of [fg, bg] impl Into<`Channel`>'s.
fn from(arr: [$i; 2]) -> Channels {
Channels::combine(arr[0], arr[1])
}
}
};
}
impl_from_int_tuple_array!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
}
/// # constructors
impl Channels {
/// A new pair of channels, set to black and NOT using the default colors.
pub fn new() -> Channels {
NcChannels::new().into()
}
/// A new pair of channels set to black and using the default colors.
pub fn with_default() -> Channels {
NcChannels::with_default().into()
}
/// A new pair of channels, using separate foreground and background colors.
pub fn from_rgb(fg_rgb: impl Into<Rgb>, bg_rgb: impl Into<Rgb>) -> Channels {
NcChannels::from_rgb(fg_rgb.into(), bg_rgb.into()).into()
}
/// A new pair of channels, using the same foreground and background colors.
pub fn from_rgb_both(rgb: impl Into<Rgb>) -> Channels {
NcChannels::from_rgb_both(rgb.into()).into()
}
/// A new pair of channels, using separate foreground and background colors,
/// and alpha.
pub fn from_rgb_alpha(
fg_rgb: impl Into<Rgb>,
fg_alpha: impl Into<Alpha>,
bg_rgb: impl Into<Rgb>,
bg_alpha: impl Into<Alpha>,
) -> Channels {
NcChannels::from_rgb_alpha(
fg_rgb.into(),
fg_alpha.into(),
bg_rgb.into(),
bg_alpha.into(),
)
.into()
}
/// A new pair of channels, using the same foreground and background colors.
pub fn from_rgb_alpha_both(rgb: impl Into<Rgb>, alpha: impl Into<Alpha>) -> Channels {
NcChannels::from_rgb_alpha_both(rgb.into(), alpha.into()).into()
}
/// Combines two separate `Channel`s into `Channels`.
pub fn combine(fg: impl Into<Channel>, bg: impl Into<Channel>) -> Channels {
NcChannels::combine(fg.into(), bg.into()).into()
}
}
/// # methods
impl Channels {
/// Gets the foreground channel.
pub fn fg(&self) -> Channel {
self.nc.fchannel().into()
}
/// Gets the background channel.
pub fn bg(&self) -> Channel {
self.nc.bchannel().into()
}
/// Sets the foreground channel.
pub fn set_fg(&mut self, fg: impl Into<Channel>) -> Channels {
self.nc.set_fchannel(fg.into()).into()
}
/// Sets the background channel.
pub fn set_bg(&mut self, bg: impl Into<Channel>) -> Channels {
self.nc.set_fchannel(bg.into()).into()
}
/// Gets the alpha and coloring bits as `Channels`.
pub fn channels(&self) -> Channels {
self.nc.channels().into()
}
/// Sets the alpha and coloring bits from another `Channels`.
pub fn set_channels(&mut self, other: impl Into<Channels>) -> Channels {
self.nc.set_channels(other.into()).into()
}
/// Returns the Channels with the foreground and background's color information
/// swapped, but without touching the rest of the bits.
///
/// Alpha is retained unless it would lead to an illegal state:
/// [`HighContrast`], [`Transparent`] and [`Blend`] are taken to [`Opaque`]
/// unless the new value is Rgb.
///
/// [`HighContrast`]: Alpha#HighContrast
/// [`Transparent`]: Alpha#Transparent
/// [`Blend`]: Alpha#Blend
/// [`Opaque`]: Alpha#Opaque
pub fn reverse(&mut self) -> Self {
self.nc.reverse().into()
}
}