From ed9330cf576b1c040550034743d7005f1e890d7f Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Wed, 5 Jun 2019 13:11:17 -0700 Subject: [PATCH] Move Blockly.hueToRgb to Blockly.utils.colour MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also adds support for ‘#f00’, ‘rgb(255, 0, 0)’ and ‘red’ for block colours, in addition to ‘#ff0000’ (and of course hue). --- core/block.js | 26 ++++++++++++++------------ core/blockly.js | 12 ------------ core/toolbox.js | 2 +- core/utils_colour.js | 10 ++++++++++ 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/core/block.js b/core/block.js index db95e20a5..c1c429aba 100644 --- a/core/block.js +++ b/core/block.js @@ -931,8 +931,8 @@ Blockly.Block.prototype.getStyleName = function() { }; /** - * Get the HSV hue value of a block. Null if hue not set. - * @return {?number} Hue value (0-360) + * Get the HSV hue value of a block. Null if hue not set. + * @return {?number} Hue value (0-360). */ Blockly.Block.prototype.getHue = function() { return this.hue_; @@ -950,18 +950,20 @@ Blockly.Block.prototype.setColour = function(colour) { var hue = Number(dereferenced); if (!isNaN(hue) && 0 <= hue && hue <= 360) { this.hue_ = hue; - this.colour_ = Blockly.hueToRgb(hue); - } else if ((typeof dereferenced == 'string') && - /^#[0-9a-fA-F]{6}$/.test(dereferenced)) { - this.colour_ = dereferenced; - // Only store hue if colour is set as a hue. - this.hue_ = null; + this.colour_ = Blockly.utils.colour.hueToRgb(hue); } else { - var errorMsg = 'Invalid colour: "' + dereferenced + '"'; - if (colour != dereferenced) { - errorMsg += ' (from "' + colour + '")'; + var hex = Blockly.utils.colour.parse(dereferenced); + if (hex) { + this.colour_ = hex; + // Only store hue if colour is set as a hue. + this.hue_ = null; + } else { + var errorMsg = 'Invalid colour: "' + dereferenced + '"'; + if (colour != dereferenced) { + errorMsg += ' (from "' + colour + '")'; + } + throw Error(errorMsg); } - throw Error(errorMsg); } }; diff --git a/core/blockly.js b/core/blockly.js index 27f12aba6..eea9c7902 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -57,8 +57,6 @@ goog.require('Blockly.inject'); goog.require('Blockly.utils'); goog.require('Blockly.Xml'); -goog.require('goog.color'); - // Turn off debugging when compiled. // Unused within the Blockly library, but used in Closure. @@ -121,16 +119,6 @@ Blockly.cache3dSupported_ = null; */ Blockly.theme_ = null; -/** - * Convert a hue (HSV model) into an RGB hex triplet. - * @param {number} hue Hue on a colour wheel (0-360). - * @return {string} RGB code, e.g. '#5ba65b'. - */ -Blockly.hueToRgb = function(hue) { - return goog.color.hsvToHex(hue, Blockly.HSV_SATURATION, - Blockly.HSV_VALUE * 255); -}; - /** * Returns the dimensions of the specified SVG image. * @param {!Element} svg SVG image. diff --git a/core/toolbox.js b/core/toolbox.js index 7f61dcaac..040707b8b 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -400,7 +400,7 @@ Blockly.Toolbox.prototype.setColour_ = function(colourValue, childOut, categoryN this.hasColours_ = true; } else if (typeof colour === 'number' || (typeof colour === 'string' && !isNaN(Number(colour)))) { - childOut.hexColour = Blockly.hueToRgb(Number(colour)); + childOut.hexColour = Blockly.utils.colour.hueToRgb(Number(colour)); this.hasColours_ = true; } else { childOut.hexColour = ''; diff --git a/core/utils_colour.js b/core/utils_colour.js index 7f4f02333..14edb440a 100644 --- a/core/utils_colour.js +++ b/core/utils_colour.js @@ -78,6 +78,16 @@ Blockly.utils.colour.parse = function(str) { +/** + * Convert a hue (HSV model) into an RGB hex triplet. + * @param {number} hue Hue on a colour wheel (0-360). + * @return {string} RGB code, e.g. '#5ba65b'. + */ +Blockly.utils.colour.hueToRgb = function(hue) { + return goog.color.hsvToHex(hue, Blockly.HSV_SATURATION, + Blockly.HSV_VALUE * 255); +}; + /** * A map that contains the 16 basic colour keywords as defined by W3C: * https://www.w3.org/TR/2018/REC-css-color-3-20180619/#html4