| Crates.io | named_colors |
| lib.rs | named_colors |
| version | 0.1.1 |
| created_at | 2024-10-05 23:13:47.389147+00 |
| updated_at | 2024-10-08 22:01:51.637263+00 |
| description | A Rust library that provides named colors in RGB and Hexadecimal formats. |
| homepage | https://github.com/teamflp/named_colors |
| repository | https://github.com/teamflp/named_colors |
| max_upload_size | |
| id | 1398552 |
| size | 35,388 |
named_colors is a Rust library that provides named colors with their RGB values. The library allows you to retrieve RGB values by color name or use these values in different applications.
red, blue, green, etc.).You can include named_colors in your project by adding the following line to your Cargo.toml file.
If the library is published on Crates.io, you can specify a version:
[dependencies]
named_colors = "0.1.0"
Or you can specify a more specific version like this:
[dependencies]
named_colors = { version = "0.1.0" }
Here is an example of how to use the library:
extern crate named_colors;
use named_colors::get_color_by_name;
fn main() {
let color_name = "navy";
match get_color_by_name(color_name) {
Some((r, g, b)) => {
println!("RGB for {}: ({}, {}, {})", color_name, r, g, b);
}
None => {
println!("Color '{}' not found.", color_name);
}
}
}
This will output:
RGB for navy: (0, 0, 128)
The library can be used to retrieve RGB values synchronously:
extern crate named_colors;
use named_colors::{get_color_by_name, load_colors};
fn main() {
// Load the color map once.
let color_map = load_colors().unwrap();
// Get the RGB values for a color by its name.
let color_name = "navy";
match get_color_by_name(&color_map, color_name) {
Some((r, g, b)) => {
println!("RGB for {}: ({}, {}, {})", color_name, r, g, b);
}
None => {
println!("Color '{}' not found.", color_name);
}
}
}
If you need to work asynchronously, for instance when downloading the color data from a remote source, here's how to use the library:
use named_colors::get_color_by_name;
use tokio; // Necessary to run async
#[tokio::main]
async fn main() {
let color_name = "red";
let red_rgb = get_color_by_name(color_name).await;
if let Some((r, g, b)) = red_rgb {
println!("RGB for {}: ({}, {}, {})", color_name, r, g, b);
}
}
This will output:
RGB for red is (255, 0, 0)
The load_colors function returns a Result type with a custom error (NamedColorsError), allowing you to handle potential parsing errors gracefully:
use named_colors::colors::load_colors;
fn main() {
match load_colors() {
Ok(colors_map) => {
println!("Colors loaded successfully.");
}
Err(err) => {
eprintln!("Failed to load colors: {}", err);
}
}
}
The library fetches color data from a JSON file hosted at a remote URL. It includes common colors such as:
The colors data is stored in a JSON file included in the library, ensuring quick access to color values at runtime.
You can use the get_color_by_name function to retrieve RGB values of a color by its name:
use named_colors::colors::{get_color_by_name, load_colors};
let color_map = load_colors().unwrap();
let rgb = get_color_by_name(&color_map, "chartreuse");
if let Some((r, g, b)) = rgb {
println!("RGB: {}, {}, {}", r, g, b);
} else {
println!("Color not found");
}
The get_color_by_name function is case-insensitive, meaning that "Red" and "red" will yield the same result:
let color_map = load_colors().unwrap();
assert_eq!(get_color_by_name(&color_map, "Red"), Some((255, 0, 0)));
assert_eq!(get_color_by_name(&color_map, "red"), Some((255, 0, 0)));
Feel free to open issues or submit pull requests if you'd like to contribute to this project.
This project is licensed under the MIT License - see the LICENSE file for details.