Skip to content

Commit ef871f7

Browse files
committed
Fix compiled color pickers returning number instead of hex code
Hacked blocks and custom extensions expect colour_picker to return a hex color code (#123abc) instead of the parsed number (1194684).
1 parent bc24d05 commit ef871f7

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/compiler/irgen.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,11 @@ class ScriptTreeGenerator {
162162
*/
163163
descendInput (block) {
164164
switch (block.opcode) {
165-
case 'colour_picker': {
166-
const color = block.fields.COLOUR.value;
167-
const hex = color.substr(1);
168-
if (/^[0-9a-f]{6,8}$/.test(hex)) {
169-
return {
170-
kind: 'constant',
171-
value: Number.parseInt(hex, 16)
172-
};
173-
}
165+
case 'colour_picker':
174166
return {
175167
kind: 'constant',
176-
value: color
168+
value: block.fields.COLOUR.value
177169
};
178-
}
179170
case 'math_angle':
180171
case 'math_integer':
181172
case 'math_number':

src/compiler/jsgen.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const generatorNameVariablePool = new VariablePool('gen');
5454
* @property {() => string} asNumberOrNaN
5555
* @property {() => string} asString
5656
* @property {() => string} asBoolean
57+
* @property {() => string} asColor
5758
* @property {() => string} asUnknown
5859
* @property {() => string} asSafe
5960
* @property {() => boolean} isAlwaysNumber
@@ -93,6 +94,10 @@ class TypedInput {
9394
return `toBoolean(${this.source})`;
9495
}
9596

97+
asColor () {
98+
return this.asUnknown();
99+
}
100+
96101
asUnknown () {
97102
return this.source;
98103
}
@@ -151,6 +156,15 @@ class ConstantInput {
151156
return Cast.toBoolean(this.constantValue).toString();
152157
}
153158

159+
asColor () {
160+
// Attempt to parse hex code at compilation time
161+
if (/^#[0-9a-f]{6,8}$/i.test(this.constantValue)) {
162+
const hex = this.constantValue.substr(1);
163+
return Number.parseInt(hex, 16).toString();
164+
}
165+
return this.asUnknown();
166+
}
167+
154168
asUnknown () {
155169
// Attempt to convert strings to numbers if it is unlikely to break things
156170
if (typeof this.constantValue === 'number') {
@@ -251,6 +265,10 @@ class VariableInput {
251265
return `toBoolean(${this.source})`;
252266
}
253267

268+
asColor () {
269+
return this.asUnknown();
270+
}
271+
254272
asUnknown () {
255273
return this.source;
256274
}
@@ -607,7 +625,7 @@ class JSGenerator {
607625
case 'sensing.answer':
608626
return new TypedInput(`runtime.ext_scratch3_sensing._answer`, TYPE_STRING);
609627
case 'sensing.colorTouchingColor':
610-
return new TypedInput(`target.colorIsTouchingColor(colorToList(${this.descendInput(node.target).asUnknown()}), colorToList(${this.descendInput(node.mask).asUnknown()}))`, TYPE_BOOLEAN);
628+
return new TypedInput(`target.colorIsTouchingColor(colorToList(${this.descendInput(node.target).asColor()}), colorToList(${this.descendInput(node.mask).asColor()}))`, TYPE_BOOLEAN);
611629
case 'sensing.date':
612630
return new TypedInput(`(new Date().getDate())`, TYPE_NUMBER);
613631
case 'sensing.dayofweek':
@@ -668,7 +686,7 @@ class JSGenerator {
668686
case 'sensing.touching':
669687
return new TypedInput(`target.isTouchingObject(${this.descendInput(node.object).asUnknown()})`, TYPE_BOOLEAN);
670688
case 'sensing.touchingColor':
671-
return new TypedInput(`target.isTouchingColor(colorToList(${this.descendInput(node.color).asUnknown()}))`, TYPE_BOOLEAN);
689+
return new TypedInput(`target.isTouchingColor(colorToList(${this.descendInput(node.color).asColor()}))`, TYPE_BOOLEAN);
672690
case 'sensing.username':
673691
return new TypedInput('runtime.ioDevices.userData.getUsername()', TYPE_STRING);
674692
case 'sensing.year':
@@ -979,7 +997,7 @@ class JSGenerator {
979997
this.source += `${PEN_EXT}._setPenShadeToNumber(${this.descendInput(node.shade).asNumber()}, target);\n`;
980998
break;
981999
case 'pen.setColor':
982-
this.source += `${PEN_EXT}._setPenColorToColor(${this.descendInput(node.color).asUnknown()}, target);\n`;
1000+
this.source += `${PEN_EXT}._setPenColorToColor(${this.descendInput(node.color).asColor()}, target);\n`;
9831001
break;
9841002
case 'pen.setParam':
9851003
this.source += `${PEN_EXT}._setOrChangeColorParam(${this.descendInput(node.param).asString()}, ${this.descendInput(node.value).asNumber()}, ${PEN_STATE}, false);\n`;
Binary file not shown.

0 commit comments

Comments
 (0)