colour.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2014 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 Dart for colour blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Dart.colour');
  26. goog.require('Blockly.Dart');
  27. Blockly.Dart.addReservedWords('Math');
  28. Blockly.Dart['colour_picker'] = function(block) {
  29. // Colour picker.
  30. var code = '\'' + block.getFieldValue('COLOUR') + '\'';
  31. return [code, Blockly.Dart.ORDER_ATOMIC];
  32. };
  33. Blockly.Dart['colour_random'] = function(block) {
  34. // Generate a random colour.
  35. Blockly.Dart.definitions_['import_dart_math'] =
  36. 'import \'dart:math\' as Math;';
  37. var functionName = Blockly.Dart.provideFunction_(
  38. 'colour_random',
  39. [ 'String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {',
  40. ' String hex = \'0123456789abcdef\';',
  41. ' var rnd = new Math.Random();',
  42. ' return \'#${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'',
  43. ' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'',
  44. ' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\';',
  45. '}']);
  46. var code = functionName + '()';
  47. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  48. };
  49. Blockly.Dart['colour_rgb'] = function(block) {
  50. // Compose a colour from RGB components expressed as percentages.
  51. var red = Blockly.Dart.valueToCode(block, 'RED',
  52. Blockly.Dart.ORDER_NONE) || 0;
  53. var green = Blockly.Dart.valueToCode(block, 'GREEN',
  54. Blockly.Dart.ORDER_NONE) || 0;
  55. var blue = Blockly.Dart.valueToCode(block, 'BLUE',
  56. Blockly.Dart.ORDER_NONE) || 0;
  57. Blockly.Dart.definitions_['import_dart_math'] =
  58. 'import \'dart:math\' as Math;';
  59. var functionName = Blockly.Dart.provideFunction_(
  60. 'colour_rgb',
  61. [ 'String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
  62. '(num r, num g, num b) {',
  63. ' num rn = (Math.max(Math.min(r, 1), 0) * 255).round();',
  64. ' String rs = rn.toInt().toRadixString(16);',
  65. ' rs = \'0$rs\';',
  66. ' rs = rs.substring(rs.length - 2);',
  67. ' num gn = (Math.max(Math.min(g, 1), 0) * 255).round();',
  68. ' String gs = gn.toInt().toRadixString(16);',
  69. ' gs = \'0$gs\';',
  70. ' gs = gs.substring(gs.length - 2);',
  71. ' num bn = (Math.max(Math.min(b, 1), 0) * 255).round();',
  72. ' String bs = bn.toInt().toRadixString(16);',
  73. ' bs = \'0$bs\';',
  74. ' bs = bs.substring(bs.length - 2);',
  75. ' return \'#$rs$gs$bs\';',
  76. '}']);
  77. var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
  78. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  79. };
  80. Blockly.Dart['colour_blend'] = function(block) {
  81. // Blend two colours together.
  82. var c1 = Blockly.Dart.valueToCode(block, 'COLOUR1',
  83. Blockly.Dart.ORDER_NONE) || '\'#000000\'';
  84. var c2 = Blockly.Dart.valueToCode(block, 'COLOUR2',
  85. Blockly.Dart.ORDER_NONE) || '\'#000000\'';
  86. var ratio = Blockly.Dart.valueToCode(block, 'RATIO',
  87. Blockly.Dart.ORDER_NONE) || 0.5;
  88. Blockly.Dart.definitions_['import_dart_math'] =
  89. 'import \'dart:math\' as Math;';
  90. var functionName = Blockly.Dart.provideFunction_(
  91. 'colour_blend',
  92. [ 'String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
  93. '(String c1, String c2, num ratio) {',
  94. ' ratio = Math.max(Math.min(ratio, 1), 0);',
  95. ' int r1 = int.parse(\'0x${c1.substring(1, 3)}\');',
  96. ' int g1 = int.parse(\'0x${c1.substring(3, 5)}\');',
  97. ' int b1 = int.parse(\'0x${c1.substring(5, 7)}\');',
  98. ' int r2 = int.parse(\'0x${c2.substring(1, 3)}\');',
  99. ' int g2 = int.parse(\'0x${c2.substring(3, 5)}\');',
  100. ' int b2 = int.parse(\'0x${c2.substring(5, 7)}\');',
  101. ' num rn = (r1 * (1 - ratio) + r2 * ratio).round();',
  102. ' String rs = rn.toInt().toRadixString(16);',
  103. ' num gn = (g1 * (1 - ratio) + g2 * ratio).round();',
  104. ' String gs = gn.toInt().toRadixString(16);',
  105. ' num bn = (b1 * (1 - ratio) + b2 * ratio).round();',
  106. ' String bs = bn.toInt().toRadixString(16);',
  107. ' rs = \'0$rs\';',
  108. ' rs = rs.substring(rs.length - 2);',
  109. ' gs = \'0$gs\';',
  110. ' gs = gs.substring(gs.length - 2);',
  111. ' bs = \'0$bs\';',
  112. ' bs = bs.substring(bs.length - 2);',
  113. ' return \'#$rs$gs$bs\';',
  114. '}']);
  115. var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
  116. return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
  117. };