Tj Holowaychuk 15 years ago
parent
commit
ccb2b31701
  1. 4
      lib/canvas.js
  2. 3
      test/canvas.test.js

4
lib/canvas.js

@ -71,6 +71,7 @@ var cache = {};
var parseColor = exports.parseColor = function(str){
if (cache[str]) return cache[str];
str = colors[str] || String(str);
// RGBA
if (0 == str.indexOf('rgba')) {
var captures = /rgba\((\d{1,3}) *, *(\d{1,3}) *, *(\d{1,3}) *, *(\d+\.\d+|\.\d+|\d+) *\)/.exec(str);
if (!captures) return;
@ -80,6 +81,7 @@ var parseColor = exports.parseColor = function(str){
, parseInt(captures[3], 10)
, parseFloat(captures[4], 10)
];
// RGB
} else if (0 == str.indexOf('rgb')) {
var captures = /rgb\((\d{1,3}) *, *(\d{1,3}) *, *(\d{1,3}) *\)/.exec(str);
if (!captures) return;
@ -89,6 +91,7 @@ var parseColor = exports.parseColor = function(str){
, parseInt(captures[3], 10)
, 1
];
// #RRGGBB
} else if ('#' == str.charAt(0) && str.length > 4) {
var captures = /#(\w{2})(\w{2})(\w{2})/.exec(str);
if (!captures) return;
@ -98,6 +101,7 @@ var parseColor = exports.parseColor = function(str){
, parseInt(captures[3], 16)
, 1
];
// #RGB
} else if ('#' == str.charAt(0)) {
var captures = /#(\w)(\w)(\w)/.exec(str);
if (!captures) return;

3
test/canvas.test.js

@ -47,6 +47,9 @@ module.exports = {
assert.equal(null, Canvas.parseColor('rgba(2554,165 , 0 ,.6)'));
assert.equal(null, Canvas.parseColor('rgba()'));
// hsl()
assert.equal([]);
// hex
assert.eql([165,89,89,1], Canvas.parseColor('#A55959'));
assert.eql([255,255,255,1], Canvas.parseColor('#FFFFFF'));

Loading…
Cancel
Save