@@ -54,6 +54,7 @@ const generatorNameVariablePool = new VariablePool('gen');
54
54
* @property {() => string } asNumberOrNaN
55
55
* @property {() => string } asString
56
56
* @property {() => string } asBoolean
57
+ * @property {() => string } asColor
57
58
* @property {() => string } asUnknown
58
59
* @property {() => string } asSafe
59
60
* @property {() => boolean } isAlwaysNumber
@@ -93,6 +94,10 @@ class TypedInput {
93
94
return `toBoolean(${ this . source } )` ;
94
95
}
95
96
97
+ asColor ( ) {
98
+ return this . asUnknown ( ) ;
99
+ }
100
+
96
101
asUnknown ( ) {
97
102
return this . source ;
98
103
}
@@ -151,6 +156,15 @@ class ConstantInput {
151
156
return Cast . toBoolean ( this . constantValue ) . toString ( ) ;
152
157
}
153
158
159
+ asColor ( ) {
160
+ // Attempt to parse hex code at compilation time
161
+ if ( / ^ # [ 0 - 9 a - 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
+
154
168
asUnknown ( ) {
155
169
// Attempt to convert strings to numbers if it is unlikely to break things
156
170
if ( typeof this . constantValue === 'number' ) {
@@ -251,6 +265,10 @@ class VariableInput {
251
265
return `toBoolean(${ this . source } )` ;
252
266
}
253
267
268
+ asColor ( ) {
269
+ return this . asUnknown ( ) ;
270
+ }
271
+
254
272
asUnknown ( ) {
255
273
return this . source ;
256
274
}
@@ -607,7 +625,7 @@ class JSGenerator {
607
625
case 'sensing.answer' :
608
626
return new TypedInput ( `runtime.ext_scratch3_sensing._answer` , TYPE_STRING ) ;
609
627
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 ) ;
611
629
case 'sensing.date' :
612
630
return new TypedInput ( `(new Date().getDate())` , TYPE_NUMBER ) ;
613
631
case 'sensing.dayofweek' :
@@ -668,7 +686,7 @@ class JSGenerator {
668
686
case 'sensing.touching' :
669
687
return new TypedInput ( `target.isTouchingObject(${ this . descendInput ( node . object ) . asUnknown ( ) } )` , TYPE_BOOLEAN ) ;
670
688
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 ) ;
672
690
case 'sensing.username' :
673
691
return new TypedInput ( 'runtime.ioDevices.userData.getUsername()' , TYPE_STRING ) ;
674
692
case 'sensing.year' :
@@ -979,7 +997,7 @@ class JSGenerator {
979
997
this . source += `${ PEN_EXT } ._setPenShadeToNumber(${ this . descendInput ( node . shade ) . asNumber ( ) } , target);\n` ;
980
998
break ;
981
999
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` ;
983
1001
break ;
984
1002
case 'pen.setParam' :
985
1003
this . source += `${ PEN_EXT } ._setOrChangeColorParam(${ this . descendInput ( node . param ) . asString ( ) } , ${ this . descendInput ( node . value ) . asNumber ( ) } , ${ PEN_STATE } , false);\n` ;
0 commit comments