Skip to content

Commit

Permalink
fix some doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Sep 4, 2024
1 parent 0db3265 commit cdd3aaa
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 103 deletions.
40 changes: 22 additions & 18 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
/// let grad = colorgrad::GradientBuilder::new()
/// .html_colors(&["deeppink", "gold", "seagreen"])
/// .domain(&[0.0, 100.0])
/// .mode(colorgrad::BlendMode::Rgb)
/// .build::<colorgrad::LinearGradient>()?;
///
/// 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<dyn Error>> {
let grad = colorgrad::GradientBuilder::new()
.html_colors(&["deeppink", "gold", "seagreen"])
.domain(&[0.0, 100.0])
.mode(colorgrad::BlendMode::Rgb)
.build::<colorgrad::LinearGradient>()?;
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<Color>,
Expand Down
26 changes: 15 additions & 11 deletions src/gradient/basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
/// let grad = colorgrad::GradientBuilder::new()
/// .html_colors(&["deeppink", "gold", "seagreen"])
/// .build::<colorgrad::BasisGradient>()?;
/// # Ok(())
/// # }
/// ```
#[cfg_attr(
feature = "named-colors",
doc = r##"
```
# use std::error::Error;
use colorgrad::Gradient;
# fn main() -> Result<(), Box<dyn Error>> {
let grad = colorgrad::GradientBuilder::new()
.html_colors(&["deeppink", "gold", "seagreen"])
.build::<colorgrad::BasisGradient>()?;
# Ok(())
# }
```"##
)]
#[derive(Debug, Clone)]
pub struct BasisGradient {
values: Vec<[f32; 4]>,
Expand Down
26 changes: 15 additions & 11 deletions src/gradient/catmull_rom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
/// let grad = colorgrad::GradientBuilder::new()
/// .html_colors(&["deeppink", "gold", "seagreen"])
/// .build::<colorgrad::CatmullRomGradient>()?;
/// # Ok(())
/// # }
/// ```
#[cfg_attr(
feature = "named-colors",
doc = r##"
```
# use std::error::Error;
use colorgrad::Gradient;
# fn main() -> Result<(), Box<dyn Error>> {
let grad = colorgrad::GradientBuilder::new()
.html_colors(&["deeppink", "gold", "seagreen"])
.build::<colorgrad::CatmullRomGradient>()?;
# Ok(())
# }
```"##
)]
#[derive(Debug, Clone)]
pub struct CatmullRomGradient {
segments: Vec<[[f32; 4]; 4]>,
Expand Down
26 changes: 15 additions & 11 deletions src/gradient/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
/// let grad = colorgrad::GradientBuilder::new()
/// .html_colors(&["deeppink", "gold", "seagreen"])
/// .build::<colorgrad::LinearGradient>()?;
/// # Ok(())
/// # }
/// ```
#[cfg_attr(
feature = "named-colors",
doc = r##"
```
# use std::error::Error;
use colorgrad::Gradient;
# fn main() -> Result<(), Box<dyn Error>> {
let grad = colorgrad::GradientBuilder::new()
.html_colors(&["deeppink", "gold", "seagreen"])
.build::<colorgrad::LinearGradient>()?;
# Ok(())
# }
```"##
)]
#[derive(Debug, Clone)]
pub struct LinearGradient {
stops: Vec<(f32, [f32; 4])>,
Expand Down
14 changes: 9 additions & 5 deletions src/gradient/sharp.rs
Original file line number Diff line number Diff line change
@@ -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])>,
Expand Down
108 changes: 61 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//! ```
Expand All @@ -37,32 +41,37 @@
//!
//! ### Gradient Image
//!
//! ```ignore
//! use colorgrad::Gradient;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let grad = colorgrad::GradientBuilder::new()
//! .html_colors(&["deeppink", "gold", "seagreen"])
//! .build::<colorgrad::CatmullRomGradient>()?;
//!
//! 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<dyn std::error::Error>> {
let grad = colorgrad::GradientBuilder::new()
.html_colors(&["deeppink", "gold", "seagreen"])
.build::<colorgrad::CatmullRomGradient>()?;
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
//!
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit cdd3aaa

Please sign in to comment.