Browse Source

Cache css color parser values

v1.x
Tj Holowaychuk 14 years ago
parent
commit
179e762b50
  1. 15
      lib/canvas.js

15
lib/canvas.js

@ -34,6 +34,12 @@ exports.version = '0.0.1';
exports.cairoVersion = cairoVersion;
/**
* Cache color string RGBA values.
*/
var cache = {};
/**
* Parse the given color `str`.
*
@ -62,11 +68,12 @@ exports.cairoVersion = cairoVersion;
*/
exports.parseColor = function(str){
if (cache[str]) return cache[str];
str = colors[str] || String(str);
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;
return [
return cache[str] = [
parseInt(captures[1], 10)
, parseInt(captures[2], 10)
, parseInt(captures[3], 10)
@ -75,7 +82,7 @@ exports.parseColor = function(str){
} else if (0 == str.indexOf('rgb')) {
var captures = /rgb\((\d{1,3}) *, *(\d{1,3}) *, *(\d{1,3}) *\)/.exec(str);
if (!captures) return;
return [
return cache[str] = [
parseInt(captures[1], 10)
, parseInt(captures[2], 10)
, parseInt(captures[3], 10)
@ -84,7 +91,7 @@ exports.parseColor = function(str){
} else if ('#' == str.charAt(0) && str.length > 4) {
var captures = /#(\w{2})(\w{2})(\w{2})/.exec(str);
if (!captures) return;
return [
return cache[str] = [
parseInt(captures[1], 16)
, parseInt(captures[2], 16)
, parseInt(captures[3], 16)
@ -93,7 +100,7 @@ exports.parseColor = function(str){
} else if ('#' == str.charAt(0)) {
var captures = /#(\w)(\w)(\w)/.exec(str);
if (!captures) return;
return [
return cache[str] = [
parseInt(captures[1] + captures[1], 16)
, parseInt(captures[2] + captures[2], 16)
, parseInt(captures[3] + captures[3], 16)

Loading…
Cancel
Save