From e1acdf5cafd997144a5c43d9bf628c86f0505503 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Sun, 13 Dec 2015 19:13:05 +0100 Subject: [PATCH] Allow RGB colours. --- core/block.js | 24 ++++++++++++++++++------ core/block_svg.js | 2 +- core/blockly.js | 2 +- core/field_dropdown.js | 2 +- core/icon.js | 3 +-- core/toolbox.js | 10 +++++++--- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/core/block.js b/core/block.js index 53b7ba1b1..c58a5f147 100644 --- a/core/block.js +++ b/core/block.js @@ -145,6 +145,13 @@ Blockly.Block.obtain = function(workspace, prototypeName) { */ Blockly.Block.prototype.data = null; +/** + * Colour of the block in '#RRGGBB' format. + * @type {string} + * @private + */ +Blockly.Block.prototype.colour_ = '#000000'; + /** * Dispose of this block. * @param {boolean} healStack If true, then try to heal any gap by connecting @@ -556,18 +563,24 @@ Blockly.Block.prototype.setTooltip = function(newTip) { /** * Get the colour of a block. - * @return {number} HSV hue value. + * @return {string} #RRGGBB string. */ Blockly.Block.prototype.getColour = function() { - return this.colourHue_; + return this.colour_; }; /** * Change the colour of a block. - * @param {number} colourHue HSV hue value. + * @param {number|string} colour HSV hue value, or #RRGGBB string. */ -Blockly.Block.prototype.setColour = function(colourHue) { - this.colourHue_ = colourHue; +Blockly.Block.prototype.setColour = function(colour) { + if (goog.isNumber(colour)) { + this.colour_ = Blockly.hueToRgb(colour); + } else if (goog.isString(colour) && colour.match(/^#[0-9a-fA-F]{6}$/)) { + this.colour_ = colour; + } else { + throw 'Invalid colour: ' + colour; + } if (this.rendered) { this.updateColour(); } @@ -1244,4 +1257,3 @@ Blockly.Block.BlockDB_ = Object.create(null); Blockly.Block.getById = function(id) { return Blockly.Block.BlockDB_[id] || null; }; - diff --git a/core/block_svg.js b/core/block_svg.js index 15af148cc..8894d6df7 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -1319,7 +1319,7 @@ Blockly.BlockSvg.prototype.updateColour = function() { // Disabled blocks don't have colour. return; } - var hexColour = Blockly.makeColour(this.getColour()); + var hexColour = this.getColour(); var rgb = goog.color.hexToRgb(hexColour); if (this.isShadow()) { rgb = goog.color.lighten(rgb, 0.6); diff --git a/core/blockly.js b/core/blockly.js index b2961a5ea..9a8701e70 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -95,7 +95,7 @@ Blockly.SPRITE = { * @param {number} hue Hue on a colour wheel (0-360). * @return {string} RGB code, e.g. '#5ba65b'. */ -Blockly.makeColour = function(hue) { +Blockly.hueToRgb = function(hue) { return goog.color.hsvToHex(hue, Blockly.HSV_SATURATION, Blockly.HSV_VALUE * 255); }; diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 4c1f234dd..b773d12f7 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -287,7 +287,7 @@ Blockly.FieldDropdown.prototype.setValue = function(newValue) { Blockly.FieldDropdown.prototype.setText = function(text) { if (this.sourceBlock_ && this.arrow_) { // Update arrow's colour. - this.arrow_.style.fill = Blockly.makeColour(this.sourceBlock_.getColour()); + this.arrow_.style.fill = this.sourceBlock_.getColour(); } if (text === null || text === this.text_) { // No change if null. diff --git a/core/icon.js b/core/icon.js index f64502b48..d9d041dc7 100644 --- a/core/icon.js +++ b/core/icon.js @@ -142,8 +142,7 @@ Blockly.Icon.prototype.iconClick_ = function(e) { */ Blockly.Icon.prototype.updateColour = function() { if (this.isVisible()) { - var hexColour = Blockly.makeColour(this.block_.getColour()); - this.bubble_.setColour(hexColour); + this.bubble_.setColour(this.block_.getColour()); } }; diff --git a/core/toolbox.js b/core/toolbox.js index d142e08f7..3b681811a 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -209,9 +209,13 @@ Blockly.Toolbox.prototype.populate_ = function(newTree) { } else { syncTrees(childIn, childOut); } - var hue = childIn.getAttribute('colour'); - if (goog.isString(hue)) { - childOut.hexColour = Blockly.makeColour(hue); + var colour = childIn.getAttribute('colour'); + if (goog.isString(colour)) { + if (colour.match(/^#[0-9a-fA-F]{6}$/)) { + childOut.hexColour = colour; + } else { + childOut.hexColour = Blockly.hueToRgb(colour); + } hasColours = true; } else { childOut.hexColour = '';