Permalink
Browse files

Fix color helpers plugin to return black rather than dieing when

somebody tries to parse "" or another unknown color


git-svn-id: https://flot.googlecode.com/svn/trunk@321 1e0a6537-2640-0410-bfb7-f154510ff394
  • Loading branch information...
1 parent a095971 commit 7e33c7ba318c63f6db13935157f28db34c08cf46 olau@iola.dk committed Mar 17, 2011
Showing with 10 additions and 5 deletions.
  1. +10 −5 jquery.colorhelpers.js
View
@@ -1,6 +1,6 @@
/* Plugin for jQuery for working with colors.
*
- * Version 1.0.
+ * Version 1.1.
*
* Inspiration from jQuery color animation plugin by John Resig.
*
@@ -13,8 +13,11 @@
* console.log(c.r, c.g, c.b, c.a);
* $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
*
- * Note that .scale() and .add() work in-place instead of returning
- * new objects.
+ * Note that .scale() and .add() return the same modified object
+ * instead of making a new one.
+ *
+ * V. 1.1: Fix error handling so e.g. parsing an empty string does
+ * produce a color rather than just crashing.
*/
(function($) {
@@ -88,7 +91,8 @@
}
// parse CSS color string (like "rgb(10, 32, 43)" or "#fff"),
- // returns color object
+ // returns color object, if parsing failed, you get black (0, 0,
+ // 0) out
$.color.parse = function (str) {
var res, m = $.color.make;
@@ -121,7 +125,8 @@
if (name == "transparent")
return m(255, 255, 255, 0);
else {
- res = lookupColors[name];
+ // default to black
+ res = lookupColors[name] || [0, 0, 0];
return m(res[0], res[1], res[2]);
}
}

0 comments on commit 7e33c7b

Please sign in to comment.