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, penDown: false,
hue: 120, hue: 120,
shade: 50, shade: 50,
transparency: 100, transparency: 0,
penAttributes: { penAttributes: {
color4f: [0, 0, 1, 1], color4f: [0, 0, 1, 1],
diameter: 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. * @param {PenState} penState - the pen state to update.
* @private * @private
*/ */
@ -184,7 +185,7 @@ class Scratch3PenBlocks {
penState.penAttributes.color4f[0] = rgb.r / 255.0; penState.penAttributes.color4f[0] = rgb.r / 255.0;
penState.penAttributes.color4f[1] = rgb.g / 255.0; penState.penAttributes.color4f[1] = rgb.g / 255.0;
penState.penAttributes.color4f[2] = rgb.b / 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);
} }
/** /**
@ -200,15 +201,37 @@ class Scratch3PenBlocks {
} }
/** /**
* Wrap a pen transparency value to the range (0, 100). * Clamp a pen transparency value to the range (0,100).
* @param {number} value - the pen transparency value to the proper range. * @param {number} value - the pen transparency value to be clamped.
* @returns {number} the wrapped value. * @returns {number} the clamped value.
* @private * @private
*/ */
_wrapTransparency (value) { _clampTransparency (value) {
value = value % 100; return MathUtil.clamp(value, 0, 100);
if (value < 0) value += 100; }
return value;
/**
* 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 { } else {
penState.penAttributes.color4f[3] = 1; penState.penAttributes.color4f[3] = 1;
} }
penState.transparency = this._alphaToTransparency(penState.penAttributes.color4f[3]);
} }
/** /**
@ -395,7 +419,7 @@ class Scratch3PenBlocks {
*/ */
changePenTransparencyBy (args, util) { changePenTransparencyBy (args, util) {
const penState = this._getPenState(util.target); 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); this._updatePenColor(penState);
} }
@ -407,7 +431,7 @@ class Scratch3PenBlocks {
*/ */
setPenTransparencyTo (args, util) { setPenTransparencyTo (args, util) {
const penState = this._getPenState(util.target); 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); this._updatePenColor(penState);
} }
} }