colour.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 JavaScript for colour blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.JavaScript.colour');
  26. goog.require('Blockly.JavaScript');
  27. Blockly.JavaScript['colour_picker'] = function(block) {
  28. // Colour picker.
  29. var code = '\'' + block.getFieldValue('COLOUR') + '\'';
  30. return [code, Blockly.JavaScript.ORDER_ATOMIC];
  31. };
  32. Blockly.JavaScript['colour_random'] = function(block) {
  33. // Generate a random colour.
  34. var functionName = Blockly.JavaScript.provideFunction_(
  35. 'colour_random',
  36. [ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '() {',
  37. ' var num = Math.floor(Math.random() * Math.pow(2, 24));',
  38. ' return \'#\' + (\'00000\' + num.toString(16)).substr(-6);',
  39. '}']);
  40. var code = functionName + '()';
  41. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  42. };
  43. Blockly.JavaScript['colour_rgb'] = function(block) {
  44. // Compose a colour from RGB components expressed as percentages.
  45. var red = Blockly.JavaScript.valueToCode(block, 'RED',
  46. Blockly.JavaScript.ORDER_COMMA) || 0;
  47. var green = Blockly.JavaScript.valueToCode(block, 'GREEN',
  48. Blockly.JavaScript.ORDER_COMMA) || 0;
  49. var blue = Blockly.JavaScript.valueToCode(block, 'BLUE',
  50. Blockly.JavaScript.ORDER_COMMA) || 0;
  51. var functionName = Blockly.JavaScript.provideFunction_(
  52. 'colour_rgb',
  53. [ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
  54. '(r, g, b) {',
  55. ' r = Math.max(Math.min(Number(r), 100), 0) * 2.55;',
  56. ' g = Math.max(Math.min(Number(g), 100), 0) * 2.55;',
  57. ' b = Math.max(Math.min(Number(b), 100), 0) * 2.55;',
  58. ' r = (\'0\' + (Math.round(r) || 0).toString(16)).slice(-2);',
  59. ' g = (\'0\' + (Math.round(g) || 0).toString(16)).slice(-2);',
  60. ' b = (\'0\' + (Math.round(b) || 0).toString(16)).slice(-2);',
  61. ' return \'#\' + r + g + b;',
  62. '}']);
  63. var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
  64. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  65. };
  66. Blockly.JavaScript['colour_blend'] = function(block) {
  67. // Blend two colours together.
  68. var c1 = Blockly.JavaScript.valueToCode(block, 'COLOUR1',
  69. Blockly.JavaScript.ORDER_COMMA) || '\'#000000\'';
  70. var c2 = Blockly.JavaScript.valueToCode(block, 'COLOUR2',
  71. Blockly.JavaScript.ORDER_COMMA) || '\'#000000\'';
  72. var ratio = Blockly.JavaScript.valueToCode(block, 'RATIO',
  73. Blockly.JavaScript.ORDER_COMMA) || 0.5;
  74. var functionName = Blockly.JavaScript.provideFunction_(
  75. 'colour_blend',
  76. [ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
  77. '(c1, c2, ratio) {',
  78. ' ratio = Math.max(Math.min(Number(ratio), 1), 0);',
  79. ' var r1 = parseInt(c1.substring(1, 3), 16);',
  80. ' var g1 = parseInt(c1.substring(3, 5), 16);',
  81. ' var b1 = parseInt(c1.substring(5, 7), 16);',
  82. ' var r2 = parseInt(c2.substring(1, 3), 16);',
  83. ' var g2 = parseInt(c2.substring(3, 5), 16);',
  84. ' var b2 = parseInt(c2.substring(5, 7), 16);',
  85. ' var r = Math.round(r1 * (1 - ratio) + r2 * ratio);',
  86. ' var g = Math.round(g1 * (1 - ratio) + g2 * ratio);',
  87. ' var b = Math.round(b1 * (1 - ratio) + b2 * ratio);',
  88. ' r = (\'0\' + (r || 0).toString(16)).slice(-2);',
  89. ' g = (\'0\' + (g || 0).toString(16)).slice(-2);',
  90. ' b = (\'0\' + (b || 0).toString(16)).slice(-2);',
  91. ' return \'#\' + r + g + b;',
  92. '}']);
  93. var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
  94. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  95. };