Add comments

This commit is contained in:
Nicolas 2022-04-03 12:30:36 +02:00
parent 1bc267461a
commit 0e0f012810
4 changed files with 31 additions and 0 deletions

View File

@ -1,12 +1,18 @@
/// Used to get a character with a given brightness.
pub struct CharSampler {
chars: String
}
impl CharSampler {
/// # Arguments
/// * `chars`: The characters ordered by brightness.
pub fn new(chars: String) -> Self {
Self { chars }
}
/// Gets a character with the given brightness.
/// # Arguments
/// * `level`: `0 <= level` and `level < 1`
pub fn sample(&self, level: f32) -> char {
let pos = level * self.chars.chars().count() as f32;
let index = pos as usize;

View File

@ -6,7 +6,14 @@ use rand::Rng;
use crate::fill::level::LevelFillMode;
use crate::vec::Vector;
/// Used to choose the colors of characters.
pub trait FillMode {
/// Gets the color for this character.
/// # Arguments
/// * `level`:
/// * `pos`:
///
/// returns: f32
fn sample(&self, level: f32, pos: Vector) -> f32;
}

View File

@ -4,6 +4,7 @@ use rand::prelude::IteratorRandom;
use rand::Rng;
use clap::ArgEnum;
/// A collection of colors.
pub struct Pallet {
values: Vec<Color>
}
@ -45,6 +46,9 @@ impl Pallet {
Pallet::new(vec![Magenta, Blue, Green, Yellow, Red])
}
/// Gets a color for the given fill.
/// # Arguments
/// * `fill`: `0 <= fill` and `fill < 1`
pub fn sample(&self, fill: f32) -> Color {
let pos = self.values.len() as f32 * fill;
let index = pos as usize;
@ -65,6 +69,12 @@ pub enum PalletEnum {
Rainbow
}
/// Chooses a random color pallet.
/// If none is provided, a random one of all available is chosen.
/// # Arguments
/// * `options`: A list of all options.
/// * `rng`: The number generator.
pub fn choose_pallet(mut options: Vec<PalletEnum>, rng: &mut impl Rng) -> PalletEnum {
if options.is_empty() {
options.push(PalletEnum::Red);
@ -79,6 +89,9 @@ pub fn choose_pallet(mut options: Vec<PalletEnum>, rng: &mut impl Rng) -> Pallet
options.into_iter().choose(rng).unwrap()
}
/// Creates the requested pallet.
/// # Arguments
/// * `pallet`: The pallet type.
pub fn create_pallet(pallet: PalletEnum) -> Pallet {
match pallet {
PalletEnum::Red => Pallet::red(),

View File

@ -1,3 +1,4 @@
/// A vector with a x and y axis.
#[derive(Copy, Clone)]
pub struct Vector {
pub x: f32,
@ -9,6 +10,10 @@ impl Vector {
Self { x, y }
}
/// Creates a vector with the on screen coordinates based on the terminal coordinates.
/// # Arguments
/// * `x`: The x axis of the terminal character.
/// * `y`: The y axis of the terminal character.
pub fn from_terminal(x: usize, y: usize) -> Self {
Vector::new(x as f32, y as f32 * 2.0)
}