mirror of
https://github.com/google/blockly.git
synced 2026-01-11 10:57:07 +01:00
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:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 = '';
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user