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:
Tim Mickel 2016-09-12 10:58:50 -04:00 committed by GitHub
parent 5df0acc895
commit d93eeed05d
5 changed files with 132 additions and 7 deletions

View file

@ -1,3 +1,5 @@
var Cast = require('../util/cast');
function Scratch3SensingBlocks(runtime) {
/**
* The runtime instantiating this block package.
@ -12,6 +14,9 @@ function Scratch3SensingBlocks(runtime) {
*/
Scratch3SensingBlocks.prototype.getPrimitives = function() {
return {
'colour_picker': this.colorPicker,
'sensing_touchingcolor': this.touchingColor,
'sensing_coloristouchingcolor': this.colorTouchingColor,
'sensing_timer': this.getTimer,
'sensing_resettimer': this.resetTimer,
'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) {
return util.ioQuery('clock', 'projectTimer');
};

View file

@ -258,7 +258,7 @@ function parseBlock (sb2block) {
}
} else if (expectedArg.inputOp == 'colour_picker') {
// Convert SB2 color to hex.
fieldValue = Color.scratchColorToHex(providedArg);
fieldValue = Color.decimalToHex(providedArg);
fieldName = 'COLOUR';
if (shadowObscured) {
fieldValue = '#990000';

View file

@ -273,4 +273,33 @@ Clone.prototype.getName = function () {
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;

View file

@ -1,3 +1,5 @@
var Color = require('../util/color');
function Cast () {}
/**
@ -63,6 +65,21 @@ Cast.toString = function (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.
* In Scratch 2.0, this is captured by `interp.compare.`

View file

@ -1,17 +1,76 @@
function Color () {}
/**
* Convert a Scratch color number to a hex string, #RRGGBB.
* @param {number} color RGB color as a decimal.
* Convert a Scratch decimal color to a hex string, #RRGGBB.
* @param {number} decimal RGB color as a decimal.
* @return {string} RGB color as #RRGGBB hex string.
*/
Color.scratchColorToHex = function (color) {
if (color < 0) {
color += 0xFFFFFF + 1;
Color.decimalToHex = function (decimal) {
if (decimal < 0) {
decimal += 0xFFFFFF + 1;
}
var hex = Number(color).toString(16);
var hex = Number(decimal).toString(16);
hex = '#' + '000000'.substr(0, 6 - hex.length) + 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;