Rust : How to Generate Random Colors

How to Generate Random Colors in Rust using various crates.

In this tutorial we will look at crates and examples that allow us generate random colors.

(a). Use random_color

random_color is a Rust crate for generating random attractive colors.

Step 1: Install

You start by installing it. In your Cargo.toml add this line:

random_color = "0.6.1"

Step 2: Gerenate Colors

Then generate colors as follows:

use random_color::{Color, Luminosity, RandomColor};

let color = RandomColor::new()
  .hue(Color::Blue) // Optional
  .luminosity(Luminosity::Light) // Optional
  .seed(42) // Optional
  .alpha(1.0) // Optional
  .to_hsl_string(); //

// color => "hsl(179, 99%, 10%)"

Here are the possible hue values;

  • Color::Monochrome
  • Color::Red
  • Color::Orange
  • Color::Yellow
  • Color::Green
  • Color::Blue
  • Color::Purple
  • Color::Pink

Here are the Possible luminosity values:

  • Luminosity::Random
  • Luminosity::Bright
  • Luminosity::Light
  • Luminosity::Dark

Here are the Possible alpha values:

  • You can specify a value between 0 and 1 with .alpha()
  • You can specify a random value with .random_alpha()

Here are the available outputs:

  // As HSV Array
  let color = RandomColor::new().to_hsv_array(); // color => [179, 20, 100]

  // As RGB String
  let color = RandomColor::new().to_rgb_string(); // color => "rgb(204, 255, 254)"

  // As RGBA String
  let color = RandomColor::new().to_rgba_string(); // color => "rgba(204, 255, 254, 1)"

  // As RGB Array
  let color = RandomColor::new().to_rgb_array(); // color => [204, 255, 254]

  // As HSL String
  let color = RandomColor::new().to_hsl_string(); // color => "hsl(179, 99%, 10%)"

  // As HSLA String
  let color = RandomColor::new().to_hsla_string(); // color => "hsl(179, 99%, 10%, 1)"

  // As HSL Array
  let color = RandomColor::new().to_hsl_array(); // color => [179, 99, 10]

  // As Hex String
  let color = RandomColor::new().to_hex(); // color => "#b31464"

Reference

Read more here or here.

(b). colourado

colourado is a small and minimalistic library to generate a random color palette.

The user-facing Color struct contains RGB colors ranging from 0 to 1.
All colors are of type f32 (no exceptions).

Step 1: Install it

To install it, add the following line in your Cargo.toml:

colourado = "0.2.0"

Step 2: Write Code

use colourado::{Color, ColorPalette, PaletteType};

let palette = ColorPalette::new(4, PaletteType::Random, false);
let random_color = palette.colors[0].red;
let color_array: [f32; 3] = palette.colors[1].to_array();
let hue = 315.0;
let saturation = 0.5;
let value = 0.3;
let rgb_color: Color = Color::hsv_to_rgb(hue, saturation, value);

A color palette might look like this when rendered:

Example image

Test the color palettes for yourself by running
cargo run --example preview TYPE NUM adjacent|spread
TYPE can be one of random, pastel, or dark NUM is the amount of colors to generate and display adjacent or spread determine whether the colors are generated close to each other or spread apart.

Reference

Read more here or here.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *