Coder Social home page Coder Social logo

elementh / random_color Goto Github PK

View Code? Open in Web Editor NEW
43.0 43.0 6.0 83 KB

โš™๏ธ๐ŸŽจ Rust crate for generating random attractive colors

Home Page: https://crates.io/crates/random_color

License: MIT License

Rust 100.00%
art colors random rgb rust rust-crate

random_color's Introduction

Hello there ๐Ÿ‘‹

general kenobi

random_color's People

Contributors

attila-lin avatar blefevre avatar elementh avatar inclooder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

random_color's Issues

Let the caller choose the random number generator.

Functions and methods that use random numbers should probably have a signature like fn pick_brightness<R: Rng>(&self, rng: &mut R, ...) and then it should call rng.gen_range.

It should provide convenience methods that use some default so specifying a Rng doesn't becomes mandatory.

I think the `seed` function broken

random_color/src/lib.rs

Lines 223 to 228 in 433c62b

Some(seed) => {
// do with float
let seed = (seed * 9301 + 49297) % 233280;
let mut rnd = seed / 233280;
(min + rnd * (max - min))
}

Like in the comment, the calculation should be done with float type, but instead, the code always do calculation with integer type. So the value of rnd in line 226 is always 0, so the generated random number is always min.

Also, the multiplication in line 225 could cause the result to overflow which will panic the program.

random alpha documentation

I was looking through the code and spotted that random alpha values are generated if alpha is None. Currently the only way to do that to use struct initialization like so:

let color = Color {
    alpha: None,
    ..Color::new()
};

I think this functionality should be noted in the documentation somewhere.

Rng.gen_range called with low >= high

It's look's like where is a problem with this function. In some case min and max are equal and gen_range create a exception.

fn random_within(&self, mut min: i32, mut max: i32) -> i32 {
        if min > max {
            std::mem::swap(&mut min, &mut max);
        }
        match self.seed {
            None => rand::thread_rng().gen_range(min, max),
            Some(seed) => {
                // do with float
                let seed = (seed * 9301 + 49297) % 233280;
                let mut rnd = seed / 233280;
                (min + rnd * (max - min))
            }
        }
}

Reusing a pre-seeded instance should return new colors

I'm creating an instance with a .seed(0) because I want to get random colors, but I want the sequence to be consistent between reruns. This is what seed is usually leveraged for in regular PRNGs.

However, in random_color instance it seems it creates a new random number generator on every call, which means that it will always return the same color on subsequent invocations.

Example:

let mut random_color = RandomColor::new();
random_color.seed(0);

let color1 = random_color.to_rgb_array();
let color2 = random_color.to_rgb_array(); // returns same as color1
let color3 = random_color.to_rgb_array(); // same again
...

Seed values greater than ~230k cause thread panicked at 'attempt to multiply with overflow'

extern crate random_color;
use random_color::{Color, Luminosity, RandomColor};
fn main() {
let color = RandomColor::new()
.hue(Color::Blue) // Optional
.luminosity(Luminosity::Light) // Optional
.seed(260_000) // Optional
.to_rgb_array(); //
}

Causes a panic in random_within. I'm very new to rust so I'm not exactly sure the best way to handle this. usize might be large enough type for the random_within calculations that any i32 passed into the function will not cause an overflow.

Thanks for the library

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.