Store Block hue so it can be extracted reliably (#1293)

Adds `Block.getHue()` to retrieve the block colour when set via a hue.
The hue value is stored in the new `.hue_` field, which is null if the colour was set via a hex string.
This commit is contained in:
Isaac Dunn
2017-09-04 16:45:33 +01:00
committed by Andrew n marshall
parent 5518873389
commit f8f807f53c

View File

@@ -192,6 +192,13 @@ Blockly.Block.prototype.data = null;
*/
Blockly.Block.prototype.colour_ = '#000000';
/**
* Colour of the block as HSV hue value (0-360)
* @type {?number}
* @private
*/
Blockly.Block.prototype.hue_ = null;
/**
* Dispose of this block.
* @param {boolean} healStack If true, then try to heal any gap by connecting
@@ -595,16 +602,27 @@ Blockly.Block.prototype.getColour = function() {
return this.colour_;
};
/**
* 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_;
}
/**
* Change the colour of a block.
* @param {number|string} colour HSV hue value, or #RRGGBB string.
*/
Blockly.Block.prototype.setColour = function(colour) {
var hue = Number(colour);
if (!isNaN(hue)) {
if (!isNaN(hue) && 0 <= hue && hue <= 360) {
this.hue_ = hue;
this.colour_ = Blockly.hueToRgb(hue);
} else if (goog.isString(colour) && colour.match(/^#[0-9a-fA-F]{6}$/)) {
this.colour_ = colour;
// Only store hue if colour is set as a hue
this.hue_ = null;
} else {
throw 'Invalid colour: ' + colour;
}