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
//! NcAlpha

use std::fmt;

#[allow(unused_imports)] // for doc comments
use crate::NcCell;

/// Alpha information, part of an [`NcChannel`][crate::NcChannel],
/// applies to [`NcCell`]'s foreground or background color.
///
/// # Default:
/// *[`NcAlpha::Opaque`]*
///
/// ## Diagram
///
/// Internally it's 2 bits of alpha, surrounded by context dependent bits:
///
/// ```txt
/// ~~AA~~~~ -------- -------- --------
/// ```
///
/// See also: [`NcChannels`][crate::NcChannels] for more context information.
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NcAlpha {
    /// Indicates [`NcCell`]'s foreground or background color will be a
    /// composite between its color and the `NcCell`s' corresponding colors
    /// underneath it.
    Blend = c_api::NCALPHA_BLEND,

    /// Indicates the foreground color will be high-contrast,
    /// relative to the computed background.
    ///
    /// Background cannot be high-contrast.
    HighContrast = c_api::NCALPHA_HIGHCONTRAST,

    /// Indicates [`NcCell`]'s foreground or background color is used unchanged.
    Opaque = c_api::NCALPHA_OPAQUE,

    /// Indicates [`NcCell`]'s foreground or background color is derived
    /// entirely from the `NcCell`s underneath it.
    Transparent = c_api::NCALPHA_TRANSPARENT,
}

impl Default for NcAlpha {
    fn default() -> Self {
        Self::Opaque
    }
}

impl fmt::Display for NcAlpha {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use NcAlpha::*;
        write!(
            f,
            "{}",
            match self {
                Blend => "Blend",
                HighContrast => "HighContrast",
                Opaque => "Opaque",
                Transparent => "Transparent",
            }
        )
    }
}

impl From<c_api::NcAlpha_u32> for NcAlpha {
    fn from(alpha: c_api::NcAlpha_u32) -> Self {
        use {c_api::*, NcAlpha::*};
        match alpha {
            NCALPHA_BLEND => Blend,
            NCALPHA_HIGHCONTRAST => HighContrast,
            NCALPHA_OPAQUE => Opaque,
            NCALPHA_TRANSPARENT => Transparent,
            _ => Opaque, // invalid values default to `Opaque`
        }
    }
}

impl From<NcAlpha> for c_api::NcAlpha_u32 {
    fn from(alpha: NcAlpha) -> Self {
        use {c_api::*, NcAlpha::*};
        match alpha {
            Blend => NCALPHA_BLEND,
            HighContrast => NCALPHA_HIGHCONTRAST,
            Opaque => NCALPHA_OPAQUE,
            Transparent => NCALPHA_TRANSPARENT,
        }
    }
}

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

    #[allow(unused_imports)] // for doc comments
    use crate::NcCell;

    /// 2 bits of alpha (surrounded by context dependent bits)
    /// part of an [`NcChannel`][crate::NcChannel], (alias of `u32`).
    ///
    /// ## Diagram
    ///
    /// ```txt
    /// ~~AA~~~~ -------- -------- --------
    /// ```
    /// `type in C: no data type`
    pub type NcAlpha_u32 = u32;

    /// [`NcAlpha_u32`] bits indicating [`NcCell`]'s foreground or background color
    /// will be a composite between its color and the `NcCell`s' corresponding
    /// colors underneath it.
    pub const NCALPHA_BLEND: NcAlpha_u32 = ffi::NCALPHA_BLEND;

    /// [`NcAlpha_u32`] bits indicating [`NcCell`]'s foreground color will be
    /// high-contrast (relative to the computed background).
    /// Background cannot be high-contrast.
    pub const NCALPHA_HIGHCONTRAST: NcAlpha_u32 = ffi::NCALPHA_HIGHCONTRAST;

    /// [`NcAlpha_u32`] bits indicating [`NcCell`]'s foreground or background color
    /// is used unchanged.
    pub const NCALPHA_OPAQUE: NcAlpha_u32 = ffi::NCALPHA_OPAQUE;

    /// [`NcAlpha_u32`] bits indicating [`NcCell`]'s foreground or background color
    /// is derived entirely from the `NcCell`s underneath it.
    pub const NCALPHA_TRANSPARENT: NcAlpha_u32 = ffi::NCALPHA_TRANSPARENT;
}