refactor: convert python block generators to goog.module (#5771)

* refactor: convert generators/python/colour.js to goog.module

* refactor: convert generators/python/colour.js to named requires

* chore: run clang-format

* refactor: convert generators/python/lists.js to goog.module

* refactor: convert generators/python/lists.js to named requires

* chore: run clang-format

* refactor: convert generators/python/logic.js to goog.module

* refactor: convert generators/python/logic.js to named requires

* chore: run clang-format

* refactor: convert generators/python/loops.js to goog.module

* refactor: convert generators/python/loops.js to named requires

* chore: run clang-format

* refactor: convert generators/python/math.js to goog.module

* refactor: convert generators/python/math.js to named requires

* chore: run clang-format

* refactor: convert generators/python/procedures.js to goog.module

* refactor: convert generators/python/procedures.js to named requires

* chore: run clang-format

* refactor: convert generators/python/text.js to goog.module

* refactor: convert generators/python/text.js to named requires

* chore: run clang-format

* refactor: convert generators/python/variables_dynamic.js to named requires

* refactor: convert generators/python/variables.js to named requires

* chore: run clang-format

* refactor: convert generators/python.js to goog.module

* refactor: convert generators/python.js to named requires

* chore: run clang-format

* chore: remove spurious @private annotations

* chore: rebuild
This commit is contained in:
Rachel Fenichel
2021-12-01 20:43:08 -08:00
committed by GitHub
parent c0517ea360
commit 931499295e
16 changed files with 680 additions and 769 deletions

View File

@@ -9,63 +9,59 @@
*/
'use strict';
goog.provide('Blockly.Python.colour');
goog.module('Blockly.Python.colour');
goog.require('Blockly.Python');
const Python = goog.require('Blockly.Python');
Blockly.Python['colour_picker'] = function(block) {
Python['colour_picker'] = function(block) {
// Colour picker.
const code = Blockly.Python.quote_(block.getFieldValue('COLOUR'));
return [code, Blockly.Python.ORDER_ATOMIC];
const code = Python.quote_(block.getFieldValue('COLOUR'));
return [code, Python.ORDER_ATOMIC];
};
Blockly.Python['colour_random'] = function(block) {
Python['colour_random'] = function(block) {
// Generate a random colour.
Blockly.Python.definitions_['import_random'] = 'import random';
Python.definitions_['import_random'] = 'import random';
const code = '\'#%06x\' % random.randint(0, 2**24 - 1)';
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
return [code, Python.ORDER_FUNCTION_CALL];
};
Blockly.Python['colour_rgb'] = function(block) {
Python['colour_rgb'] = function(block) {
// Compose a colour from RGB components expressed as percentages.
const functionName = Blockly.Python.provideFunction_(
'colour_rgb',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b):',
' r = round(min(100, max(0, r)) * 2.55)',
' g = round(min(100, max(0, g)) * 2.55)',
' b = round(min(100, max(0, b)) * 2.55)',
' return \'#%02x%02x%02x\' % (r, g, b)']);
const r = Blockly.Python.valueToCode(block, 'RED',
Blockly.Python.ORDER_NONE) || 0;
const g = Blockly.Python.valueToCode(block, 'GREEN',
Blockly.Python.ORDER_NONE) || 0;
const b = Blockly.Python.valueToCode(block, 'BLUE',
Blockly.Python.ORDER_NONE) || 0;
const functionName = Python.provideFunction_('colour_rgb', [
'def ' + Python.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b):',
' r = round(min(100, max(0, r)) * 2.55)',
' g = round(min(100, max(0, g)) * 2.55)',
' b = round(min(100, max(0, b)) * 2.55)',
' return \'#%02x%02x%02x\' % (r, g, b)'
]);
const r = Python.valueToCode(block, 'RED', Python.ORDER_NONE) || 0;
const g = Python.valueToCode(block, 'GREEN', Python.ORDER_NONE) || 0;
const b = Python.valueToCode(block, 'BLUE', Python.ORDER_NONE) || 0;
const code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
return [code, Python.ORDER_FUNCTION_CALL];
};
Blockly.Python['colour_blend'] = function(block) {
Python['colour_blend'] = function(block) {
// Blend two colours together.
const functionName = Blockly.Python.provideFunction_(
'colour_blend',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
'(colour1, colour2, ratio):',
' r1, r2 = int(colour1[1:3], 16), int(colour2[1:3], 16)',
' g1, g2 = int(colour1[3:5], 16), int(colour2[3:5], 16)',
' b1, b2 = int(colour1[5:7], 16), int(colour2[5:7], 16)',
' ratio = min(1, max(0, ratio))',
' r = round(r1 * (1 - ratio) + r2 * ratio)',
' g = round(g1 * (1 - ratio) + g2 * ratio)',
' b = round(b1 * (1 - ratio) + b2 * ratio)',
' return \'#%02x%02x%02x\' % (r, g, b)']);
const colour1 = Blockly.Python.valueToCode(block, 'COLOUR1',
Blockly.Python.ORDER_NONE) || '\'#000000\'';
const colour2 = Blockly.Python.valueToCode(block, 'COLOUR2',
Blockly.Python.ORDER_NONE) || '\'#000000\'';
const ratio = Blockly.Python.valueToCode(block, 'RATIO',
Blockly.Python.ORDER_NONE) || 0;
const code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
const functionName = Python.provideFunction_('colour_blend', [
'def ' + Python.FUNCTION_NAME_PLACEHOLDER_ + '(colour1, colour2, ratio):',
' r1, r2 = int(colour1[1:3], 16), int(colour2[1:3], 16)',
' g1, g2 = int(colour1[3:5], 16), int(colour2[3:5], 16)',
' b1, b2 = int(colour1[5:7], 16), int(colour2[5:7], 16)',
' ratio = min(1, max(0, ratio))',
' r = round(r1 * (1 - ratio) + r2 * ratio)',
' g = round(g1 * (1 - ratio) + g2 * ratio)',
' b = round(b1 * (1 - ratio) + b2 * ratio)',
' return \'#%02x%02x%02x\' % (r, g, b)'
]);
const colour1 =
Python.valueToCode(block, 'COLOUR1', Python.ORDER_NONE) || '\'#000000\'';
const colour2 =
Python.valueToCode(block, 'COLOUR2', Python.ORDER_NONE) || '\'#000000\'';
const ratio = Python.valueToCode(block, 'RATIO', Python.ORDER_NONE) || 0;
const code =
functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
return [code, Python.ORDER_FUNCTION_CALL];
};