colour.js 4.0 KB

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