colour.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2016 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 Lua for colour blocks.
  22. * @author rodrigoq@google.com (Rodrigo Queiro)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Lua.colour');
  26. goog.require('Blockly.Lua');
  27. Blockly.Lua['colour_picker'] = function(block) {
  28. // Colour picker.
  29. var code = '\'' + block.getFieldValue('COLOUR') + '\'';
  30. return [code, Blockly.Lua.ORDER_ATOMIC];
  31. };
  32. Blockly.Lua['colour_random'] = function(block) {
  33. // Generate a random colour.
  34. var code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
  35. return [code, Blockly.Lua.ORDER_HIGH];
  36. };
  37. Blockly.Lua['colour_rgb'] = function(block) {
  38. // Compose a colour from RGB components expressed as percentages.
  39. var functionName = Blockly.Lua.provideFunction_(
  40. 'colour_rgb',
  41. ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b)',
  42. ' r = math.floor(math.min(100, math.max(0, r)) * 2.55 + .5)',
  43. ' g = math.floor(math.min(100, math.max(0, g)) * 2.55 + .5)',
  44. ' b = math.floor(math.min(100, math.max(0, b)) * 2.55 + .5)',
  45. ' return string.format("#%02x%02x%02x", r, g, b)',
  46. 'end']);
  47. var r = Blockly.Lua.valueToCode(block, 'RED',
  48. Blockly.Lua.ORDER_NONE) || 0;
  49. var g = Blockly.Lua.valueToCode(block, 'GREEN',
  50. Blockly.Lua.ORDER_NONE) || 0;
  51. var b = Blockly.Lua.valueToCode(block, 'BLUE',
  52. Blockly.Lua.ORDER_NONE) || 0;
  53. var code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
  54. return [code, Blockly.Lua.ORDER_HIGH];
  55. };
  56. Blockly.Lua['colour_blend'] = function(block) {
  57. // Blend two colours together.
  58. var functionName = Blockly.Lua.provideFunction_(
  59. 'colour_blend',
  60. ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
  61. '(colour1, colour2, ratio)',
  62. ' local r1 = tonumber(string.sub(colour1, 2, 3), 16)',
  63. ' local r2 = tonumber(string.sub(colour2, 2, 3), 16)',
  64. ' local g1 = tonumber(string.sub(colour1, 4, 5), 16)',
  65. ' local g2 = tonumber(string.sub(colour2, 4, 5), 16)',
  66. ' local b1 = tonumber(string.sub(colour1, 6, 7), 16)',
  67. ' local b2 = tonumber(string.sub(colour2, 6, 7), 16)',
  68. ' local ratio = math.min(1, math.max(0, ratio))',
  69. ' local r = math.floor(r1 * (1 - ratio) + r2 * ratio + .5)',
  70. ' local g = math.floor(g1 * (1 - ratio) + g2 * ratio + .5)',
  71. ' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
  72. ' return string.format("#%02x%02x%02x", r, g, b)',
  73. 'end']);
  74. var colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
  75. Blockly.Lua.ORDER_NONE) || '\'#000000\'';
  76. var colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
  77. Blockly.Lua.ORDER_NONE) || '\'#000000\'';
  78. var ratio = Blockly.Lua.valueToCode(block, 'RATIO',
  79. Blockly.Lua.ORDER_NONE) || 0;
  80. var code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
  81. return [code, Blockly.Lua.ORDER_HIGH];
  82. };