From cdd3aaabf307b3a0adcc0fc94763d2d558c3123f Mon Sep 17 00:00:00 2001 From: Nor Khasyatillah Date: Wed, 4 Sep 2024 18:44:07 +0700 Subject: [PATCH] fix some doctests --- src/builder.rs | 40 +++++++------ src/gradient/basis.rs | 26 +++++---- src/gradient/catmull_rom.rs | 26 +++++---- src/gradient/linear.rs | 26 +++++---- src/gradient/sharp.rs | 14 +++-- src/lib.rs | 108 ++++++++++++++++++++---------------- 6 files changed, 137 insertions(+), 103 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 2701aa9..46ff739 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -57,24 +57,28 @@ impl error::Error for GradientBuilderError {} /// # } /// ``` /// -/// ## Using web color format string -/// -/// ```ignore -/// # use std::error::Error; -/// use colorgrad::Gradient; -/// -/// # fn main() -> Result<(), Box> { -/// let grad = colorgrad::GradientBuilder::new() -/// .html_colors(&["deeppink", "gold", "seagreen"]) -/// .domain(&[0.0, 100.0]) -/// .mode(colorgrad::BlendMode::Rgb) -/// .build::()?; -/// -/// assert_eq!(grad.at(0.0).to_rgba8(), [255, 20, 147, 255]); -/// assert_eq!(grad.at(100.0).to_rgba8(), [46, 139, 87, 255]); -/// # Ok(()) -/// # } -/// ``` +#[cfg_attr( + feature = "named-colors", + doc = r##" +## Using web color format string + +``` +# use std::error::Error; +use colorgrad::Gradient; + +# fn main() -> Result<(), Box> { +let grad = colorgrad::GradientBuilder::new() + .html_colors(&["deeppink", "gold", "seagreen"]) + .domain(&[0.0, 100.0]) + .mode(colorgrad::BlendMode::Rgb) + .build::()?; + +assert_eq!(grad.at(0.0).to_rgba8(), [255, 20, 147, 255]); +assert_eq!(grad.at(100.0).to_rgba8(), [46, 139, 87, 255]); +# Ok(()) +# } +```"## +)] #[derive(Debug, Clone)] pub struct GradientBuilder { pub(crate) colors: Vec, diff --git a/src/gradient/basis.rs b/src/gradient/basis.rs index 0a39bad..31ffebe 100644 --- a/src/gradient/basis.rs +++ b/src/gradient/basis.rs @@ -16,17 +16,21 @@ fn basis(t1: f32, v0: f32, v1: f32, v2: f32, v3: f32) -> f32 { / 6.0 } -/// ```ignore -/// # use std::error::Error; -/// use colorgrad::Gradient; -/// -/// # fn main() -> Result<(), Box> { -/// let grad = colorgrad::GradientBuilder::new() -/// .html_colors(&["deeppink", "gold", "seagreen"]) -/// .build::()?; -/// # Ok(()) -/// # } -/// ``` +#[cfg_attr( + feature = "named-colors", + doc = r##" +``` +# use std::error::Error; +use colorgrad::Gradient; + +# fn main() -> Result<(), Box> { +let grad = colorgrad::GradientBuilder::new() + .html_colors(&["deeppink", "gold", "seagreen"]) + .build::()?; +# Ok(()) +# } +```"## +)] #[derive(Debug, Clone)] pub struct BasisGradient { values: Vec<[f32; 4]>, diff --git a/src/gradient/catmull_rom.rs b/src/gradient/catmull_rom.rs index cf3a463..5448f37 100644 --- a/src/gradient/catmull_rom.rs +++ b/src/gradient/catmull_rom.rs @@ -5,17 +5,21 @@ use crate::{convert_colors, BlendMode, Color, Gradient, GradientBuilder, Gradien // Catmull-Rom spline algorithm adapted from: // https://qroph.github.io/2018/07/30/smooth-paths-using-catmull-rom-splines.html -/// ```ignore -/// # use std::error::Error; -/// use colorgrad::Gradient; -/// -/// # fn main() -> Result<(), Box> { -/// let grad = colorgrad::GradientBuilder::new() -/// .html_colors(&["deeppink", "gold", "seagreen"]) -/// .build::()?; -/// # Ok(()) -/// # } -/// ``` +#[cfg_attr( + feature = "named-colors", + doc = r##" +``` +# use std::error::Error; +use colorgrad::Gradient; + +# fn main() -> Result<(), Box> { +let grad = colorgrad::GradientBuilder::new() + .html_colors(&["deeppink", "gold", "seagreen"]) + .build::()?; +# Ok(()) +# } +```"## +)] #[derive(Debug, Clone)] pub struct CatmullRomGradient { segments: Vec<[[f32; 4]; 4]>, diff --git a/src/gradient/linear.rs b/src/gradient/linear.rs index 42aee6c..5be6135 100644 --- a/src/gradient/linear.rs +++ b/src/gradient/linear.rs @@ -2,17 +2,21 @@ use std::convert::TryFrom; use crate::{convert_colors, BlendMode, Color, Gradient, GradientBuilder, GradientBuilderError}; -/// ```ignore -/// # use std::error::Error; -/// use colorgrad::Gradient; -/// -/// # fn main() -> Result<(), Box> { -/// let grad = colorgrad::GradientBuilder::new() -/// .html_colors(&["deeppink", "gold", "seagreen"]) -/// .build::()?; -/// # Ok(()) -/// # } -/// ``` +#[cfg_attr( + feature = "named-colors", + doc = r##" +``` +# use std::error::Error; +use colorgrad::Gradient; + +# fn main() -> Result<(), Box> { +let grad = colorgrad::GradientBuilder::new() + .html_colors(&["deeppink", "gold", "seagreen"]) + .build::()?; +# Ok(()) +# } +```"## +)] #[derive(Debug, Clone)] pub struct LinearGradient { stops: Vec<(f32, [f32; 4])>, diff --git a/src/gradient/sharp.rs b/src/gradient/sharp.rs index 031e6fb..99558c5 100644 --- a/src/gradient/sharp.rs +++ b/src/gradient/sharp.rs @@ -1,10 +1,14 @@ use crate::{convert_colors, linspace, BlendMode, Color, Gradient}; -/// ```ignore -/// use colorgrad::Gradient; -/// -/// let g = colorgrad::preset::rainbow().sharp(11, 0.); -/// ``` +#[cfg_attr( + feature = "preset", + doc = r##" +``` +use colorgrad::Gradient; + +let g = colorgrad::preset::rainbow().sharp(11, 0.0); +```"## +)] #[derive(Debug, Clone)] pub struct SharpGradient { stops: Vec<(f32, [f32; 4])>, diff --git a/src/lib.rs b/src/lib.rs index 249bbf6..7e062a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,15 +4,19 @@ //! //! ## Usage //! -//! Using preset gradient: -//! ```ignore -//! use colorgrad::Gradient; -//! let g = colorgrad::preset::rainbow(); -//! -//! assert_eq!(g.domain(), (0.0, 1.0)); // all preset gradients are in the domain [0..1] -//! assert_eq!(g.at(0.5).to_rgba8(), [175, 240, 91, 255]); -//! assert_eq!(g.at(0.5).to_hex_string(), "#aff05b"); -//! ``` +#![cfg_attr( + feature = "preset", + doc = r##" +Using preset gradient: +``` +use colorgrad::Gradient; +let g = colorgrad::preset::rainbow(); + +assert_eq!(g.domain(), (0.0, 1.0)); // all preset gradients are in the domain [0..1] +assert_eq!(g.at(0.5).to_rgba8(), [175, 240, 91, 255]); +assert_eq!(g.at(0.5).to_hex_string(), "#aff05b"); +```"## +)] //! //! Custom gradient: //! ``` @@ -37,32 +41,37 @@ //! //! ### Gradient Image //! -//! ```ignore -//! use colorgrad::Gradient; -//! -//! fn main() -> Result<(), Box> { -//! let grad = colorgrad::GradientBuilder::new() -//! .html_colors(&["deeppink", "gold", "seagreen"]) -//! .build::()?; -//! -//! let width = 1500; -//! let height = 70; -//! -//! let mut imgbuf = image::ImageBuffer::new(width, height); -//! -//! for (x, _, pixel) in imgbuf.enumerate_pixels_mut() { -//! let rgba = grad.at(x as f32 / width as f32).to_rgba8(); -//! *pixel = image::Rgba(rgba); -//! } -//! -//! imgbuf.save("gradient.png")?; -//! Ok(()) -//! } -//! ``` -//! -//! Example output: -//! -//! ![img](https://raw.githubusercontent.com/mazznoer/colorgrad-rs/master/docs/images/example-gradient.png) +#![cfg_attr( + feature = "named-colors", + doc = r##" +``` +use colorgrad::Gradient; + +fn main() -> Result<(), Box> { + let grad = colorgrad::GradientBuilder::new() + .html_colors(&["deeppink", "gold", "seagreen"]) + .build::()?; + + let width = 1500; + let height = 70; + + let mut imgbuf = image::ImageBuffer::new(width, height); + + for (x, _, pixel) in imgbuf.enumerate_pixels_mut() { + let rgba = grad.at(x as f32 / width as f32).to_rgba8(); + *pixel = image::Rgba(rgba); + } + + imgbuf.save("gradient.png")?; + Ok(()) +} +``` + +Example output: + +![img](https://raw.githubusercontent.com/mazznoer/colorgrad-rs/master/docs/images/example-gradient.png) +"## +)] //! //! ### Colored Noise //! @@ -183,18 +192,23 @@ pub trait Gradient: CloneGradient { .collect() } - /// Get new hard-edge gradient - /// - /// ```ignore - /// let g = colorgrad::preset::rainbow(); - /// ``` - /// ![img](https://raw.githubusercontent.com/mazznoer/colorgrad-rs/master/docs/images/preset/rainbow.png) - /// - /// ```ignore - /// use colorgrad::Gradient; - /// let g = colorgrad::preset::rainbow().sharp(11, 0.); - /// ``` - /// ![img](https://raw.githubusercontent.com/mazznoer/colorgrad-rs/master/docs/images/rainbow-sharp.png) + #[cfg_attr( + feature = "preset", + doc = r##" + Get new hard-edge gradient + + ``` + let g = colorgrad::preset::rainbow(); + ``` + ![img](https://raw.githubusercontent.com/mazznoer/colorgrad-rs/master/docs/images/preset/rainbow.png) + + ``` + use colorgrad::Gradient; + let g = colorgrad::preset::rainbow().sharp(11, 0.0); + ``` + ![img](https://raw.githubusercontent.com/mazznoer/colorgrad-rs/master/docs/images/rainbow-sharp.png) + "## + )] fn sharp(&self, segment: u16, smoothness: f32) -> SharpGradient { let colors = if segment > 1 { self.colors(segment.into())