| Crates.io | colourss |
| lib.rs | colourss |
| version | 0.1.1 |
| created_at | 2025-11-12 18:47:48.336478+00 |
| updated_at | 2025-11-14 21:04:34.153167+00 |
| description | A Rust library for parsing CSS color strings into RGB values. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1929763 |
| size | 40,247 |
A Rust library for parsing CSS color strings into RGB values.
This project provides a library and a command-line tool to parse various CSS color formats (Hex, RGB, HSL, Named) into a simple RGB struct.
The package is published on creates.io
cargo install colourss
git clone https://github.com/tumelonito/ColourSS.git
cd ColourSS
cargo build --release
This library provides a single main function, parse_color(input: &str),
which takes a string slice and attempts to parse it into a
Color { r: u8, g: u8, b: u8 } struct.
The parser works by trying to match the input string against a set of predefined grammar rules. It checks in this order:
If a match is found, it processes the string and returns the Ok(Color).
If no rule matches, or if the format is invalid (e.g., wrong number of
components, bad numbers), it returns an Err(ParseError).
The parser understands the following formats:
Hex: <hex-color> ::= '#__{3,4,6,8}__'
#rgb (e.g., #f03)#rgba (e.g., #f03a)#rrggbb (e.g., #ff0033)#rrggbbaa (e.g., #ff0033aa)RGB(A): <rgb-color> ::= 'rgb(' <number> ',' <number> ',' <number> ')' | 'rgba(' ... ')'
rgb(255, 100, 0)rgba(255, 100, 0, 0.5)HSL(A): <hsl-color> ::= 'hsl(' <hue> ',' <percent> ',' <percent> ')' | 'hsla(' ... ')'
hsl(120, 100%, 50%)hsla(120, 100%, 50%, 1.0)Named: <named-color> ::= 'red' | 'blue' | ...
red, green, blue, white, black, yellow, rebeccapurple(Note: For rgba and hsla formats, the alpha component is parsed
to ensure the format is valid, but it is discarded in the final Color
struct, as per the requirements.)
The resulting Color struct is a simple data container:
#[derive(Debug, PartialEq)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}
This struct can be used by any Rust application that needs to work with colors, such as:
This project also includes a CLI app.
cargo run --help
cargo run --parse <path/to/file.txt>
This command will read the specified file and try to parse each line as a color. It will print the result for each line.
Example colors.txt: #ff0000 blue hsl(120, 100%, 50%) not a color rgb(10, 20, 30)
cargo run --credits
Displays author and license information.