### InvalidHwb Error Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Provides examples of malformed HWB color strings that cause an InvalidHwb error, focusing on argument count, percentage limits, and syntax. ```text "hwb(0, 50%, 50%)" // Missing whiteness percentage "hwb(0 150% 0%)" // Whiteness > 100% "hwb(0 50% 60%)" // w + b > 100% (valid, creates gray) "hwb(0)" // Missing arguments ``` -------------------------------- ### Relative Color Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/parse.md Demonstrates creating new colors based on existing ones using the 'from' keyword and `calc()` expressions for color components. ```text rgb(from red r g calc(b + 20)) hsl(from gold h s calc(l + 10%)) hwb(from #bad455 calc(h + 35) w b) oklch(from purple l c h / 0.5) ``` -------------------------------- ### Usage Example for FromStr Trait Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/module-overview.md Demonstrates how to use the FromStr trait implementation to parse a hex color string into a Color object. ```rust let color: Color = "#ff0000".parse()?; ``` -------------------------------- ### Rust CSS Color Parsing Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Illustrates the various CSS color formats supported by the parser, including hex (3, 4, 6, 8 digits), RGB/RGBA, HSL/HSLA, modern functions (HWB, Lab, LCH, Oklab, Oklch), named colors, and relative colors. Use these examples to verify input string compatibility. ```rust // Hex parse("#fff") // 3-digit parse("#ffffff") // 6-digit parse("#ffff") // 4-digit with alpha parse("#ffffffff") // 8-digit with alpha // RGB/RGBA parse("rgb(255 0 0)") parse("rgb(255, 0, 0)") parse("rgba(255 0 0 / 0.5)") // HSL/HSLA parse("hsl(0deg 100% 50%)") parse("hsl(0, 100%, 50%)") parse("hsla(0deg 100% 50% / 50%)") // Modern functions parse("hwb(0 0% 0%)") parse("lab(50 20 30)") parse("lch(50 40 120deg)") parse("oklab(0.5 0.1 -0.1)") parse("oklch(0.5 0.2 120deg)") // Named colors (if named-colors feature enabled) parse("red") parse("darkblue") parse("aliceblue") // Relative colors (CSS Level 4) parse("rgb(from red r g calc(b + 20))") parse("hsl(from gold h s calc(l + 10%))") ``` -------------------------------- ### Parse Advanced Color Spaces in Rust Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/parse.md Examples of parsing colors from advanced color spaces like LAB, LCH, Oklab, and Oklch. ```Rust let color = parse("lab(50 20 30)")?; let color = parse("lch(50 40 120deg)")?; let color = parse("oklab(0.5 0.1 -0.1)")?; let color = parse("oklch(0.5 0.2 120deg)")?; ``` -------------------------------- ### Relative Color with calc() Expressions Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/parse.md Shows examples of using `calc()` with variables and basic arithmetic operations within relative color definitions. Supports nested `calc()` with parentheses. ```text rgb(from #bad455 255 g b) rgb(from #bad455 r g b / 0.5) rgb(from #bad455 calc(r+15) 90 b) rgb(from #bad455 calc((r+g)-30) 90 b) hwb(from rgb(from rgb(100% 0% 50%) r g 75) calc(h+25) w b) ``` -------------------------------- ### RGB Color Interpolation Example Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/interpolation.md Blends two colors in the sRGB color space. This is the fastest method and suitable for simple transitions where perceptual uniformity is not critical. ```rust let red = Color::from_rgba8(255, 0, 0, 255); let blue = Color::from_rgba8(0, 0, 255, 255); // 50/50 blend let purple = red.interpolate_rgb(&blue, 0.5); // Result: approximately [127, 0, 127, 255] // 25% toward blue let quarter = red.interpolate_rgb(&blue, 0.25); // Extrapolate: 150% toward blue (overshoots) let beyond = red.interpolate_rgb(&blue, 1.5); ``` -------------------------------- ### Basic Color List Parsing Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/ParseColors.md Demonstrates basic parsing of a comma-separated string containing valid color formats. Use this to get a vector of parsed colors. ```rust use csscolorparser::parse_colors; let input = "red, #00ff00, blue"; let results: Vec<_> = parse_colors(input).collect(); assert_eq!(results.len(), 3); assert!(results[0].is_ok()); ``` -------------------------------- ### InvalidHsv Error Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Lists invalid HSV color string inputs that lead to an InvalidHsv error, highlighting issues with argument counts and value ranges. ```text "hsv()" // No arguments "hsv(0, 150%, 100%)" // Saturation > 100% "hsv(0, 100%, 150%)" // Value > 100% "hsv(0)" // Missing saturation/value ``` -------------------------------- ### Add csscolorparser with std Feature Enabled Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Example of how to add the csscolorparser crate to your Cargo.toml, explicitly enabling the 'std' feature and disabling default features. ```toml [dependencies] csscolorparser = { version = "0.9", default-features = false, features = ["std"] } ``` -------------------------------- ### InvalidHex Error Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Illustrates common issues that lead to an InvalidHex error, such as incorrect length, invalid characters, or missing prefixes. ```text "#gg0000" // Invalid hex characters "#ff00" // Wrong length (4 required or 3 or 6 or 8) "#ff000000aa" // Too many digits "ff0000" // No # prefix (accepted in some contexts) ``` -------------------------------- ### Format Color as CSS HWB String Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Returns a CSS `hwb()` function representation as a display formatter. Call `.to_string()` to get the final string. ```rust pub fn to_css_hwb(&self) -> impl fmt::Display + fmt::Debug + '_ ``` -------------------------------- ### Get CSS String Representations of a Color Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/INDEX.md Illustrates how to obtain CSS-formatted string representations (hexadecimal, RGB, HSL) of a color object using `to_css_*` methods. ```rust println!("{}", color.to_css_hex().to_string()); println!("{}", color.to_css_rgb().to_string()); println!("{}", color.to_css_hsl().to_string()); ``` -------------------------------- ### InvalidLab Error Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Shows incorrect Lab color string formats that result in an InvalidLab error, including issues with argument separators, value ranges, and missing components. ```text "lab(50, 20, 30)" // Commas instead of spaces (optional in new spec) "lab(-10 20 30)" // Negative lightness "lab(200 20 30)" // Lightness > 100 "lab(50)" // Missing a and b values "lab(50 200 30)" // a-axis > 125 ``` -------------------------------- ### InvalidHsl Error Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Demonstrates invalid HSL color string formats that result in an InvalidHsl error, covering argument issues, range violations, and incorrect percentage usage. ```text "hsl()" // No arguments "hsl(0, 100%, 50%)" // Missing saturation percentage "hsl(0deg, 150%, 50%)" // Saturation > 100% "hsl(0, 100%, 150%)" // Lightness > 100% "hsl(0, 100%, 50%" // Missing closing paren "hsl(from red h s l l)" // Extra argument ``` -------------------------------- ### InvalidRgb Error Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Shows various malformed RGB color strings that trigger an InvalidRgb error, including incorrect argument counts, out-of-range values, and invalid syntax. ```text "rgb()" // No arguments "rgb(255)" // Only one argument "rgb(255, 0)" // Only two arguments "rgb(256, 0, 0)" // Value out of range (>255) "rgb(-1, 0, 0)" // Negative value "rgb(100%, 50%, 0%)" // Mixed percentage/absolute "rgb(from red r)" // Incomplete relative color "rgb(from red r g b)" // Missing slash/alpha ``` -------------------------------- ### Get CSS String Representation with csscolorparser-rs Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Demonstrates converting a Color object into various CSS string formats, including hex, rgb, hsl, and oklab. Also shows using the Display trait for a default string representation. ```rust println!("{}", color.to_css_hex().to_string()); // #ff0000 println!("{}", color.to_css_rgb().to_string()); // rgb(255 0 0) println!("{}", color.to_css_hsl().to_string()); // hsl(0 100% 50%) println!("{}", color.to_css_oklab().to_string()); // oklab(0.628 0.225 0.126) ``` ```rust // Or via Display println!("{}", color); // #ff0000 ``` -------------------------------- ### Get Color Values using csscolorparser-rs Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Illustrates how to extract color component values from a Color object in different formats: normalized RGBA, 8-bit RGBA, HSL, and OKLab. ```rust let [r, g, b, a] = color.to_array(); // Normalized [0..1] let [r, g, b, a] = color.to_rgba8(); // 8-bit [0..255] let [h, s, l, a] = color.to_hsla(); // HSL let [l, a, b, alpha] = color.to_oklaba(); // OKLab ``` -------------------------------- ### Minimal Build Configuration Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Configure a minimal build for csscolorparser with no_std support and only hex color parsing. This results in a smaller binary size (~15KB). ```toml csscolorparser = { version = "0.9", default-features = false } ``` -------------------------------- ### Format Color as CSS HSL String Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Use `to_css_hsl` to get a CSS `hsl()` function representation. This method returns a display formatter; call `.to_string()` to get the final string. ```rust let color = Color::from_rgba8(255, 0, 0, 255); assert_eq!(color.to_css_hsl().to_string(), "hsl(0 100% 50%)"); ``` -------------------------------- ### Format Color as CSS Hex String Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Use `to_css_hex` to get a CSS hexadecimal color representation. This method returns a display formatter, so call `.to_string()` to get the final string. ```rust let color = Color::from_rgba8(255, 0, 0, 255); assert_eq!(color.to_css_hex().to_string(), "#ff0000"); let color = Color::from_rgba8(255, 0, 0, 127); assert_eq!(color.to_css_hex().to_string(), "#ff00007f"); ``` -------------------------------- ### Standard Build Configuration Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Use the standard build configuration for csscolorparser, which includes named colors but excludes rust-rgb, cint, and serde features. This is the default choice for most applications and results in a ~30KB binary. ```toml csscolorparser = { version = "0.9" } ``` -------------------------------- ### Full Features Build Configuration Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Enable all features for csscolorparser, including std, named-colors, rust-rgb, cint, and serde. This provides complete interoperability but results in a larger binary size (~50KB). ```toml csscolorparser = { version = "0.9", features = ["std", "named-colors", "rust-rgb", "cint", "serde"] } ``` -------------------------------- ### Create a Color Gradient using Interpolation Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/INDEX.md Shows how to create a color gradient by interpolating between two colors (red and blue) in the OKLab color space over 10 steps. ```rust let start = parse("red")?; let end = parse("blue")?; for i in 0..=10 { let t = i as f32 / 10.0; let color = start.interpolate_oklab(&end, t); } ``` -------------------------------- ### Add csscolorparser with Default Features (including named-colors) Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Example of adding csscolorparser to Cargo.toml. The 'named-colors' feature is enabled by default. ```toml [dependencies] csscolorparser = { version = "0.9", features = ["std"] } ``` -------------------------------- ### Web/API Use Build Configuration Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Configure csscolorparser for web or API use, enabling named-colors and serde features. This allows parsing colors from web requests and serializing for JSON responses without external crate dependencies. ```toml csscolorparser = { version = "0.9", features = ["named-colors", "serde"] } ``` -------------------------------- ### Project Index File Structure Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/README.md Overview of the generated documentation's file structure for the csscolorparser-rs project. ```text output/ ├── README.md (This file) ├── INDEX.md (Master navigation) ├── quick-reference.md (Fast lookup) ├── types.md (Type definitions) ├── errors.md (Error reference) ├── configuration.md (Features & config) ├── module-overview.md (Architecture) └── api-reference/ ├── Color.md (Main type) ├── parse.md (Parsing function) ├── ParseColors.md (Batch iterator) ├── color-spaces.md (Conversions) └── interpolation.md (Blending) ``` -------------------------------- ### Add csscolorparser with named-colors Feature Disabled Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Example of adding csscolorparser to Cargo.toml while explicitly disabling the 'named-colors' feature for a minimal binary size. ```toml [dependencies] csscolorparser = { version = "0.9", features = ["std"], default-features = false } ``` -------------------------------- ### Format Color as CSS oklab String Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Returns a CSS `oklab()` function representation as a display formatter. ```rust pub fn to_css_oklab(&self) -> impl fmt::Display + fmt::Debug + '_ ``` -------------------------------- ### Convert to RGBA8, RGBA16, and Array Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Convert a color to its RGBA8, RGBA16, or array representation. Useful for getting raw color component values. ```rust let color = Color::from_rgba8(255, 0, 0, 255); let [r, g, b, a] = color.to_rgba8(); let [r, g, b, a] = color.to_rgba16(); let [r, g, b, a] = color.to_array(); ``` -------------------------------- ### Create Color from OKLab Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Constructs a Color instance from the OKLab color space, which is perceptually uniform and suitable for color manipulation. ```rust let color = Color::from_oklaba(0.5, 0.1, -0.1, 1.0); ``` -------------------------------- ### Create Color Directly with csscolorparser-rs Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Shows how to create Color objects directly using normalized RGBA values, 8-bit RGBA values, HSL, and OKLab color spaces. ```rust let color = Color::new(1.0, 0.0, 0.0, 1.0); // Normalized [0..1] let color = Color::from_rgba8(255, 0, 0, 255); // 8-bit [0..255] let color = Color::from_hsla(0.0, 1.0, 0.5, 1.0); // HSL let color = Color::from_oklaba(0.5, 0.1, -0.1, 1.0); // OKLab ``` -------------------------------- ### LCh Interpolation Example Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/interpolation.md Blends colors in the LCh color space, which is perceptually uniform and suitable for hue-based transitions using the shortest path on the hue circle. ```rust use std::f32::consts::PI; let red = Color::from_lcha(50.0, 50.0, 0.0, 1.0); let green = Color::from_lcha(50.0, 50.0, PI / 3.0, 1.0); // Hue smoothly transitions while maintaining lightness let blend = red.interpolate_lch(&green, 0.5); // Result has same lightness as inputs, hue at 30° ``` -------------------------------- ### to_css_oklab Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Returns a CSS `oklab()` function representation. ```APIDOC ## to_css_oklab ### Description Returns a CSS `oklab()` function representation. ### Method `pub fn to_css_oklab(&self) -> impl fmt::Display + fmt::Debug + '_` ### Return Display formatter ``` -------------------------------- ### InvalidLch Error Examples Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Demonstrates malformed LCh color strings that trigger an InvalidLch error, covering incorrect separators, out-of-range values, and missing arguments. ```text "lch(50, 40, 120)" // Commas (should be spaces) "lch(-10 40 120)" // Negative lightness "lch(50 200 120)" // Chroma > 150 "lch(50 40)" // Missing hue "lch(50 40 400deg)" // Hue wraps, typically accepted ``` -------------------------------- ### Module Structure Overview Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/INDEX.md Illustrates the hierarchical organization of the csscolorparser-rs crate's modules. This helps in understanding the codebase structure and locating specific functionalities. ```text csscolorparser (root) ├── Color (struct) ├── parse() (function) ├── ParseColors (iterator) ├── ParseColorError (enum) ├── NAMED_COLORS (static) └── Internal modules ├── parser.rs - parsing logic ├── color.rs - Color impl ├── error.rs - error types ├── named_colors.rs - color names ├── lab.rs - Lab/OKLab math └── utils/ - helpers ``` -------------------------------- ### Display Error Messages with Display Trait Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Use the Display trait to get user-friendly error messages for parsing failures. This is useful for displaying errors to end-users. ```rust use csscolorparser::parse; if let Err(e) = parse("invalid") { eprintln!("Error: {}", e); // Prints: "Error: invalid unknown format" } ``` -------------------------------- ### Convert Color Spaces and Create Colors Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/INDEX.md Demonstrates converting a color to its HSLA components and creating a new color from OKLaba values. ```rust let [h, s, l, a] = color.to_hsla(); let new_color = Color::from_oklaba(0.5, 0.1, -0.1, 1.0); ``` -------------------------------- ### Configure csscolorparser in Cargo.toml Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Shows different ways to add the csscolorparser crate to your Cargo.toml file, including default, minimal (no-std), and with specific features like 'serde'. ```toml # Default (std + named-colors) csscolorparser = "0.9" # Minimal (no-std, no named colors) csscolorparser = { version = "0.9", default-features = false } # With serde support csscolorparser = { version = "0.9", features = ["serde"] } # Full features csscolorparser = { version = "0.9", features = ["std", "named-colors", "rust-rgb", "cint", "serde"] } ``` -------------------------------- ### Converting Color to CSS String Formats Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/types.md Illustrates converting a `Color` object into various CSS string representations like hex, RGB, HSL, HWB, Oklab, Oklch, Lab, and Lch. These methods return opaque display wrappers. ```rust impl Color { pub fn to_css_hex(&self) -> impl fmt::Display + fmt::Debug + '_; pub fn to_css_rgb(&self) -> impl fmt::Display + fmt::Debug + '_; pub fn to_css_hsl(&self) -> impl fmt::Display + fmt::Debug + '_; pub fn to_css_hwb(&self) -> impl fmt::Display + fmt::Debug + '_; pub fn to_css_oklab(&self) -> impl fmt::Display + fmt::Debug + '_; pub fn to_css_oklch(&self) -> impl fmt::Display + fmt::Debug + '_; pub fn to_css_lab(&self) -> impl fmt::Display + fmt::Debug + '_; pub fn to_css_lch(&self) -> impl fmt::Display + fmt::Debug + '_; } ``` ```rust let color = Color::from_rgba8(255, 0, 0, 255); let hex_str = color.to_css_hex().to_string(); // "#ff0000" let rgb_str = color.to_css_rgb().to_string(); // "rgb(255 0 0)" ``` -------------------------------- ### Module Hierarchy Structure Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/module-overview.md This tree structure outlines the organization of modules within the csscolorparser-rs library, starting from the root `lib.rs` and detailing public and private modules. ```text csscolorparser (lib.rs - root module) ├── Color (pub struct) ├── ParseColorError (pub enum) ├── parse (pub fn) ├── ParseColors (pub struct) ├── ParseColorsError (pub struct) ├── NAMED_COLORS (pub static, cfg: named-colors) ├── color (private module) ├── error (private module) ├── parser (private module) ├── parse_colors (private module) ├── named_colors (private module, cfg: named-colors) ├── cint (private module, cfg: cint) ├── lab (private module) └── utils (private module) ├── calc (private submodule) ├── helper (private submodule) └── param (private submodule) ``` -------------------------------- ### Secondary Entry Point: Color::from_html() Constructor Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/module-overview.md An alternative parsing method implemented as a constructor for the Color type. It delegates to the primary parse() function internally. ```rust pub fn from_html>(s: S) -> Result ``` -------------------------------- ### Color::from_oklaba Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Creates a Color instance from the OKLab color space. ```APIDOC ## Color::from_oklaba ### Description Creates a Color from OKLab color space. OKLab is a perceptually uniform color space designed for color manipulation. ### Method `fn from_oklaba(l: f32, a: f32, b: f32, alpha: f32) -> Self` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **l** (f32) - Required - Perceived lightness [0..1] - **a** (f32) - Required - Green-red axis [-0.4..0.4] - **b** (f32) - Required - Blue-yellow axis [-0.4..0.4] - **alpha** (f32) - Required - Alpha [0..1] ### Request Example ```rust let color = Color::from_oklaba(0.5, 0.1, -0.1, 1.0); ``` ### Response #### Success Response - **Color** - The created color instance #### Response Example None provided. ``` -------------------------------- ### Get Color Name with csscolorparser-rs (Named Colors Feature) Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Shows how to retrieve the common name of a color if the 'named-colors' feature is enabled. This is useful for identifying predefined colors. ```rust #[cfg(feature = "named-colors")] { if let Some(name) = color.name() { println!("Color name: {}", name); // e.g., "red" } } ``` -------------------------------- ### from_oklaba Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/color-spaces.md Creates a Color object from OKLab values (lightness, green-red axis, blue-yellow axis, alpha). ```APIDOC ## from_oklaba ### Description Creates Color from OKLab values. ### Method Signature ```rust pub fn from_oklaba(l: f32, a: f32, b: f32, alpha: f32) -> Self ``` ### Parameters #### Path Parameters - **l** (f32) - Required - Perceived lightness [0..1] - **a** (f32) - Required - Green-red axis [-0.4..0.4] - **b** (f32) - Required - Blue-yellow axis [-0.4..0.4] - **alpha** (f32) - Required - Alpha [0..1] ### Return `Color` ### Example ```rust let mid_gray = Color::from_oklaba(0.5, 0.0, 0.0, 1.0); let light_gray = Color::from_oklaba(0.75, 0.0, 0.0, 1.0); let dark_gray = Color::from_oklaba(0.25, 0.0, 0.0, 1.0); ``` ``` -------------------------------- ### Animate Color Transitions Between Two Colors Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/interpolation.md Generates a sequence of colors representing a smooth transition between a starting and ending color over a specified number of frames. Useful for animations. ```rust use csscolorparser::{parse, Color}; fn animate_color_transition( from: &str, to: &str, frames: usize, ) -> Result, Box> { let c_from = parse(from)?; let c_to = parse(to)?; let mut frames_out = Vec::new(); for i in 0..frames { let t = i as f32 / (frames - 1) as f32; frames_out.push(c_from.interpolate_oklab(&c_to, t)); } Ok(frames_out) } // Usage: Create 30-frame transition from red to blue let frames = animate_color_transition("red", "blue", 30)?; ``` -------------------------------- ### Rust Usage with serde for Serialization/Deserialization Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Demonstrates serializing and deserializing a struct containing Color types using serde_json. Requires the 'serde' feature to be enabled. Serialization outputs hex strings, while deserialization accepts various color formats. ```rust use csscolorparser::Color; use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] struct Config { primary_color: Color, secondary_color: Color, } let json = r"#ff0000","secondary_color":"rgb(0 255 0)"}"; let config: Config = serde_json::from_str(json)?; let serialized = serde_json::to_string(&config)?; ``` -------------------------------- ### Parse Basic Hex Colors in Rust Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/parse.md Demonstrates parsing of standard and shorthand hex color codes, with and without the leading '#'. ```Rust use csscolorparser::parse; let red = parse("#ff0000")?; let red = parse("ff0000")?; let red_hex4 = parse("#f00")?; assert_eq!(red.to_rgba8(), [255, 0, 0, 255]); ``` -------------------------------- ### Perceptually Uniform Blend (OKLab) Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Perform a color blend using the Oklab color space, which is perceptually uniform. This provides more natural-looking transitions between colors and is recommended for most blending tasks. ```rust let red = Color::from_rgba8(255, 0, 0, 255); let blue = Color::from_rgba8(0, 0, 255, 255); let blend = red.interpolate_oklab(&blue, 0.5); // Best for most cases ``` -------------------------------- ### Creating Colors Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/INDEX.md Methods for creating Color objects, including direct construction, parsing from RGBA values, and parsing CSS strings. ```APIDOC ## Creating Colors ### `Color::new()` **Purpose:** Direct construction with normalized RGBA. ### `Color::from_rgba8()` **Purpose:** Create a color from 8-bit RGBA values. ### `Color::from_html()` **Purpose:** Parse a CSS color string. ### `parse()` **Purpose:** Top-level parsing function for CSS color strings. ### `Color::from_*()` methods **Purpose:** Color space constructors for various color representations. ``` -------------------------------- ### Color::new Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Creates a new Color instance from normalized RGBA floating-point values. ```APIDOC ## Color::new ### Description Creates a new Color from normalized RGBA values [0..1]. ### Method `const fn new(r: f32, g: f32, b: f32, a: f32) -> Self` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **r** (f32) - Required - Red value [0..1] - **g** (f32) - Required - Green value [0..1] - **b** (f32) - Required - Blue value [0..1] - **a** (f32) - Required - Alpha value [0..1] ### Request Example ```rust let color = Color::new(1.0, 0.0, 0.0, 1.0); // Pure red assert_eq!(color.to_rgba8(), [255, 0, 0, 255]); ``` ### Response #### Success Response - **Color** - A new color instance #### Response Example None provided. ``` -------------------------------- ### Enable serde Feature Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Enable the serde feature for serialization and deserialization of Color using the serde framework. This is ideal for reading/writing colors in configuration files or for API serialization. ```toml [features] serde = ["dep:serde"] ``` ```toml [dependencies] csscolorparser = { version = "0.9", features = ["serde"] } serde_json = "1.0" ``` -------------------------------- ### Debug Error Variants with Debug Trait Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/errors.md Implement the Debug trait for error variants to get detailed developer output. This is helpful for debugging and understanding the specific error encountered. ```rust use csscolorparser::parse; if let Err(e) = parse("invalid") { println!("{:?}", e); // Prints: InvalidUnknown } ``` -------------------------------- ### Get Normalized RGB Array Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/color-spaces.md Returns normalized [r, g, b, a] values in the range [0..1]. The values are clamped to ensure they stay within the valid range. ```rust let color = Color::new(0.5, 0.3, 0.7, 0.9); let arr = color.to_array(); assert_eq!(arr, [0.5, 0.3, 0.7, 0.9]); ``` -------------------------------- ### Format Color as CSS oklch String Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Returns a CSS `oklch()` function representation as a display formatter. ```rust pub fn to_css_oklch(&self) -> impl fmt::Display + fmt::Debug + '_ ``` -------------------------------- ### Convert Color to RGBA Array [0..1] Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Use `to_array` to get the color components as a normalized [r, g, b, a] array, with values clamped between 0.0 and 1.0. ```rust let color = Color::new(1.0, 0.5, 0.0, 0.8); assert_eq!(color.to_array(), [1.0, 0.5, 0.0, 0.8]); ``` -------------------------------- ### Get CSS Color Name Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md The `name` method returns the CSS color name if the color exactly matches a predefined name, ignoring the alpha channel. Requires the `named-colors` feature. ```rust let color = Color::from_rgba8(255, 0, 0, 255); assert_eq!(color.name(), Some("red")); let color = Color::from_rgba8(255, 0, 0, 128); assert_eq!(color.name(), Some("red")); // Alpha ignored ``` -------------------------------- ### From Conversions Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Supports conversions from various tuple, array, and struct types representing RGBA or RGB color values. ```APIDOC ## From Conversions ### Description The `Color` type supports conversions from a wide range of input formats, including tuples, arrays, and types from external crates like `rust-rgb` and `cint`. ### Supported Input Types - `(f32, f32, f32, f32)` - Normalized RGBA - `(f32, f32, f32)` - Normalized RGB (alpha = 1.0) - `[f32; 4]` - Normalized RGBA array - `[f32; 3]` - Normalized RGB array - `[f64; 4]` - Double-precision RGBA - `[f64; 3]` - Double-precision RGB - `(u8, u8, u8, u8)` - 8-bit RGBA - `(u8, u8, u8)` - 8-bit RGB (alpha = 255) - `[u8; 4]` - 8-bit RGBA array - `[u8; 3]` - 8-bit RGB array - `RGB` - From `rust-rgb` crate (requires `rust-rgb` feature) - `RGBA` - From `rust-rgb` crate (requires `rust-rgb` feature) - `EncodedSrgb` - From `cint` crate (requires `cint` feature) - `Alpha>` - From `cint` crate (requires `cint` feature) ``` -------------------------------- ### Parse RGB and RGBA Functions in Rust Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/parse.md Illustrates parsing of RGB and RGBA color functions, supporting both comma-separated and space-separated values, including percentages. ```Rust let red = parse("rgb(255, 0, 0)")?; let red = parse("rgb(255 0 0)")?; let red_pct = parse("rgb(100% 0% 0%)")?; let red_alpha = parse("rgba(255 0 0 / 0.8)")?; assert_eq!(red.to_rgba8(), [255, 0, 0, 255]); ``` -------------------------------- ### Parse Complex Colors with Functions Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/ParseColors.md Parses a list of colors that include functional notations like rgb(), hsl(), and hex codes. This example shows how to handle a mix of standard and functional color formats. ```rust use csscolorparser::parse_colors; let input = "rgb(255, 0, 0), hsl(120deg, 100%, 50%), #0000ff"; let colors: Result, _> = parse_colors(input).collect(); match colors { Ok(c) => println!("Parsed {} colors", c.len()), Err(e) => println!("Error: {}", e), } ``` -------------------------------- ### Rust Usage with rgb Crate Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Demonstrates converting between csscolorparser Color and rgb crate types. Requires the 'rust-rgb' feature to be enabled. ```rust use csscolorparser::Color; use rgb::RGB; // Convert from rgb crate let rgb = RGB::new(1.0, 0.0, 0.0); let color = Color::from(rgb); // Convert to rgb crate let rgb: RGB = color.into(); ``` -------------------------------- ### Parsing Relative Color Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/module-overview.md Details the parsing of a relative color string, which involves a base color and calculations for its components. This example shows how 'rgb(from red r g calc(b + 20))' is processed, including recursive parsing of the base color and evaluation of the 'calc' expression. ```text "rgb(from red r g calc(b + 20))" ↓ parse() in parser.rs ↓ parse_abs() fails (not standard rgb) ↓ Detects "from" keyword ↓ Recursively parse("red") -> Color { r: 1.0, g: 0.0, b: 0.0, a: 1.0 } ↓ Setup variables: r=255, g=0, b=0, alpha=1.0 ↓ ParamParser extracts: val1="r", val2="g", val3="calc(b + 20)", val4="alpha" ↓ parse_values() evaluates calc(0 + 20) = 20 ↓ Color::new(255/255, 0/255, 20/255, 1.0) ↓ Color { r: 1.0, g: 0.0, b: 0.078, a: 1.0 } ``` -------------------------------- ### Rust Color to CSS Function Syntax Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/color-spaces.md Demonstrates converting a color to various CSS function syntaxes including hex, RGB, and HSL. Ensure the Color type is properly initialized before calling these methods. ```rust pub fn to_css_hex(&self) -> impl Display + Debug + '_; pub fn to_css_rgb(&self) -> impl Display + Debug + '_; pub fn to_css_hsl(&self) -> impl Display + Debug + '_; pub fn to_css_hwb(&self) -> impl Display + Debug + '_; pub fn to_css_oklab(&self) -> impl Display + Debug + '_; pub fn to_css_oklch(&self) -> impl Display + Debug + '_; pub fn to_css_lab(&self) -> impl Display + Debug + '_; pub fn to_css_lch(&self) -> impl Display + Debug + '_; ``` ```rust let color = Color::from_rgba8(255, 100, 50, 200); println!("{}", color.to_css_hex().to_string()); // Output: #ff6432c8 println!("{}", color.to_css_rgb().to_string()); // Output: rgb(255 100 50 / 0.784) println!("{}", color.to_css_hsl().to_string()); // Output: hsl(15 100% 60% / 0.784) ``` -------------------------------- ### Parse Various Color Formats Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/INDEX.md Demonstrates parsing colors from hexadecimal, RGB, and named color strings. Assumes the `parse` function is available and error handling is managed. ```rust let color = parse("#ff0000")?; let color = parse("rgb(255, 0, 0)")?; let color = parse("red")?; ``` -------------------------------- ### Create Color from Lab Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Creates a Color object from the Lab color space, a device-independent color model. ```rust pub fn from_laba(l: f32, a: f32, b: f32, alpha: f32) -> Self ``` -------------------------------- ### Parse HSL and HSLA Functions in Rust Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/parse.md Demonstrates parsing of HSL and HSLA color functions, including support for degrees and percentages, and modern space-separated formats. ```Rust let red = parse("hsl(0deg, 100%, 50%)")?; let red = parse("hsl(0 100% 50%)")?; let red_alpha = parse("hsl(0deg 100% 50% / 50%)")?; assert_eq!(red.to_rgba8()[0], 255); // Red component ``` -------------------------------- ### Parse Color String using csscolorparser-rs Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Demonstrates various methods to parse color strings into a Color object, including direct parsing, using the FromStr trait, and the Color::from_html method. Assumes a context where '?' can be used for error propagation. ```rust // Most flexible - returns Result let color = parse("#ff0000")?; let color = parse("rgb(255, 0, 0)")?; let color = parse("red")?; ``` ```rust // Via FromStr trait let color: Color = "#ff0000".parse()?; ``` ```rust // Via method let color = Color::from_html("#ff0000")?; ``` -------------------------------- ### to_oklaba Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Converts the color to the OKLab color space, returning [l, a, b, alpha]. ```APIDOC ## to_oklaba ### Description Converts to OKLab color space, returning [l, a, b, alpha]. ### Method `pub fn to_oklaba(&self) -> [f32; 4]` ### Return `[f32; 4]` ``` -------------------------------- ### Release Profile Configuration for Reduced Binary Size Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Configure the release profile in Cargo.toml to enable Link Time Optimization (LTO) and reduce the number of codegen units, which helps minimize the final binary size. ```toml [profile.release] lto = true codegen-units = 1 ``` -------------------------------- ### Create Color from OKLCH Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Constructs a Color object using the OKLCH color space, which is a cylindrical representation of OKLab. ```rust pub fn from_oklcha(l: f32, c: f32, h: f32, alpha: f32) -> Self ``` -------------------------------- ### Tertiary Entry Point: FromStr Trait Implementation Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/module-overview.md Enables parsing of color strings using the standard library's FromStr trait, allowing for idiomatic parsing with the .parse() method. ```rust impl FromStr for Color { type Err = ParseColorError; fn from_str(s: &str) -> Result } ``` -------------------------------- ### Parse Relative Colors in Rust Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/parse.md Demonstrates parsing relative color syntax, allowing modification of existing colors or creation of new ones based on a base color. ```Rust // Create a color based on red, but with green+20 let color = parse("rgb(from red r g calc(b + 20))")?; // Lighten a color by 10% let color = parse("hsl(from gold h s calc(l + 10%))")?; // Shift hue and add transparency let color = parse("hwb(from #bad455 calc(h + 35) w b)")?; // Change opacity let color = parse("oklch(from purple l c h / 0.5)")?; ``` -------------------------------- ### Convert to OKLab and OKLCh Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/quick-reference.md Convert colors to OKLab and OKLCh (cylindrical OKLab) color spaces. These are modern, perceptually uniform color spaces recommended for most applications. ```rust use std::f32::consts::PI; let color = Color::from_oklaba(0.5, 0.1, -0.1, 1.0); let [l, a, b, alpha] = color.to_oklaba(); let color = Color::from_oklcha(0.5, 0.15, PI / 3.0, 1.0); let [l, c, h, a] = color.to_oklcha(); ``` -------------------------------- ### Generate a Temperature Color Scale Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/interpolation.md Creates a color scale that transitions from blue (cold) to green (neutral) to red (hot) based on a float value between 0.0 and 1.0. Suitable for data visualization. ```rust use csscolorparser::Color; fn temperature_color(value: f32) -> Color { let cold = Color::from_rgba8(0, 0, 255, 255); // Blue let neutral = Color::from_rgba8(0, 255, 0, 255); // Green let hot = Color::from_rgba8(255, 0, 0, 255); // Red if value < 0.5 { // Map [0..0.5] to [cold..neutral] let t = value * 2.0; cold.interpolate_oklab(&neutral, t) } else { // Map [0.5..1] to [neutral..hot] let t = (value - 0.5) * 2.0; neutral.interpolate_oklab(&hot, t) } } // Usage let color_0_0 = temperature_color(0.0); // Blue (cold) let color_0_5 = temperature_color(0.5); // Green (neutral) let color_1_0 = temperature_color(1.0); // Red (hot) ``` -------------------------------- ### Enable Features for Docs.rs Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/configuration.md Configure Cargo metadata to enable specific features when building documentation for docs.rs. This ensures all types and methods are included in the online API documentation. ```toml [package.metadata.docs.rs] features = ["named-colors", "rust-rgb", "cint", "serde", "std"] ``` -------------------------------- ### Convert Color to OKLab Array Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Converts the color to the OKLab color space, returning an array [l, a, b, alpha]. ```rust pub fn to_oklaba(&self) -> [f32; 4] ``` -------------------------------- ### Utilities Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/INDEX.md Utility functions for color manipulation and access. ```APIDOC ## Utilities ### `clamp()` **Purpose:** Restrict color channels to the range [0..1]. ### `name()` **Purpose:** Get the CSS color name if applicable. ### `parse_colors()` **Purpose:** Batch parse a list of CSS color strings. ### `NAMED_COLORS` **Purpose:** Access the map of named CSS colors. ``` -------------------------------- ### to_css_hwb Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/Color.md Returns a CSS `hwb()` function representation. ```APIDOC ## to_css_hwb ### Description Returns a CSS `hwb()` function representation. ### Method `pub fn to_css_hwb(&self) -> impl fmt::Display + fmt::Debug + '_` ### Return Display formatter ``` -------------------------------- ### Create Color from Lab Values Source: https://github.com/mazznoer/csscolorparser-rs/blob/master/_autodocs/api-reference/color-spaces.md Use `from_laba` to create a Color object from Lab and alpha values. This is useful for perceptual uniformity and device-independent color specifications. ```rust let mid_gray = Color::from_laba(50.0, 0.0, 0.0, 1.0); let light_gray = Color::from_laba(75.0, 0.0, 0.0, 1.0); let dark_gray = Color::from_laba(25.0, 0.0, 0.0, 1.0); // Reddish color let red_tinted = Color::from_laba(50.0, 50.0, 0.0, 1.0); ```