Allow RGB colours.

This commit is contained in:
Neil Fraser 2015-12-13 19:13:05 +01:00
parent 19263bdd5b
commit e1acdf5caf
6 changed files with 29 additions and 14 deletions

View file

@ -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;
};

View file

@ -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);

View file

@ -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);
};

View file

@ -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.

View file

@ -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());
}
};

View file

@ -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 = '';