libnotcurses_sys/alpha.rs
1//! NcAlpha
2
3#[allow(unused_imports)] // for doc comments
4use crate::NcCell;
5
6/// Alpha information, part of an [`NcChannel`][crate::NcChannel],
7/// applies to [`NcCell`]'s foreground or background color.
8///
9/// # Default:
10/// *[`NcAlpha::Opaque`]*
11///
12/// ## Diagram
13///
14/// Internally it's 2 bits of alpha, surrounded by context dependent bits:
15///
16/// ```txt
17/// ~~AA~~~~ -------- -------- --------
18/// ```
19///
20/// See also: [`NcChannels`][crate::NcChannels] for more context information.
21#[repr(u32)]
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum NcAlpha {
24 /// Indicates [`NcCell`]'s foreground or background color will be a
25 /// composite between its color and the `NcCell`s' corresponding colors
26 /// underneath it.
27 Blend = c_api::NCALPHA_BLEND,
28
29 /// Indicates the foreground color will be high-contrast,
30 /// relative to the computed background.
31 ///
32 /// Background cannot be high-contrast.
33 HighContrast = c_api::NCALPHA_HIGHCONTRAST,
34
35 /// Indicates [`NcCell`]'s foreground or background color is used unchanged.
36 Opaque = c_api::NCALPHA_OPAQUE,
37
38 /// Indicates [`NcCell`]'s foreground or background color is derived
39 /// entirely from the `NcCell`s underneath it.
40 Transparent = c_api::NCALPHA_TRANSPARENT,
41}
42
43mod core_impls {
44 use super::{c_api, NcAlpha};
45 use core::fmt;
46
47 impl Default for NcAlpha {
48 fn default() -> Self {
49 Self::Opaque
50 }
51 }
52
53 impl fmt::Display for NcAlpha {
54 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55 use NcAlpha::*;
56 write!(
57 f,
58 "{}",
59 match self {
60 Blend => "Blend",
61 HighContrast => "HighContrast",
62 Opaque => "Opaque",
63 Transparent => "Transparent",
64 }
65 )
66 }
67 }
68
69 impl From<c_api::NcAlpha_u32> for NcAlpha {
70 fn from(alpha: c_api::NcAlpha_u32) -> Self {
71 use {c_api::*, NcAlpha::*};
72 match alpha {
73 NCALPHA_BLEND => Blend,
74 NCALPHA_HIGHCONTRAST => HighContrast,
75 NCALPHA_OPAQUE => Opaque,
76 NCALPHA_TRANSPARENT => Transparent,
77 _ => Self::default(),
78 }
79 }
80 }
81
82 impl From<NcAlpha> for c_api::NcAlpha_u32 {
83 fn from(alpha: NcAlpha) -> Self {
84 use {c_api::*, NcAlpha::*};
85 match alpha {
86 Blend => NCALPHA_BLEND,
87 HighContrast => NCALPHA_HIGHCONTRAST,
88 Opaque => NCALPHA_OPAQUE,
89 Transparent => NCALPHA_TRANSPARENT,
90 }
91 }
92 }
93}
94
95impl NcAlpha {
96 /// Displays the short name identifier of the alpha value.
97 pub fn display_short(&self) -> &str {
98 use NcAlpha::*;
99 match self {
100 Blend => "B",
101 HighContrast => "H",
102 Opaque => "O",
103 Transparent => "T",
104 }
105 }
106}
107
108pub(crate) mod c_api {
109 use crate::c_api::ffi;
110
111 #[allow(unused_imports)] // for doc comments
112 use crate::NcCell;
113
114 /// 2 bits of alpha (surrounded by context dependent bits)
115 /// part of an [`NcChannel`][crate::NcChannel].
116 ///
117 /// It's recommended to use [`NcAlpha`][crate::NcAlpha] instead.
118 ///
119 /// # Associated `c_api` constants
120 ///
121 /// - [`NCALPHA_BLEND`]
122 /// - [`NCALPHA_HIGHCONTRAST`]
123 /// - [`NCALPHA_OPAQUE`]
124 /// - [`NCALPHA_TRANSPARENT`]
125 ///
126 /// ## Diagram
127 ///
128 /// ```txt
129 /// ~~AA~~~~ -------- -------- --------
130 /// ```
131 /// `type in C: no data type`
132 pub type NcAlpha_u32 = u32;
133
134 /// [`NcAlpha_u32`] bits indicating [`NcCell`]'s foreground or background color
135 /// will be a composite between its color and the `NcCell`s' corresponding
136 /// colors underneath it.
137 pub const NCALPHA_BLEND: NcAlpha_u32 = ffi::NCALPHA_BLEND;
138
139 /// [`NcAlpha_u32`] bits indicating [`NcCell`]'s foreground color will be
140 /// high-contrast (relative to the computed background).
141 /// Background cannot be high-contrast.
142 pub const NCALPHA_HIGHCONTRAST: NcAlpha_u32 = ffi::NCALPHA_HIGHCONTRAST;
143
144 /// [`NcAlpha_u32`] bits indicating [`NcCell`]'s foreground or background color
145 /// is used unchanged.
146 pub const NCALPHA_OPAQUE: NcAlpha_u32 = ffi::NCALPHA_OPAQUE;
147
148 /// [`NcAlpha_u32`] bits indicating [`NcCell`]'s foreground or background color
149 /// is derived entirely from the `NcCell`s underneath it.
150 pub const NCALPHA_TRANSPARENT: NcAlpha_u32 = ffi::NCALPHA_TRANSPARENT;
151}