diff --git a/src/util/cast.js b/src/util/cast.js index ab7c274c1..49c189039 100644 --- a/src/util/cast.js +++ b/src/util/cast.js @@ -93,6 +93,9 @@ class Cast { let color; if (typeof value === 'string' && value.substring(0, 1) === '#') { color = Color.hexToRgb(value); + + // If the color wasn't *actually* a hex color, cast to black + if (!color) color = {r: 0, g: 0, b: 0, a: 255}; } else { color = Color.decimalToRgb(Cast.toNumber(value)); } diff --git a/test/unit/util_cast.js b/test/unit/util_cast.js index 9ce5981ba..bf2bbdff7 100644 --- a/test/unit/util_cast.js +++ b/test/unit/util_cast.js @@ -73,7 +73,7 @@ test('toString', t => { t.end(); }); -test('toRbgColorList', t => { +test('toRgbColorList', t => { // Hex (minimal, see "color" util tests) t.deepEqual(cast.toRgbColorList('#000'), [0, 0, 0]); t.deepEqual(cast.toRgbColorList('#000000'), [0, 0, 0]); @@ -88,10 +88,11 @@ test('toRbgColorList', t => { // Malformed t.deepEqual(cast.toRgbColorList('ffffff'), [0, 0, 0]); t.deepEqual(cast.toRgbColorList('foobar'), [0, 0, 0]); + t.deepEqual(cast.toRgbColorList('#nothex'), [0, 0, 0]); t.end(); }); -test('toRbgColorObject', t => { +test('toRgbColorObject', t => { // Hex (minimal, see "color" util tests) t.deepEqual(cast.toRgbColorObject('#000'), {r: 0, g: 0, b: 0}); t.deepEqual(cast.toRgbColorObject('#000000'), {r: 0, g: 0, b: 0}); @@ -107,6 +108,7 @@ test('toRbgColorObject', t => { // Malformed t.deepEqual(cast.toRgbColorObject('ffffff'), {a: 255, r: 0, g: 0, b: 0}); t.deepEqual(cast.toRgbColorObject('foobar'), {a: 255, r: 0, g: 0, b: 0}); + t.deepEqual(cast.toRgbColorObject('#nothex'), {a: 255, r: 0, g: 0, b: 0}); t.end(); });