Move Blockly.hueToRgb to Blockly.utils.colour

Also adds support for ‘#f00’, ‘rgb(255, 0, 0)’ and ‘red’ for block colours, in addition to ‘#ff0000’ (and of course hue).
This commit is contained in:
Neil Fraser
2019-06-05 13:11:17 -07:00
committed by Neil Fraser
parent cdaded4296
commit ed9330cf57
4 changed files with 25 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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