colour.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating Pseudo for colour blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Pseudo.colour');
  26. goog.require('Blockly.Pseudo');
  27. Blockly.Pseudo['colour_picker'] = function(block) {
  28. // Colour picker.
  29. var code = '\'' + block.getFieldValue('COLOUR') + '\'';
  30. return [code, Blockly.Pseudo.ORDER_ATOMIC];
  31. };
  32. Blockly.Pseudo['colour_random'] = function(block) {
  33. // Generate a random colour.
  34. Blockly.Pseudo.definitions_['import_random'] = 'import random';
  35. var code = '\'#%06x\' % random.randint(0, 2**24 - 1)';
  36. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  37. };
  38. Blockly.Pseudo['colour_rgb'] = function(block) {
  39. // Compose a colour from RGB components expressed as percentages.
  40. var functionName = Blockly.Pseudo.provideFunction_(
  41. 'colour_rgb',
  42. [ 'def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b):',
  43. ' r = round(min(100, max(0, r)) * 2.55)',
  44. ' g = round(min(100, max(0, g)) * 2.55)',
  45. ' b = round(min(100, max(0, b)) * 2.55)',
  46. ' return \'#%02x%02x%02x\' % (r, g, b)']);
  47. var r = Blockly.Pseudo.valueToCode(block, 'RED',
  48. Blockly.Pseudo.ORDER_NONE) || 0;
  49. var g = Blockly.Pseudo.valueToCode(block, 'GREEN',
  50. Blockly.Pseudo.ORDER_NONE) || 0;
  51. var b = Blockly.Pseudo.valueToCode(block, 'BLUE',
  52. Blockly.Pseudo.ORDER_NONE) || 0;
  53. var code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
  54. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  55. };
  56. Blockly.Pseudo['colour_blend'] = function(block) {
  57. // Blend two colours together.
  58. var functionName = Blockly.Pseudo.provideFunction_(
  59. 'colour_blend',
  60. ['def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ +
  61. '(colour1, colour2, ratio):',
  62. ' r1, r2 = int(colour1[1:3], 16), int(colour2[1:3], 16)',
  63. ' g1, g2 = int(colour1[3:5], 16), int(colour2[3:5], 16)',
  64. ' b1, b2 = int(colour1[5:7], 16), int(colour2[5:7], 16)',
  65. ' ratio = min(1, max(0, ratio))',
  66. ' r = round(r1 * (1 - ratio) + r2 * ratio)',
  67. ' g = round(g1 * (1 - ratio) + g2 * ratio)',
  68. ' b = round(b1 * (1 - ratio) + b2 * ratio)',
  69. ' return \'#%02x%02x%02x\' % (r, g, b)']);
  70. var colour1 = Blockly.Pseudo.valueToCode(block, 'COLOUR1',
  71. Blockly.Pseudo.ORDER_NONE) || '\'#000000\'';
  72. var colour2 = Blockly.Pseudo.valueToCode(block, 'COLOUR2',
  73. Blockly.Pseudo.ORDER_NONE) || '\'#000000\'';
  74. var ratio = Blockly.Pseudo.valueToCode(block, 'RATIO',
  75. Blockly.Pseudo.ORDER_NONE) || 0;
  76. var code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
  77. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  78. };