1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
// (c) Dean McNamee <dean@gmail.com>, 2012. // (c) Rust port by Katkov Oleksandr <alexx.katkoff@gmail.com>, 2016. // // https://github.com/deanm/css-color-parser-js // https://github.com/7thSigil/css-color-parser-rs // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. //! Easy-to-use Rust parser for CSS3 color strings.<br> //! Lightweight.<br> //! Reliable (Provides tests, handles all errors to avoid panic!s).<br> //! //! Not 100% spec compliant in the name of convenience (see examples below):<br> //! * allows for extra whitespaces<br> //! * allows for floats where standard allows percentages only<br> //! //! CSS3 Color spec: <br> //! http://www.w3.org/TR/css3-color/<br> //! https://developer.mozilla.org/en-US/docs/Web/CSS/color<br> //! //! Repository:<br> //! https://github.com/7thSigil/css-color-parser-rs.git //! //! Original js parser:<br> //! https://github.com/deanm/css-color-parser-js //! //! #Examples //! //! use css_color_parser::Color as CssColor; //! //! let transparent_black = CssColor { r: 0, g: 0, b: 0, a: 1.0 }; //! //! println!("{:?}", " rgba (255, 128, 12, 0.5)".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 255, g: 128, b: 12, a: 0.5 } //! //! println!("{:?}", "#fff".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 255, g: 255, b: 255, a: 1 } //! //! println!("{:?}", "#ff0011".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 255, g: 0, b: 17, a: 1 } //! //! println!("{:?}", "slateblue".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 106, g: 90, b: 205, a: 1 } //! //! println!("{:?}", "blah".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 0, g: 0, b: 0, a: 0 } - ColorParseError //! //! println!("{:?}", "ffffff".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 0, g: 0, b: 0, a: 0 } - ColorParseError //! //! println!("{:?}", "hsla(900, 15%, 90%, 0.5)".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 226, g: 233, b: 233, a: 0.5 } //! //! println!("{:?}", "hsla(900, 15%, 90%)".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 0, g: 0, b: 0, a: 0 } - ColorParseError //! println!("{:?}", "hsl(900, 15%, 90%)".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 226, g: 233, b: 233, a: 1 } //! //! // NOTE: not spec compliant. //! println!("{:?}", "hsl(900, 0.15, 90%)".parse::<CssColor>() //! .unwrap_or(transparent_black)); //! //Color { r: 226, g: 233, b: 233, a: 1 } //! #[macro_use] extern crate lazy_static; pub use self::color::color::{Color, ColorParseError}; pub use self::color::named_colors::NAMED_COLORS; mod color; #[cfg(test)] mod tests;