1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/**
* @license
* Visual Blocks Language
*
* Copyright 2015 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Generating PHP for colour blocks.
* @author daarond@gmail.com (Daaron Dwyer)
*/
'use strict';
goog.provide('Blockly.PHP.colour');
goog.require('Blockly.PHP');
Blockly.PHP['colour_picker'] = function(block) {
// Colour picker.
var code = '\'' + block.getFieldValue('COLOUR') + '\'';
return [code, Blockly.PHP.ORDER_ATOMIC];
};
Blockly.PHP['colour_random'] = function(block) {
// Generate a random colour.
var functionName = Blockly.PHP.provideFunction_(
'colour_random',
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '() {',
' return \'#\' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), ' +
'6, \'0\', STR_PAD_LEFT);',
'}']);
var code = functionName + '()';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['colour_rgb'] = function(block) {
// Compose a colour from RGB components expressed as percentages.
var red = Blockly.PHP.valueToCode(block, 'RED',
Blockly.PHP.ORDER_COMMA) || 0;
var green = Blockly.PHP.valueToCode(block, 'GREEN',
Blockly.PHP.ORDER_COMMA) || 0;
var blue = Blockly.PHP.valueToCode(block, 'BLUE',
Blockly.PHP.ORDER_COMMA) || 0;
var functionName = Blockly.PHP.provideFunction_(
'colour_rgb',
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($r, $g, $b) {',
' $r = round(max(min($r, 100), 0) * 2.55);',
' $g = round(max(min($g, 100), 0) * 2.55);',
' $b = round(max(min($b, 100), 0) * 2.55);',
' $hex = \'#\';',
' $hex .= str_pad(dechex($r), 2, \'0\', STR_PAD_LEFT);',
' $hex .= str_pad(dechex($g), 2, \'0\', STR_PAD_LEFT);',
' $hex .= str_pad(dechex($b), 2, \'0\', STR_PAD_LEFT);',
' return $hex;',
'}']);
var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['colour_blend'] = function(block) {
// Blend two colours together.
var c1 = Blockly.PHP.valueToCode(block, 'COLOUR1',
Blockly.PHP.ORDER_COMMA) || '\'#000000\'';
var c2 = Blockly.PHP.valueToCode(block, 'COLOUR2',
Blockly.PHP.ORDER_COMMA) || '\'#000000\'';
var ratio = Blockly.PHP.valueToCode(block, 'RATIO',
Blockly.PHP.ORDER_COMMA) || 0.5;
var functionName = Blockly.PHP.provideFunction_(
'colour_blend',
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($c1, $c2, $ratio) {',
' $ratio = max(min($ratio, 1), 0);',
' $r1 = hexdec(substr($c1, 1, 2));',
' $g1 = hexdec(substr($c1, 3, 2));',
' $b1 = hexdec(substr($c1, 5, 2));',
' $r2 = hexdec(substr($c2, 1, 2));',
' $g2 = hexdec(substr($c2, 3, 2));',
' $b2 = hexdec(substr($c2, 5, 2));',
' $r = round($r1 * (1 - $ratio) + $r2 * $ratio);',
' $g = round($g1 * (1 - $ratio) + $g2 * $ratio);',
' $b = round($b1 * (1 - $ratio) + $b2 * $ratio);',
' $hex = \'#\';',
' $hex .= str_pad(dechex($r), 2, \'0\', STR_PAD_LEFT);',
' $hex .= str_pad(dechex($g), 2, \'0\', STR_PAD_LEFT);',
' $hex .= str_pad(dechex($b), 2, \'0\', STR_PAD_LEFT);',
' return $hex;',
'}']);
var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
|