From b977280566bd570e9ab20d3449294491a9ad8c23 Mon Sep 17 00:00:00 2001 From: Shannon Kao Date: Tue, 22 Oct 2019 11:15:05 -0700 Subject: [PATCH 1/2] Handle 0x hex values in color parsing --- core/utils/colour.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/utils/colour.js b/core/utils/colour.js index 849737448..c64da4aef 100644 --- a/core/utils/colour.js +++ b/core/utils/colour.js @@ -37,6 +37,7 @@ goog.require('Blockly.utils'); * .parse('red') -> '#ff0000' * .parse('#f00') -> '#ff0000' * .parse('#ff0000') -> '#ff0000' + * .parse('0xff0000') -> '#ff0000' * .parse('rgb(255, 0, 0)') -> '#ff0000' * @param {string} str Colour in some CSS format. * @return {string|null} A string containing a hex representation of the colour, @@ -49,7 +50,8 @@ Blockly.utils.colour.parse = function(str) { // e.g. 'red' return hex; } - hex = str[0] == '#' ? str : '#' + str; + hex = str.substring(0, 2) == '0x' ? '#' + str.substring(2) : str; + hex = hex[0] == '#' ? hex : '#' + hex; if (/^#[0-9a-f]{6}$/.test(hex)) { // e.g. '#00ff88' return hex; @@ -92,7 +94,12 @@ Blockly.utils.colour.rgbToHex = function(r, g, b) { * @return {!Array.} RGB representation of the colour. */ Blockly.utils.colour.hexToRgb = function(hexColour) { - var rgb = parseInt(hexColour.substr(1), 16); + var hex = Blockly.utils.colour.parse(hexColour); + if (!hex) { + return [0, 0, 0]; + } + + var rgb = parseInt(hex.substr(1), 16); var r = rgb >> 16; var g = (rgb >> 8) & 255; var b = rgb & 255; From ef283863560da8b93530d2d79160b2b6bf36bac1 Mon Sep 17 00:00:00 2001 From: Shannon Kao Date: Fri, 1 Nov 2019 15:06:35 -0700 Subject: [PATCH 2/2] Update jsdoc and parameter to reflect additional colour parsing --- core/utils/colour.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/utils/colour.js b/core/utils/colour.js index c64da4aef..eafb03d06 100644 --- a/core/utils/colour.js +++ b/core/utils/colour.js @@ -89,12 +89,13 @@ Blockly.utils.colour.rgbToHex = function(r, g, b) { }; /** - * Converts a hex representation of a colour to RGB. - * @param {string} hexColour Colour in '#ff0000' format. + * Converts a colour to RGB. + * @param {string} colour String representing colour in any + * colour format ('#ff0000', 'red', '0xff000', etc). * @return {!Array.} RGB representation of the colour. */ -Blockly.utils.colour.hexToRgb = function(hexColour) { - var hex = Blockly.utils.colour.parse(hexColour); +Blockly.utils.colour.hexToRgb = function(colour) { + var hex = Blockly.utils.colour.parse(colour); if (!hex) { return [0, 0, 0]; }