Named Colors : version 0.1.1
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.
Features
- Retrieve RGB values by color name.
- Supports loading colors from a pre-defined JSON file or a user-provided JSON string.
- Handles a wide range of named colors (e.g.,
red,blue,green, etc.). - Error handling with custom error types for smooth integration.
Installation
You can include named_colors in your project by adding the following line to your Cargo.toml file.
From crates.io (Version Specified)
If the library is published on Crates.io, you can specify a version:
[]
= "0.1.0"
Or you can specify a more specific version like this:
[]
= { = "0.1.0" }
Example
Here is an example of how to use the library:
extern crate named_colors;
use get_color_by_name;
This will output:
RGB for navy: (0, 0, 128)
Synchronous Example: Retrieve RGB Values by Color Name
The library can be used to retrieve RGB values synchronously:
extern crate named_colors;
use ;
Asynchronous Example: Retrieve RGB Values
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 get_color_by_name;
use tokio; // Necessary to run async
async
This will output:
RGB for red is (255, 0, 0)
Handling Errors
The load_colors function returns a Result type with a custom error (NamedColorsError), allowing you to handle potential parsing errors gracefully:
use load_colors;
Available Colors
The library fetches color data from a JSON file hosted at a remote URL. It includes common colors such as:
- Red
- Green
- Blue
- Navy
- Magenta
- And many more...
The colors data is stored in a JSON file included in the library, ensuring quick access to color values at runtime.
Retrieving Colors by Name
You can use the get_color_by_name function to retrieve RGB values of a color by its name:
use ;
let color_map = load_colors.unwrap;
let rgb = get_color_by_name;
if let Some = rgb else
Case Insensitivity
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!;
assert_eq!;
Contributing
Feel free to open issues or submit pull requests if you'd like to contribute to this project.
- Fork the repository.
- Create a new branch for your feature.
- Make your changes and test thoroughly.
- Submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.