color-string
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -9,2 +9,3 @@ /* MIT license */ | ||
getHsl: getHsl, | ||
getAlpha: getAlpha, | ||
@@ -22,2 +23,5 @@ hexString: hexString, | ||
function getRgba(string) { | ||
if (!string) { | ||
return; | ||
} | ||
var abbr = /^#([a-fA-F0-9]{3})$/, | ||
@@ -80,2 +84,5 @@ hex = /^#([a-fA-F0-9]{6})$/, | ||
function getHsla(string) { | ||
if (!string) { | ||
return; | ||
} | ||
var hsl = /^hsla?\(\s*(\d+)\s*,\s*([\d\.]+)%\s*,\s*([\d\.]+)%\s*(?:,\s*([\d\.]+)\s*)?\)/; | ||
@@ -100,2 +107,12 @@ var match = string.match(hsl); | ||
function getAlpha(string) { | ||
var vals = getRgba(string); | ||
if (vals) { | ||
return vals[3]; | ||
} | ||
else if (vals = getHsla(string)) { | ||
return vals[3]; | ||
} | ||
} | ||
// generators | ||
@@ -107,5 +124,5 @@ function hexString(rgb) { | ||
function rgbString(rgba) { | ||
if (rgba[3] && rgba[3] < 1) { | ||
return rgbaString(rgba); | ||
function rgbString(rgba, alpha) { | ||
if (alpha < 1 || (rgba[3] && rgba[3] < 1)) { | ||
return rgbaString(rgba, alpha); | ||
} | ||
@@ -115,10 +132,10 @@ return "rgb(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2] + ")"; | ||
function rgbaString(rgba) { | ||
function rgbaString(rgba, alpha) { | ||
return "rgba(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2] | ||
+ ", " + (rgba[3] || 1) + ")"; | ||
+ ", " + (alpha || rgba[3] || 1) + ")"; | ||
} | ||
function percentString(rgba) { | ||
if (rgba[3] && rgba[3] < 1) { | ||
return percentaString(rgba); | ||
function percentString(rgba, alpha) { | ||
if (alpha < 1 || (rgba[3] && rgba[3] < 1)) { | ||
return percentaString(rgba, alpha); | ||
} | ||
@@ -132,12 +149,12 @@ var r = Math.round(rgba[0]/255 * 100), | ||
function percentaString(rgba) { | ||
function percentaString(rgba, alpha) { | ||
var r = Math.round(rgba[0]/255 * 100), | ||
g = Math.round(rgba[1]/255 * 100), | ||
b = Math.round(rgba[2]/255 * 100); | ||
return "rgba(" + r + "%, " + g + "%, " + b + "%, " + (rgba[3] || 1) + ")"; | ||
return "rgba(" + r + "%, " + g + "%, " + b + "%, " + (alpha || rgba[3] || 1) + ")"; | ||
} | ||
function hslString(hsla) { | ||
if (hsla[3] && hsla[3] < 1) { | ||
return hslaString(hsla); | ||
function hslString(hsla, alpha) { | ||
if (alpha < 1 || (hsla[3] && hsla[3] < 1)) { | ||
return hslaString(hsla, alpha); | ||
} | ||
@@ -147,5 +164,5 @@ return "hsl(" + hsla[0] + ", " + hsla[1] + "%, " + hsla[2] + "%)"; | ||
function hslaString(hsla) { | ||
function hslaString(hsla, alpha) { | ||
return "hsla(" + hsla[0] + ", " + hsla[1] + "%, " + hsla[2] + "%, " | ||
+ (hsla[3] || 1) + ")"; | ||
+ (alpha || hsla[3] || 1) + ")"; | ||
} | ||
@@ -152,0 +169,0 @@ |
{ | ||
"name": "color-string", | ||
"description": "parse and generate CSS color strings", | ||
"version": "0.1.0", | ||
"description": "Parser and generator for CSS color strings", | ||
"version": "0.1.1", | ||
"author": "Heather Arthur <fayearthur@gmail.com>", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -14,2 +14,4 @@ # color-string | ||
colorString.getHsla("hsla(360, 60%, 50%, 0.4)") // [360, 60, 50, 0.4] | ||
colorString.getAlpha("rgba(200, 0, 12, 0.6)") // 0.6 | ||
``` | ||
@@ -21,2 +23,3 @@ #### generating: | ||
colorString.rgbString([0, 0, 255, 0.4]) // "rgba(0, 0, 255, 0.4)" | ||
colorString.rgbString([0, 0, 255], 0.4) // "rgba(0, 0, 255, 0.4)" | ||
colorString.percentString([0, 0, 255]) // "rgb(0%, 0%, 100%)" | ||
@@ -23,0 +26,0 @@ colorString.keyword([255, 255, 0]) // "yellow" |
@@ -12,2 +12,7 @@ var string = require("../color-string"), | ||
assert.equal(string.getAlpha("rgb(244, 233, 100)"), 1); | ||
assert.equal(string.getAlpha("rgba(244, 233, 100, 0.5)"), 0.5); | ||
assert.equal(string.getAlpha("hsla(244, 100%, 100%, 0.6)"), 0.6); | ||
// alpha | ||
@@ -37,3 +42,5 @@ assert.deepEqual(string.getRgba("rgba(200, 20, 233, 0.2)"), [200, 20, 233, 0.2]); | ||
assert.equal(string.rgbString([255, 10, 35, 0.3]), "rgba(255, 10, 35, 0.3)"); | ||
assert.equal(string.rgbString([255, 10, 35], 0.3), "rgba(255, 10, 35, 0.3)"); | ||
assert.equal(string.rgbaString([255, 10, 35, 0.3]), "rgba(255, 10, 35, 0.3)"); | ||
assert.equal(string.rgbaString([255, 10, 35], 0.3), "rgba(255, 10, 35, 0.3)"); | ||
assert.equal(string.rgbaString([255, 10, 35]), "rgba(255, 10, 35, 1)"); | ||
@@ -43,3 +50,5 @@ | ||
assert.equal(string.percentString([255, 10, 35, 0.3]), "rgba(100%, 4%, 14%, 0.3)"); | ||
assert.equal(string.percentString([255, 10, 35], 0.3), "rgba(100%, 4%, 14%, 0.3)"); | ||
assert.equal(string.percentaString([255, 10, 35, 0.3]), "rgba(100%, 4%, 14%, 0.3)"); | ||
assert.equal(string.percentaString([255, 10, 35], 0.3), "rgba(100%, 4%, 14%, 0.3)"); | ||
assert.equal(string.percentaString([255, 10, 35]), "rgba(100%, 4%, 14%, 1)"); | ||
@@ -49,3 +58,5 @@ | ||
assert.equal(string.hslString([280, 40, 60, 0.3]), "hsla(280, 40%, 60%, 0.3)"); | ||
assert.equal(string.hslString([280, 40, 60], 0.3), "hsla(280, 40%, 60%, 0.3)"); | ||
assert.equal(string.hslaString([280, 40, 60, 0.3]), "hsla(280, 40%, 60%, 0.3)"); | ||
assert.equal(string.hslaString([280, 40, 60], 0.3), "hsla(280, 40%, 60%, 0.3)"); | ||
assert.equal(string.hslaString([280, 40, 60]), "hsla(280, 40%, 60%, 1)"); | ||
@@ -52,0 +63,0 @@ |
10916
246
37