mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-25 07:22:33 -05:00
More color utilities, touching color, color touching color (#155)
* Fill out color utilities * Amend Color.decimalToHex name * Add `Cast.toScaledRgbColor` * Add touching color, color touching color blocks * Fix scale of Cast.toRgbColor * Fix format of colors to use renderer-style lists * Update `Color.decimalToRgb` * Lint self
This commit is contained in:
parent
5df0acc895
commit
d93eeed05d
5 changed files with 132 additions and 7 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
var Cast = require('../util/cast');
|
||||||
|
|
||||||
function Scratch3SensingBlocks(runtime) {
|
function Scratch3SensingBlocks(runtime) {
|
||||||
/**
|
/**
|
||||||
* The runtime instantiating this block package.
|
* The runtime instantiating this block package.
|
||||||
|
@ -12,6 +14,9 @@ function Scratch3SensingBlocks(runtime) {
|
||||||
*/
|
*/
|
||||||
Scratch3SensingBlocks.prototype.getPrimitives = function() {
|
Scratch3SensingBlocks.prototype.getPrimitives = function() {
|
||||||
return {
|
return {
|
||||||
|
'colour_picker': this.colorPicker,
|
||||||
|
'sensing_touchingcolor': this.touchingColor,
|
||||||
|
'sensing_coloristouchingcolor': this.colorTouchingColor,
|
||||||
'sensing_timer': this.getTimer,
|
'sensing_timer': this.getTimer,
|
||||||
'sensing_resettimer': this.resetTimer,
|
'sensing_resettimer': this.resetTimer,
|
||||||
'sensing_mousex': this.getMouseX,
|
'sensing_mousex': this.getMouseX,
|
||||||
|
@ -24,6 +29,21 @@ Scratch3SensingBlocks.prototype.getPrimitives = function() {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Scratch3SensingBlocks.prototype.colorPicker = function (args) {
|
||||||
|
return args.COLOUR;
|
||||||
|
};
|
||||||
|
|
||||||
|
Scratch3SensingBlocks.prototype.touchingColor = function (args, util) {
|
||||||
|
var color = Cast.toRgbColorList(args.COLOR);
|
||||||
|
return util.target.isTouchingColor(color);
|
||||||
|
};
|
||||||
|
|
||||||
|
Scratch3SensingBlocks.prototype.colorTouchingColor = function (args, util) {
|
||||||
|
var maskColor = Cast.toRgbColorList(args.COLOR);
|
||||||
|
var targetColor = Cast.toRgbColorList(args.COLOR2);
|
||||||
|
return util.target.colorIsTouchingColor(targetColor, maskColor);
|
||||||
|
};
|
||||||
|
|
||||||
Scratch3SensingBlocks.prototype.getTimer = function (args, util) {
|
Scratch3SensingBlocks.prototype.getTimer = function (args, util) {
|
||||||
return util.ioQuery('clock', 'projectTimer');
|
return util.ioQuery('clock', 'projectTimer');
|
||||||
};
|
};
|
||||||
|
|
|
@ -258,7 +258,7 @@ function parseBlock (sb2block) {
|
||||||
}
|
}
|
||||||
} else if (expectedArg.inputOp == 'colour_picker') {
|
} else if (expectedArg.inputOp == 'colour_picker') {
|
||||||
// Convert SB2 color to hex.
|
// Convert SB2 color to hex.
|
||||||
fieldValue = Color.scratchColorToHex(providedArg);
|
fieldValue = Color.decimalToHex(providedArg);
|
||||||
fieldName = 'COLOUR';
|
fieldName = 'COLOUR';
|
||||||
if (shadowObscured) {
|
if (shadowObscured) {
|
||||||
fieldValue = '#990000';
|
fieldValue = '#990000';
|
||||||
|
|
|
@ -273,4 +273,33 @@ Clone.prototype.getName = function () {
|
||||||
return this.sprite.name;
|
return this.sprite.name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the clone is touching a color.
|
||||||
|
* @param {Array.<number>} rgb [r,g,b], values between 0-255.
|
||||||
|
* @return {Promise.<Boolean>} True iff the clone is touching the color.
|
||||||
|
*/
|
||||||
|
Clone.prototype.isTouchingColor = function (rgb) {
|
||||||
|
if (this.renderer) {
|
||||||
|
return this.renderer.isTouchingColor(this.drawableID, rgb);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the clone's color is touching a color.
|
||||||
|
* @param {Object} targetRgb {Array.<number>} [r,g,b], values between 0-255.
|
||||||
|
* @param {Object} maskRgb {Array.<number>} [r,g,b], values between 0-255.
|
||||||
|
* @return {Promise.<Boolean>} True iff the clone's color is touching the color.
|
||||||
|
*/
|
||||||
|
Clone.prototype.colorIsTouchingColor = function (targetRgb, maskRgb) {
|
||||||
|
if (this.renderer) {
|
||||||
|
return this.renderer.isTouchingColor(
|
||||||
|
this.drawableID,
|
||||||
|
targetRgb,
|
||||||
|
maskRgb
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = Clone;
|
module.exports = Clone;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
var Color = require('../util/color');
|
||||||
|
|
||||||
function Cast () {}
|
function Cast () {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,6 +65,21 @@ Cast.toString = function (value) {
|
||||||
return String(value);
|
return String(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast any Scratch argument to an RGB color object to be used for the renderer.
|
||||||
|
* @param {*} value Value to convert to RGB color object.
|
||||||
|
* @return {Array.<number>} [r,g,b], values between 0-255.
|
||||||
|
*/
|
||||||
|
Cast.toRgbColorList = function (value) {
|
||||||
|
var color;
|
||||||
|
if (typeof value == 'string' && value.substring(0, 1) == '#') {
|
||||||
|
color = Color.hexToRgb(value);
|
||||||
|
} else {
|
||||||
|
color = Color.decimalToRgb(Cast.toNumber(value));
|
||||||
|
}
|
||||||
|
return [color.r, color.g, color.b];
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two values, using Scratch cast, case-insensitive string compare, etc.
|
* Compare two values, using Scratch cast, case-insensitive string compare, etc.
|
||||||
* In Scratch 2.0, this is captured by `interp.compare.`
|
* In Scratch 2.0, this is captured by `interp.compare.`
|
||||||
|
|
|
@ -1,17 +1,76 @@
|
||||||
function Color () {}
|
function Color () {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a Scratch color number to a hex string, #RRGGBB.
|
* Convert a Scratch decimal color to a hex string, #RRGGBB.
|
||||||
* @param {number} color RGB color as a decimal.
|
* @param {number} decimal RGB color as a decimal.
|
||||||
* @return {string} RGB color as #RRGGBB hex string.
|
* @return {string} RGB color as #RRGGBB hex string.
|
||||||
*/
|
*/
|
||||||
Color.scratchColorToHex = function (color) {
|
Color.decimalToHex = function (decimal) {
|
||||||
if (color < 0) {
|
if (decimal < 0) {
|
||||||
color += 0xFFFFFF + 1;
|
decimal += 0xFFFFFF + 1;
|
||||||
}
|
}
|
||||||
var hex = Number(color).toString(16);
|
var hex = Number(decimal).toString(16);
|
||||||
hex = '#' + '000000'.substr(0, 6 - hex.length) + hex;
|
hex = '#' + '000000'.substr(0, 6 - hex.length) + hex;
|
||||||
return hex;
|
return hex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a Scratch decimal color to an RGB color object.
|
||||||
|
* @param {number} decimal RGB color as decimal.
|
||||||
|
* @returns {Object} {r: R, g: G, b: B}, values between 0-255
|
||||||
|
*/
|
||||||
|
Color.decimalToRgb = function (decimal) {
|
||||||
|
var r = (decimal >> 16) & 0xFF;
|
||||||
|
var g = (decimal >> 8) & 0xFF;
|
||||||
|
var b = decimal & 0xFF;
|
||||||
|
return {r: r, g: g, b: b};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a hex color (e.g., F00, #03F, #0033FF) to an RGB color object.
|
||||||
|
* CC-BY-SA Tim Down:
|
||||||
|
* https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
|
||||||
|
* @param {!string} hex Hex representation of the color.
|
||||||
|
* @return {Object} {r: R, g: G, b: B}, 0-255, or null.
|
||||||
|
*/
|
||||||
|
Color.hexToRgb = function (hex) {
|
||||||
|
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
|
||||||
|
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
|
||||||
|
return r + r + g + g + b + b;
|
||||||
|
});
|
||||||
|
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
||||||
|
return result ? {
|
||||||
|
r: parseInt(result[1], 16),
|
||||||
|
g: parseInt(result[2], 16),
|
||||||
|
b: parseInt(result[3], 16)
|
||||||
|
} : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an RGB color object to a hex color.
|
||||||
|
* @param {Object} rgb {r: R, g: G, b: B}, values between 0-255.
|
||||||
|
* @return {!string} Hex representation of the color.
|
||||||
|
*/
|
||||||
|
Color.rgbToHex = function (rgb) {
|
||||||
|
return Color.decimalToHex(Color.rgbToDecimal(rgb));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an RGB color object to a Scratch decimal color.
|
||||||
|
* @param {Object} rgb {r: R, g: G, b: B}, values between 0-255.
|
||||||
|
* @return {!number} Number representing the color.
|
||||||
|
*/
|
||||||
|
Color.rgbToDecimal = function (rgb) {
|
||||||
|
return (rgb.r << 16) + (rgb.g << 8) + rgb.b;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a hex color (e.g., F00, #03F, #0033FF) to a decimal color number.
|
||||||
|
* @param {!string} hex Hex representation of the color.
|
||||||
|
* @return {!number} Number representing the color.
|
||||||
|
*/
|
||||||
|
Color.hexToDecimal = function (hex) {
|
||||||
|
return Color.rgbToDecimal(Color.hexToRgb(hex));
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = Color;
|
module.exports = Color;
|
||||||
|
|
Loading…
Reference in a new issue