Comparing version 0.9.0 to 1.0.0
@@ -680,2 +680,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
exports.parseHsla = parseHsla; | ||
exports.parseColor = parseColor; | ||
@@ -866,2 +867,29 @@ var _Colors = __webpack_require__(3); | ||
/** | ||
* Parse a valid css color into a set of hex, rgb and hsl values | ||
* @param color - a string to be parsed | ||
* @throws TypeError — if type of value passed to function was not a string | ||
* @throws Error — if color name passed to function was not a valid css color | ||
* @returns {{hex: string, rgb: {r: number, g:number, b:number, a:number}, hsl: {h: number, s:number, l:number, a:number}}} | ||
* | ||
* @example | ||
* var color = Parser.parseColor("hsla(0,255,0,.5)"); // color => { hex: "#0f0", rgb: {r:0, g:255, b:0, a: 0.5}, hsl: {h:120, s:1, l: 0.5, a: 0.5} } | ||
* var anotherColor = Parser.parseColor("rgba(255,0,0,.5)"); // color => { hex: "#f00", rgb: {r:255, g:0, b:0, a: 0.5}, hsl: {h:0, s:1, l: 0.5, a: 0.5} } | ||
* var oneMoreColor = Parser.parseColor("black"); // color => { hex: "#000", rgb: {r:0, g:0, b:0}, hsl: {h:0, s:0, l:0}} | ||
*/ | ||
function parseColor(color) { | ||
if (typeof color !== "string") throw new TypeError("Type of target color should be a String"); | ||
// Try to parse one of the available css color formats | ||
if (Validator.isColorName(color)) return parseColorName(color); | ||
if (Validator.isHex(color)) return parseHex(color); | ||
if (Validator.isRgb(color)) return parseRgb(color); | ||
if (Validator.isRgba(color)) return parseRgba(color); | ||
if (Validator.isHsl(color)) return parseHsl(color); | ||
if (Validator.isHsla(color)) return parseHsla(color); | ||
// Otherwise, throw an Error | ||
throw new Error("Invalid css color"); | ||
} | ||
/***/ }) | ||
@@ -868,0 +896,0 @@ /******/ ]) |
{ | ||
"name": "collit", | ||
"version": "0.9.0", | ||
"version": "1.0.0", | ||
"description": "A modern JavaScript library delivering fast and modular color utils. (eg. color convertor, css color validator and parser etc.)", | ||
@@ -5,0 +5,0 @@ "main": "dist/collit.js", |
@@ -241,3 +241,3 @@ <p align="center"> | ||
```js | ||
import Parser from "collit"; | ||
import Parser from "collit"; // or var Parser = Collit.Parser if using in browser | ||
@@ -251,3 +251,3 @@ var color = Parser.parseRgba("rgba(255,0,0,.5)"); | ||
```js | ||
@import Parser from "collit"; | ||
import Parser from "collit"; // or var Parser = Collit.Parser if using in browser | ||
@@ -258,4 +258,17 @@ var color = Parser.parseRgba("hsla(0,255,0,.5)"); | ||
### parseColor(color) | ||
### parseColor(color) | ||
Parse a valid css color into a set of hex, rgb and hsl values | ||
```js | ||
import Parser from "collit"; // or var Parser = Collit.Parser if using in browser | ||
var color = Parser.parseColor("hsla(0,255,0,.5)"); | ||
// color => { hex: "#0f0", rgb: {r:0, g:255, b:0, a: 0.5}, hsl: {h:120, s:1, l: 0.5, a: 0.5} } | ||
var anotherColor = Parser.parseColor("rgba(255,0,0,.5)"); | ||
// color => { hex: "#f00", rgb: {r:255, g:0, b:0, a: 0.5}, hsl: {h:0, s:1, l: 0.5, a: 0.5} } | ||
var oneMoreColor = Parser.parseColor("black"); | ||
// color => { hex: "#000", rgb: {r:0, g:0, b:0}, hsl: {h:0, s:0, l:0}} | ||
``` | ||
## Contributing to Collit | ||
@@ -262,0 +275,0 @@ Contributions are always welcome. |
@@ -863,4 +863,63 @@ var Parser = Collit.Parser; | ||
}); | ||
}) | ||
}); | ||
}); | ||
describe("parseColor(color)", function() { | ||
it("Threw a TypeError when color was not a string", function () { | ||
expect(function () { | ||
Parser.parseColor(123) | ||
}).toThrowError(TypeError); | ||
}); | ||
it("Threw an Error when invalid color was passed", function () { | ||
expect(function () { | ||
Parser.parseColor("cmyk(200,200,10)") | ||
}).toThrowError(Error); | ||
}); | ||
it("Successfully parsed valid css hsla color", function () { | ||
expect(Parser.parseColor("hsla( 120, 100%, 50%, .5 )")).toEqual({ | ||
hex: "#0f0", | ||
rgb: { r: 0, g: 255, b: 0, a: .5 }, | ||
hsl: { h: 120, s: 1, l: .5, a: .5 } | ||
}); | ||
}); | ||
it("Successfully parsed valid css rgba color", function () { | ||
expect(Parser.parseColor("rgba( 255, 0, 0, .5 )")).toEqual({ | ||
hex: "#f00", | ||
rgb: { r: 255, g: 0, b: 0, a: .5 }, | ||
hsl: { h: 0, s: 1, l: .5, a: .5 } | ||
}); | ||
}); | ||
it("Successfully parsed valid css hsl color", function () { | ||
expect(Parser.parseColor("hsl( 240, 100%, 50% )")).toEqual({ | ||
hex: "#00f", | ||
rgb: { r: 0, g: 0, b: 255 }, | ||
hsl: { h: 240, s: 1, l: .5 } | ||
}); | ||
}); | ||
it("Successfully parsed valid css rgb color", function () { | ||
expect(Parser.parseColor("rgb( 128, 128, 128 )")).toEqual({ | ||
hex: "#808080", | ||
rgb: { r: 128, g: 128, b: 128 }, | ||
hsl: { h: 0, s: 0, l: .5 } | ||
}); | ||
}); | ||
it("Successfully parsed a hex color in short form (#000)", function () { | ||
expect(Parser.parseColor("#000")).toEqual({ | ||
hex: "#000", | ||
rgb: { r: 0, g: 0, b: 0 }, | ||
hsl: { h: 0, s: 0, l: 0 } | ||
}); | ||
}); | ||
it("Correctly parsed \"teal\" color", function () { | ||
var color = Parser.parseColor("teal"); | ||
expect(color).toEqual({hex: "#008080", rgb: {r: 0, g: 128, b: 128}, hsl: {h: 180, s: 1, l: .25}}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
236122
1885
1
358