From 179e762b50bf0f31c3e4898ca1a26b4f0f9371d3 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 4 Oct 2010 15:25:00 -0700 Subject: [PATCH] Cache css color parser values --- lib/canvas.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/canvas.js b/lib/canvas.js index 7c5e356..4c602bf 100644 --- a/lib/canvas.js +++ b/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)