Code Monkey home page Code Monkey logo

css-color-parser-rs's Introduction

Crates.io Build Status Codecov branch

Easy-to-use Rust parser for CSS3 color strings.
Lightweight.
Reliable (Provides tests, handles all errors to avoid panic!s).

Not 100% spec compliant in the name of convenience (see examples below):

  • allows for extra whitespaces
  • allows for floats where standard allows percentages only

W3C CSS3 Color spec

Mozilla CSS3 Color spec

Repository:
https://github.com/7thSigil/css-color-parser-rs.git

Original js parser:
https://github.com/deanm/css-color-parser-js

Link to the Documentation

Cargo.toml:

[dependencies]
css-color-parser = "*"

Example

extern crate css_color_parser;

//...

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 }



(c) Katkov Oleksandr [email protected], 2016.

(c) Dean McNamee [email protected], 2012.

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.

css-color-parser-rs's People

Contributors

7thsigil avatar clarfonthey avatar xion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

css-color-parser-rs's Issues

Relicense under dual MIT/Apache-2.0

TL;DR the Rust ecosystem is largely Apache-2.0. Being available under that
license is good for interoperation. The MIT license as an add-on can be nice
for GPLv2 projects to use your code.

(description of this issue was mostly taken from cmr/relicense-assistant)

Why?

The MIT license requires reproducing countless copies of the same copyright
header with different names in the copyright field, for every MIT library in
use. The Apache license does not have this drawback. However, this is not the
primary motivation for me creating these issues. The Apache license also has
protections from patent trolls and an explicit contribution licensing clause.
However, the Apache license is incompatible with GPLv2. This is why Rust is
dual-licensed as MIT/Apache (the "primary" license being Apache, MIT only for
GPLv2 compat), and doing so would be wise for this project. This also makes
this crate suitable for inclusion and unrestricted sharing in the Rust
standard distribution and other projects using dual MIT/Apache.

Some ask, "Does this really apply to binary redistributions? Does MIT really
require reproducing the whole thing?" I'm not a lawyer, and I can't give legal
advice, but some Google Android apps include open source attributions using
this interpretation. Others also agree with
it
.
But, again, the copyright notice redistribution is not the primary motivation
for the dual-licensing. It's stronger protections to licensees and better
interoperation with the wider Rust ecosystem.

Original author

Since this project started as a port and I am no lawyer, relicensing probably needs an explicit permission from original js project author @deanm

Contributor checkoff

@7thSigil
@clarcharr

To agree to relicensing, comment with :

I license past and future contributions under the dual MIT/Apache-2.0 license, allowing licensees to chose either at their option.

Publish new version on crates.io

I'd like to publish my own crate which depends on css-color-parser. It is currently impossible because I have to rely on Git HEAD to have the PR #3 fix included.

Would you mind publishing 0.1.2 with it included?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.