colourss 0.1.0

A Rust library for parsing CSS color strings into RGB values.
Documentation
  • Coverage
  • 7.69%
    1 out of 13 items documented0 out of 3 items with examples
  • Size
  • Source code size: 33.81 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.69 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 46s Average build duration of successful builds.
  • all releases: 36s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • tumelonito

ColourSS

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.

Technical Description

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:

  1. Hex
  2. RGB/RGBA
  3. HSL/HSLA
  4. Named Color

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).

Grammar Rules

The parser understands the following formats: ::= | | |

  1. Hex: <hex-color> ::= '#__{3,4,6,8}__'

    • #rgb (e.g., #f03)
    • #rgba (e.g., #f03a)
    • #rrggbb (e.g., #ff0033)
    • #rrggbbaa (e.g., #ff0033aa)
  2. RGB(A): <rgb-color> ::= 'rgb(' <number> ',' <number> ',' <number> ')' | 'rgba(' ... ')'

    • rgb(255, 100, 0)
    • rgba(255, 100, 0, 0.5)
  3. HSL(A): <hsl-color> ::= 'hsl(' <hue> ',' <percent> ',' <percent> ')' | 'hsla(' ... ')'

    • hsl(120, 100%, 50%)
    • hsla(120, 100%, 50%, 1.0)
  4. Named: <named-color> ::= 'red' | 'blue' | ...

    • red, green, blue, white, black, yellow, rebeccapurple
    • This is case-insensitive.

(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.)

How to Use the Result

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:

  • A game engine needing to set entity colors.
  • A terminal application that wants to style its output.
  • A web server templating engine that processes stylesheets.

Command-Line Interface (CLI)

This project also includes a CLI app.

cargo run --help

Commands:

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.