6 releases

0.1.5 Oct 3, 2024
0.1.4 Oct 3, 2024
0.1.1 Sep 29, 2024

#385 in Algorithms

Download history 140/week @ 2024-09-24 424/week @ 2024-10-01 17/week @ 2024-10-08 4/week @ 2024-10-15

585 downloads per month

MIT license

11KB
138 lines

Rustint

Rustint is a Rust library for working with RGB colors. It provides a simple and efficient way to create, manipulate, and convert colors in Rust applications.

Features

  • Create RGB colors
  • Interpolate between two colors
  • Convert hexadecimal color strings to RGB
  • Convert RGB colors to hexadecimal strings
  • Error handling for invalid color formats

Installation

Add this to your Cargo.toml:

[dependencies]
rustint = "0.1.0"

Usage

Creating a Color

use rustint::Color;

let red = Color::new(255, 0, 0);
let blue = Color::new(0, 0, 255);

// or

let red = Color::try_from("#FF0000").unwrap();
let blue = Color::try_from("#0000FF").unwrap();

Interpolating Between Colors

let red = Color::new(255, 0, 0);
let blue = Color::new(0, 0, 255);
let purple = red.interpolate(&blue, 50.0);
println!("Purple: {}", purple); // Outputs: Purple: #7F007F

Converting to Hex String

let color = Color::new(128, 64, 32);
println!("Hex: {}", color); // Outputs: Hex: #804020

Error Handling

The library provides custom error types for handling invalid color formats:

use rustint::{Color, ColorError};

let result = Color::try_from("#INVALID");
match result {
    Ok(color) => println!("Valid color: {}", color),
    Err(ColorError::InvalidFormat) => println!("Invalid format"),
    Err(ColorError::InvalidHexValue) => println!("Invalid hex value"),
}

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Dependencies

~265–730KB
~17K SLoC