[rust] more functions to manage the "default color"

- new functions:
  - channel_set_not_default
  - channels_set_fg_not_default
  - channels_set_bg_not_default
  - channels_set_default
  - channels_set_not_default
- new NcChannel methods:
  - set_not_default
- new NcChannelPair methods:
  - set_fg_not_default
  - set_bg_not_default
  - set_default
  - set_not_default
- improve the test of: channel_set_default
- add tests for:
  - channel_set
  - channel_set_not_default
- add doc comments to existing NcChannel reimplemented functions tests.
This commit is contained in:
joseLuís 2021-05-03 19:25:03 +02:00
parent dcfb368b21
commit e9a841954d
4 changed files with 228 additions and 27 deletions

View File

@ -35,6 +35,7 @@ pub trait NcChannelMethods {
fn default_p(&self) -> bool;
fn set_default(&mut self) -> Self;
fn set_not_default(&mut self) -> Self;
fn palindex_p(&self) -> bool;
}
@ -112,7 +113,11 @@ pub trait NcChannelPairMethods {
fn fg_default_p(&self) -> bool;
fn bg_default_p(&self) -> bool;
fn set_fg_default(&mut self) -> Self;
fn set_fg_not_default(&mut self) -> Self;
fn set_bg_default(&mut self) -> Self;
fn set_bg_not_default(&mut self) -> Self;
fn set_default(&mut self) -> Self;
fn set_not_default(&mut self) -> Self;
fn fg_palindex_p(&self) -> bool;
fn bg_palindex_p(&self) -> bool;
@ -126,12 +131,12 @@ pub trait NcChannelPairMethods {
impl NcChannelMethods for NcChannel {
// Constructors
/// New NcChannel, set to black and NOT using the default color.
/// New NcChannel, set to black and NOT using the "default color".
fn new() -> Self {
0 as NcChannel | crate::NCCELL_BGDEFAULT_MASK
}
/// New NcChannel, set to black but using the default color.
/// New NcChannel, set to black but using the "default color".
fn with_default() -> Self {
0 as NcChannel
}
@ -218,7 +223,7 @@ impl NcChannelMethods for NcChannel {
}
/// Sets the three [NcColor]s, and
/// marks the NcChannel as NOT using the default color.
/// marks the NcChannel as NOT using the "default color".
///
/// *C style function: [channel_set_rgb8()][crate::channel_set_rgb8].*
fn set_rgb8(&mut self, r: NcColor, g: NcColor, b: NcColor) -> Self {
@ -285,7 +290,7 @@ impl NcChannelMethods for NcChannel {
crate::channel_rgb(*self)
}
/// Sets the [NcRgb] and marks it as NOT using the default color,
/// Sets the [NcRgb] and marks it as NOT using the "default color",
/// retaining the other bits unchanged.
///
/// *C style function: [channel_set()][crate::channel_set].*
@ -310,6 +315,21 @@ impl NcChannelMethods for NcChannel {
crate::channel_set_default(self)
}
/// Marks an NcChannel as *not* using its "default color".
///
/// The following methods also marks the channel as not using the "default color":
/// - [new()][NcChannel#method.new]
/// - [set()][NcChannel#method.set]
/// - [set_rgb()][NcChannel#method.set_rgb]
/// - [set_rgb8()][NcChannel#method.set_rgb8]
///
/// *C style function: [channel_set_not_default()][crate::channel_set_not_default].*
//
// Not in the C API
fn set_not_default(&mut self) -> Self {
crate::channel_set_not_default(self)
}
// NcPaletteIndex
/// Is this NcChannel using palette-indexed color rather than RGB?
@ -326,7 +346,7 @@ impl NcChannelMethods for NcChannel {
impl NcChannelPairMethods for NcChannelPair {
// Constructors
/// New NcChannelPair, set to black and NOT using the default color.
/// New NcChannelPair, set to black and NOT using the "default color".
fn new() -> Self {
Self::combine(
0 as NcChannel | crate::NCCELL_BGDEFAULT_MASK,
@ -334,7 +354,7 @@ impl NcChannelPairMethods for NcChannelPair {
)
}
/// New NcChannelPair, set to black but using the default color.
/// New NcChannelPair, set to black but using the "default color".
fn with_default() -> Self {
Self::combine(0 as NcChannel, 0 as NcChannel)
}
@ -680,6 +700,44 @@ impl NcChannelPairMethods for NcChannelPair {
crate::channels_set_bg_default(self)
}
/// Marks the foreground as NOT using its "default color", and
/// returns the new [NcChannelPair].
///
/// *C style function: [channels_set_fg_default()][crate::channels_set_fg_default].*
//
// Not in the C API
fn set_fg_not_default(&mut self) -> Self {
crate::channels_set_fg_not_default(self)
}
/// Marks the background as NOT using its "default color", and
/// returns the new [NcChannelPair].
///
/// *C style function: [channels_set_bg_not_default()][crate::channels_set_bg_not_default].*
//
// Not in the C API
fn set_bg_not_default(&mut self) -> Self {
crate::channels_set_bg_not_default(self)
}
/// Marks both the foreground and background as using its "default color", and
/// returns the new [NcChannelPair].
///
//
// Not in the C API
fn set_default(&mut self) -> Self {
crate::channels_set_fg_default(&mut crate::channels_set_bg_default(self))
}
/// Marks both the foreground and background as NOT using its "default color",
/// and returns the new [NcChannelPair].
///
//
// Not in the C API
fn set_not_default(&mut self) -> Self {
crate::channels_set_fg_not_default(&mut crate::channels_set_bg_not_default(self))
}
// NcPaletteIndex
/// Is the foreground of using an [indexed][NcPaletteIndex]

View File

@ -13,12 +13,12 @@
// - `channels_set_bg_rgb8_clipped()`
// -----------------------------------------------------------------------------
//
// functions manually reimplemented: 39
// functions manually reimplemented: 44
// ------------------------------------------
// (X) wont: 3
// (+) done: 36 / 0
// (#) test: 19
// (W) wrap: 36
// (#) test: 21
// (W) wrap: 41
// ------------------------------------------
//W# channel_alpha
//W# channel_b
@ -27,9 +27,10 @@
//W# channel_palindex_p
//W# channel_r
//W# channel_rgb8
//W+ channel_set
//W# channel_set
//W# channel_set_alpha
//W# channel_set_default
//W# channel_set_not_default // not in the original C API
//W# channel_set_rgb8
// X channel_set_rgb_clipped // not needed
//W# channels_bchannel
@ -48,17 +49,21 @@
//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
//W+ channels_set_bg_rgb8
// X channels_set_bg_rgb8_clipped // not needed
//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
//W+ channels_set_fg_rgb8
// X channels_set_fg_rgb8_clipped // not needed
//W channels_set_not_default // not in the original C API
#[allow(unused_imports)] // for the doc comments
use crate::NcRgba;

View File

@ -193,7 +193,7 @@ pub fn channel_rgb8(
channel
}
/// Sets the three RGB [NcColor]s an [NcChannel], and marks it as not using the
/// Sets the three RGB [NcColor]s an [NcChannel], and marks it as NOT using the
/// "default color", retaining the other bits unchanged.
///
/// *Method: NcChannel.[set_rgb8()][NcChannel#method.set_rgb8]*
@ -232,7 +232,7 @@ pub fn channels_bg_rgb8(
}
/// Sets the three foreground RGB [NcColor]s of an [NcChannelPair], and
/// marks it as not using the "default color".
/// marks it as NOT using the "default color", retaining the other bits unchanged.
///
/// Unlike the original C API, it also returns the new NcChannelPair.
///
@ -251,7 +251,7 @@ pub fn channels_set_fg_rgb8(
}
/// Sets the three background RGB [NcColor]s of an [NcChannelPair], and
/// marks it as not using the "default color".
/// marks it as NOT using the "default color", retaining the other bits unchanged.
///
/// Unlike the original C API, it also returns the new NcChannelPair.
///
@ -299,8 +299,8 @@ pub const fn channel_rgb(channel: NcChannel) -> NcRgb {
channel & NCCELL_BG_RGB_MASK
}
/// Sets the [NcRgb] of an [NcChannel], and marks it
/// as not using the "default color", retaining the other bits unchanged.
/// Sets the [NcRgb] of an [NcChannel], and marks it as NOT using the
/// "default color", retaining the other bits unchanged.
///
/// *Method: NcChannel.[set()][NcChannel#method.set]*
#[inline]
@ -308,8 +308,8 @@ pub fn channel_set(channel: &mut NcChannel, rgb: NcRgb) {
*channel = (*channel & !NCCELL_BG_RGB_MASK) | NCCELL_BGDEFAULT_MASK | (rgb & 0x00ffffff);
}
/// Sets the foreground [NcRgb] of an [NcChannelPair],
/// and marks it as not using the the "default color".
/// Sets the foreground [NcRgb] of an [NcChannelPair], and marks it as NOT using
/// the "default color", retaining the other bits unchanged.
///
/// *Method: NcChannelPair.[set_fg_rgb()][NcChannelPair#method.set_fg_rgb]*
#[inline]
@ -319,8 +319,8 @@ pub fn channels_set_fg_rgb(channels: &mut NcChannelPair, rgb: NcRgb) {
*channels = (channel as u64) << 32 | *channels & 0xffffffff_u64;
}
/// Sets the foreground [NcRgb] of an [NcChannelPair],
/// and marks it as not using the the "default color".
/// Sets the foreground [NcRgb] of an [NcChannelPair], and marks it as NOT using
/// the "default color", retaining the other bits unchanged.
///
/// *Method: NcChannelPair.[set_bg_rgb()][NcChannelPair#method.set_bg_rgb]*
#[inline]
@ -349,6 +349,18 @@ pub fn channel_set_default(channel: &mut NcChannel) -> NcChannel {
*channel
}
/// Marks an [NcChannel] as NOT using its "default color",
/// retaining the other bits unchanged.
///
/// *Method: NcChannel.[set_not_default()][NcChannel#method.set_not_default]*
//
// Not in the C API
#[inline]
pub fn channel_set_not_default(channel: &mut NcChannel) -> NcChannel {
*channel |= NCCELL_BGDEFAULT_MASK;
*channel
}
/// Is the foreground of an [NcChannelPair] using the "default foreground color"?
///
/// *Method: NcChannelPair.[fg_default_p()][NcChannelPair#method.fg_default_p]*
@ -369,7 +381,7 @@ pub fn channels_bg_default_p(channels: NcChannelPair) -> bool {
}
/// Marks the foreground of an [NcChannelPair] as using its "default color",
/// and returns the new [NcChannelPair].
/// which also marks it opaque, and returns the new [NcChannelPair].
///
/// *Method: NcChannelPair.[set_fg_default()][NcChannelPair#method.set_fg_default]*
#[inline]
@ -380,8 +392,22 @@ pub fn channels_set_fg_default(channels: &mut NcChannelPair) -> NcChannelPair {
*channels
}
/// Marks the foreground of an [NcChannelPair] as NOT using its "default color",
/// retaining the other bits unchanged, and returns the new [NcChannelPair].
///
/// *Method: NcChannelPair.[set_fg_not_default()][NcChannelPair#method.set_fg_not_default]*
//
// Not in the C API
#[inline]
pub fn channels_set_fg_not_default(channels: &mut NcChannelPair) -> NcChannelPair {
let mut channel = channels_fchannel(*channels);
channel_set_not_default(&mut channel);
*channels = (channel as u64) << 32 | *channels & 0xffffffff_u64;
*channels
}
/// Marks the background of an [NcChannelPair] as using its "default color",
/// and returns the new [NcChannelPair].
/// which also marks it opaque, and returns the new [NcChannelPair].
///
/// *Method: NcChannelPair.[set_bg_default()][NcChannelPair#method.set_bg_default]*
#[inline]
@ -392,6 +418,46 @@ pub fn channels_set_bg_default(channels: &mut NcChannelPair) -> NcChannelPair {
*channels
}
/// Marks the background of an [NcChannelPair] as NOT using its "default color",
/// retaining the other bits unchanged, and returns the new [NcChannelPair].
///
/// *Method: NcChannelPair.[set_bg_not_default()][NcChannelPair#method.set_bg_not_default]*
//
// Not in the C API
#[inline]
pub fn channels_set_bg_not_default(channels: &mut NcChannelPair) -> NcChannelPair {
let mut channel = channels_bchannel(*channels);
channel_set_not_default(&mut channel);
channels_set_bchannel(channels, channel);
*channels
}
/// Marks both the foreground and background of an [NcChannelPair] as using their
/// "default color", which also marks them opaque, and returns the new [NcChannelPair].
///
/// *Method: NcChannelPair.[set_default()][NcChannelPair#method.set_default]*
//
// Not in the C API
#[inline]
pub fn channels_set_default(channels: &mut NcChannelPair) -> NcChannelPair {
let mut channels = channels_set_fg_default(channels);
let channels = channels_set_bg_default(&mut channels);
channels
}
/// Marks both the foreground and background of an [NcChannelPair] as NOT using their
/// "default color", retaining the other bits unchanged, and returns the new [NcChannelPair].
///
/// *Method: NcChannelPair.[set_not_default()][NcChannelPair#method.set_not_default]*
//
// Not in the C API
#[inline]
pub fn channels_set_not_default(channels: &mut NcChannelPair) -> NcChannelPair {
let mut channels = channels_set_fg_not_default(channels);
let channels = channels_set_bg_not_default(&mut channels);
channels
}
// Palette ---------------------------------------------------------------------
/// Is this [NcChannel] using palette-indexed color rather than RGB?

View File

@ -7,6 +7,9 @@ use crate::{
NCCELL_ALPHA_TRANSPARENT,
};
// NcChannel tests -------------------------------------------------------------
/// retrieves the red NcColor component
#[test]
#[serial]
fn channel_r() {
@ -14,6 +17,7 @@ fn channel_r() {
assert_eq!(crate::channel_r(c), 0x11);
}
/// retrieves the green NcColor component
#[test]
#[serial]
fn channel_g() {
@ -21,6 +25,7 @@ fn channel_g() {
assert_eq!(crate::channel_g(c), 0x22);
}
/// retrieves the blue NcColor component
#[test]
#[serial]
fn channel_b() {
@ -28,6 +33,7 @@ fn channel_b() {
assert_eq!(crate::channel_b(c), 0x33);
}
/// writes out the three RGB NcColor components
#[test]
#[serial]
fn channel_rgb8() {
@ -41,23 +47,56 @@ fn channel_rgb8() {
assert_eq!(b, 0x33);
}
/// sets the three RGB NcColor components
#[test]
#[serial]
fn channel_set_rgb8() {
let mut c: NcChannel = 0x000000;
let mut c = 0x000000;
// by default it uses the default color
assert_eq!(true, crate::channel_default_p(c));
crate::channel_set_rgb8(&mut c, 0x11, 0x22, 0x33);
assert_eq!(crate::channel_r(c), 0x11);
assert_eq!(crate::channel_g(c), 0x22);
assert_eq!(crate::channel_b(c), 0x33);
// now it shoud be marked as NOT using the default color
assert_eq!(false, crate::channel_default_p(c));
}
/// sets the NcRGB color components
#[test]
#[serial]
fn channel_set() {
let mut c = 0x000000;
// by default it uses the default color
assert_eq!(true, crate::channel_default_p(c));
crate::channel_set(&mut c, 0x112233);
println!("\n {:08x}", c); // DEBUG
assert_eq!(crate::channel_r(c), 0x11);
assert_eq!(crate::channel_g(c), 0x22);
assert_eq!(crate::channel_b(c), 0x33);
// now it shoud be marked as NOT using the default color
assert_eq!(false, crate::channel_default_p(c));
}
/// gets the alpha component
#[test]
#[serial]
fn channel_alpha() {
let c: NcChannel = 0x112233;
assert_ne!(crate::channel_alpha(c), NCCELL_ALPHA_TRANSPARENT);
let c: NcChannel = 0x112233 | NCCELL_ALPHA_TRANSPARENT;
assert_eq!(crate::channel_alpha(c), NCCELL_ALPHA_TRANSPARENT);
}
/// sets the alpha component
#[test]
#[serial]
fn channel_set_alpha() {
@ -76,18 +115,45 @@ fn channel_set_alpha() {
// TODO: CHECK for NCCELL_BGDEFAULT_MASK
}
/// sets the channel as using the default color
#[test]
#[serial]
fn channel_set_default() {
const DEFAULT: NcChannel = 0x112233;
let channel = 0x_00_112233;
// By default a channel uses the default color, if the proper bit isn't set
assert_eq!(true, crate::channel_default_p(channel));
let mut c: NcChannel = DEFAULT | NCCELL_ALPHA_TRANSPARENT;
assert!(c != DEFAULT);
// If we change it from being opaque...
let mut channel_transp = channel | NCCELL_ALPHA_TRANSPARENT;
assert_eq!(0x_20_112233, channel_transp); // the transparent bit is now set
crate::channel_set_default(&mut c);
assert_eq!(c, DEFAULT);
crate::channel_set_not_default(&mut channel_transp);
// both the "not default" & transparent bits are now set
assert_eq!(0x_60_112233, channel_transp);
// and calling set_default() should make it both default & opaque again
assert_eq!(0x_00_112233, crate::channel_set_default(&mut channel_transp));
}
/// sets the channel as *not* using the default color
//
// more functions that marks as NOT using the default color:
// - channel_set()
// - channel_set_rgb8()
#[test]
#[serial]
fn channel_set_not_default() {
let mut channel = 0x_00_112233;
// By default a channel uses the default color, if the proper bit isn't set
assert_eq!(true, crate::channel_default_p(channel));
// marking it as NOT using the default color
crate::channel_set_not_default(&mut channel);
assert_eq!(0x_40_112233, channel); // check the "not default" bit is set
assert_eq!(false, crate::channel_default_p(channel));
}
/// checks whether the channel is using the default color
#[test]
#[serial]
fn channel_default_p() {
@ -101,6 +167,9 @@ fn channel_default_p() {
assert_eq!(false, crate::channel_default_p(c));
}
// NcChannelPair tests ---------------------------------------------------------
///
#[test]
#[serial]
#[allow(non_snake_case)]
@ -111,6 +180,7 @@ fn channels_set_fchannel__channels_fchannel() {
assert_eq!(crate::channels_fchannel(cp), fc);
}
///
#[test]
#[serial]
#[allow(non_snake_case)]
@ -121,6 +191,7 @@ fn channels_set_bchannel__channels_bchannel() {
assert_eq!(crate::channels_bchannel(cp), bc);
}
///
#[test]
#[serial]
fn channels_combine() {
@ -134,6 +205,7 @@ fn channels_combine() {
assert_eq!(cp1, _cp2);
}
///
#[test]
#[serial]
fn channels_palette() {