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
//! `NcChannel*`

// -----------------------------------------------------------------------------
// - The channel components are u8 instead of u32.
//   Because of type enforcing, some runtime checks are now unnecessary.
//
// - None of the functions can't fail now. The original checks for dirty bits
//   have been substitued by mask cleaning (bitwise and)
//
// - These functions were deemed unnecessary to implement:
//   - `channel_set_rgb_clipped()`
//   - `channels_set_fg_rgb8_clipped()`
//   - `channels_set_bg_rgb8_clipped()`
// -----------------------------------------------------------------------------
//
// functions manually reimplemented: 46
// ------------------------------------------
// (X) wont:  9
// (+) done: 39
// (#) test: 21
// (W) wrap: 37
// ------------------------------------------
//W# channel_alpha
//W# channel_b
//W# channel_default_p
//W# channel_g
//W# channel_palindex_p
//W# channel_r
// # channel_rgb8                    // unneeded method
//W# channel_set
//W# channel_set_alpha
//W# channel_set_default
//W# channel_set_not_default         // not in the original C API
// # channel_set_rgb8                // unneeded method
// X channel_set_rgb_clipped         // not needed
//W# channels_bchannel
//W+ channels_bg_alpha
//W+ channels_bg_default_p
//W# channels_bg_palindex_p
//W+ channels_bg_rgb
// + channels_bg_rgb8                // unneeded method
//W+ channels_channels
//W# channels_combine
//W# channels_fchannel
//W+ channels_fg_alpha
//W+ channels_fg_default_p
//W# channels_fg_palindex_p
//W+ channels_fg_rgb
// + channels_fg_rgb8                // unneeded method
//W# channels_set_bchannel
//W+ channels_set_bg_alpha
//W+ channels_set_bg_default
//W  channels_set_bg_not_default     // not in the original C API
//W# channels_set_bg_palindex
//W+ channels_set_bg_rgb
// + channels_set_bg_rgb8            // unneeded method
// X channels_set_bg_rgb8_clipped    // not needed
//W+ channels_set_channels
//W  channels_set_default            // not in the original C API
//W# channels_set_fchannel
//W+ channels_set_fg_alpha
//W+ channels_set_fg_default
//W  channels_set_fg_not_default     // not in the original C API
//W# channels_set_fg_palindex
//W+ channels_set_fg_rgb
// + channels_set_fg_rgb8            // unneeded method
// X channels_set_fg_rgb8_clipped    // not needed
//W  channels_set_not_default        // not in the original C API

#[cfg(test)]
mod test;

#[allow(clippy::module_inception)]
mod channel;
mod channels;
pub(crate) mod reimplemented;
pub use {channel::NcChannel, channels::NcChannels};

pub(crate) mod c_api {
    use crate::c_api::ffi;

    #[allow(unused_imports)]
    use crate::{NcAlpha, NcChannel, NcChannels};

    /// 32 bits of context-dependent info containing [`NcRgb_u32`]
    /// + [`NcAlpha_u32`] + extra
    ///
    /// It's recommended to use [`NcChannel`][crate::NcChannel] instead.
    ///
    /// It is composed of:
    /// - a 24-bit [`NcRgb_u32`] value
    /// - plus 8 bits divided in:
    ///   - 2 bits of [`NcAlpha_u32`]
    ///   - 6 bits of context-dependent info
    ///
    /// The context details are documented in [`NcChannels_u64`].
    ///
    /// ## Diagram
    ///
    /// ```txt
    /// ~~AA~~~~ RRRRRRRR GGGGGGGG BBBBBBBB
    /// ```
    /// `type in C: channel (uint32_t)`
    ///
    /// # See also
    /// - [`NcRgb_u32`]
    /// - [`NcRgba_u32`]
    ///
    /// [`NcRgb_u32`]: crate::c_api::NcRgb_u32
    /// [`NcRgba_u32`]: crate::c_api::NcRgba_u32
    /// [`NcAlpha_u32`]: crate::c_api::NcAlpha_u32
    pub type NcChannel_u32 = u32;

    /// 64 bits containing a foreground and background [`NcChannel_u32`].
    ///
    /// It's recommended to use [`NcChannels`][crate::NcChannels] instead.
    ///
    /// At render time, both 24-bit [`NcRgb_u32`] values are quantized down to
    /// terminal capabilities, if necessary. There's a clear path to 10-bit
    /// support should we one day need it.
    ///
    /// ## Default Color
    ///
    /// The "default color" is best explained by [color(3NCURSES)][0] and
    /// [default_colors(3NCURSES)][1]. Ours is the same concept.
    ///
    /// [0]: https://manpages.debian.org/stretch/ncurses-doc/color.3ncurses.en.html
    /// [1]: https://manpages.debian.org/stretch/ncurses-doc/default_colors.3ncurses.en.html
    ///
    /// **Until the "not default color" bit is set, any color you load will be ignored.**
    ///
    /// ## Diagram
    ///
    /// ```txt
    /// ~~AA~~~~|RRRRRRRR|GGGGGGGG|BBBBBBBB║~~AA~~~~|RRRRRRRR|GGGGGGGG|BBBBBBBB
    /// ↑↑↑↑↑↑↑↑↑↑↑↑ foreground ↑↑↑↑↑↑↑↑↑↑↑║↑↑↑↑↑↑↑↑↑↑↑↑ background ↑↑↑↑↑↑↑↑↑↑↑
    /// ```
    ///
    /// Detailed info (specially on the context-dependent bits on each
    /// [`NcChannel_u32`]'s 4th byte):
    ///
    /// ```txt
    ///                             ~foreground channel~
    /// reserved, must be 0                                  ↓bits view↓               ↓hex mask↓
    /// 0·······|········|········|········║········|········|········|········  =  8·······|········
    ///
    /// NcChannels::FG_DEFAULT_MASK: foreground is NOT "default color"
    /// ·1······|········|········|········║········|········|········|········  =  4·······|········
    ///
    /// NcChannels::FG_ALPHA_MASK: foreground alpha (2bits)
    /// ··11····|········|········|········║········|········|········|········  =  3·······|········
    ///
    /// NcChannels::FG_PALETTE: foreground uses palette index
    /// ····1···|········|········|········║········|········|········|········  =  ·8······|········
    ///
    /// NcChannels::NOBACKGROUND_MASK: glyph is entirely foreground
    /// ·····1··|········|········|········║········|········|········|········  =  ·4······|········
    ///
    /// reserved, must be 0
    /// ······00|········|········|········║········|········|········|········  =  ·3······|········
    ///
    /// NcChannels::FG_RGB_MASK: foreground in 3x8 RGB (rrggbb)
    /// ········|11111111|11111111|11111111║········|········|········|········  =  ··FFFFFF|········
    /// ```
    ///
    /// ```txt
    ///                             ~background channel~
    /// reserved, must be 0                                  ↓bits view↓               ↓hex mask↓
    /// ········|········|········|········║0·······|········|········|········  =  ········|8·······
    ///
    /// NcChannels::BGDEFAULT_MASK: background is NOT "default color"
    /// ········|········|········|········║·1······|········|········|········  =  ········|4·······
    ///
    /// NcChannels::BG_ALPHA_MASK: background alpha (2 bits)
    /// ········|········|········|········║··11····|········|········|········  =  ········|3·······
    ///
    /// NcChannels::BG_PALETTE: background uses palette index
    /// ········|········|········|········║····1···|········|········|········  =  ········|·8······
    ///
    /// reserved, must be 0
    /// ········|········|········|········║·····000|········|········|········  =  ········|·7······
    ///
    /// NcChannels::BG_RGB_MASK: background in 3x8 RGB (rrggbb)
    /// ········|········|········|········║········|11111111|11111111|11111111  =  ········|··FFFFFF
    /// ```
    /// `type in C: channels (uint64_t)`
    ///
    /// ## `NcChannels` Mask Flags
    ///
    /// - [`NcChannels::BG_DEFAULT_MASK`][NcChannels#associatedconstant.BGDEFAULT_MASK]
    /// - [`NcChannels::BG_ALPHA_MASK`][NcChannels#associatedconstant.BG_ALPHA_MASK]
    /// - [`NcChannels::BG_PALETTE`][NcChannels#associatedconstant.BG_PALETTE]
    /// - [`NcChannels::BG_RGB_MASK`][NcChannels#associatedconstant.BG_RGB_MASK]
    /// - [`NcChannels::FG_DEFAULT_MASK`][NcChannels#associatedconstant.FGDEFAULT_MASK]
    /// - [`NcChannels::FG_ALPHA_MASK`][NcChannels#associatedconstant.FG_ALPHA_MASK]
    /// - [`NcChannels::FG_PALETTE`][NcChannels#associatedconstant.FG_PALETTE]
    /// - [`NcChannels::FG_RGB_MASK`][NcChannels#associatedconstant.FG_RGB_MASK]
    ///
    /// [`NcRgb_u32`]: crate::c_api::NcRgb_u32
    /// [`NcChannel_u32`]: crate::c_api::NcChannel_u32
    ///
    pub type NcChannels_u64 = u64;

    /// If this bit is set, we are *not* using the default background color
    ///
    /// Note: This is equivalent to
    /// [`NcChannel::DEFAULT_MASK`][NcChannel#associatedconstant.DEFAULT_MASK]
    ///
    /// See the detailed diagram at [`NcChannels`]
    pub const NC_BGDEFAULT_MASK: u32 = ffi::NC_BGDEFAULT_MASK;

    /// Extract these bits to get the (background) [`NcAlpha`] mask.
    ///
    /// Note: This is equivalent to
    /// [`NcChannel::ALPHA_MASK`][NcChannel#associatedconstant.ALPHA_MASK]
    ///
    /// See the detailed diagram at [`NcChannels`]
    pub const NC_BG_ALPHA_MASK: u32 = ffi::NC_BG_ALPHA_MASK;

    /// If this bit *and* [`NC_BGDEFAULT_MASK`] are set, we're using a
    /// palette-indexed background color
    ///
    /// Note: This is equivalent to
    /// [`NcChannel::PALETTE_MASK`][NcChannel#associatedconstant.PALETTE_MASK]
    ///
    /// See the detailed diagram at [`NcChannels`]
    pub const NC_BG_PALETTE: u32 = ffi::NC_BG_PALETTE;

    /// Extract these bits to get the background [`NcRgb`][crate::NcRgb] value
    ///
    /// Note: This is equivalent to
    /// [`NcChannel::RGB_MASK`][NcChannel#associatedconstant.RGB_MASK]
    ///
    /// See the detailed diagram at [`NcChannels`]
    pub const NC_BG_RGB_MASK: u32 = ffi::NC_BG_RGB_MASK;

    /// Does this glyph completely obscure the background?
    ///
    /// If so, there's no need to emit a background when rasterizing,
    /// a small optimization. These are also used to track regions into which
    /// we must not cellblit.
    pub const NC_NOBACKGROUND_MASK: u64 = ffi::NC_NOBACKGROUND_MASK as u64;
}