Add pen transparency, clamped 0-100

This commit is contained in:
Eric Rosenbaum 2017-09-05 18:00:34 -04:00
parent 2b89063827
commit 58eaeaaf6a

View file

@ -55,7 +55,7 @@ class Scratch3PenBlocks {
penDown: false,
hue: 120,
shade: 50,
transparency: 100,
transparency: 0,
penAttributes: {
color4f: [0, 0, 1, 1],
diameter: 1
@ -169,7 +169,8 @@ class Scratch3PenBlocks {
}
/**
* Update the cached RGB color from the hue & shade values in the provided PenState object.
* Update the cached color from the hue, shade and transparency values in the provided
* PenState object.
* @param {PenState} penState - the pen state to update.
* @private
*/
@ -184,7 +185,7 @@ class Scratch3PenBlocks {
penState.penAttributes.color4f[0] = rgb.r / 255.0;
penState.penAttributes.color4f[1] = rgb.g / 255.0;
penState.penAttributes.color4f[2] = rgb.b / 255.0;
penState.penAttributes.color4f[3] = penState.transparency / 100.0;
penState.penAttributes.color4f[3] = this._transparencyToAlpha(penState.transparency);
}
/**
@ -198,17 +199,39 @@ class Scratch3PenBlocks {
if (value < 0) value += 200;
return value;
}
/**
* Wrap a pen transparency value to the range (0, 100).
* @param {number} value - the pen transparency value to the proper range.
* @returns {number} the wrapped value.
* Clamp a pen transparency value to the range (0,100).
* @param {number} value - the pen transparency value to be clamped.
* @returns {number} the clamped value.
* @private
*/
_wrapTransparency (value) {
value = value % 100;
if (value < 0) value += 100;
return value;
_clampTransparency (value) {
return MathUtil.clamp(value, 0, 100);
}
/**
* Convert an alpha value to a pen transparency value.
* Alpha ranges from 0 to 1, where 0 is transparent and 1 is opaque.
* Transparency ranges from 0 to 100, where 0 is opaque and 100 is transparent.
* @param {number} alpha - the input alpha value.
* @returns {number} the transparency value.
* @private
*/
_alphaToTransparency (alpha) {
return (1.0 - alpha) * 100.0;
}
/**
* Convert a pen transparency value to an alpha value.
* Alpha ranges from 0 to 1, where 0 is transparent and 1 is opaque.
* Transparency ranges from 0 to 100, where 0 is opaque and 100 is transparent.
* @param {number} transparency - the input transparency value.
* @returns {number} the alpha value.
* @private
*/
_transparencyToAlpha (transparency) {
return 1.0 - (transparency / 100.0);
}
/**
@ -315,6 +338,7 @@ class Scratch3PenBlocks {
} else {
penState.penAttributes.color4f[3] = 1;
}
penState.transparency = this._alphaToTransparency(penState.penAttributes.color4f[3]);
}
/**
@ -386,7 +410,7 @@ class Scratch3PenBlocks {
const penAttributes = this._getPenState(util.target).penAttributes;
penAttributes.diameter = this._clampPenSize(Cast.toNumber(args.SIZE));
}
/**
* The pen "change pen transparency by {number}" block changes the RGBA "transparency" of the pen.
* @param {object} args - the block arguments.
@ -395,10 +419,10 @@ class Scratch3PenBlocks {
*/
changePenTransparencyBy (args, util) {
const penState = this._getPenState(util.target);
penState.transparency = this._wrapTransparency(penState.transparency + Cast.toNumber(args.TRANSPARENCY));
penState.transparency = this._clampTransparency(penState.transparency + Cast.toNumber(args.TRANSPARENCY));
this._updatePenColor(penState);
}
/**
* The pen "set pen transparency to {number}" block sets the RGBA "transparency" of the pen.
* @param {object} args - the block arguments.
@ -407,7 +431,7 @@ class Scratch3PenBlocks {
*/
setPenTransparencyTo (args, util) {
const penState = this._getPenState(util.target);
penState.transparency = this._wrapTransparency(Cast.toNumber(args.TRANSPARENCY));
penState.transparency = this._clampTransparency(Cast.toNumber(args.TRANSPARENCY));
this._updatePenColor(penState);
}
}