Skip to content

Commit

Permalink
Add color blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Dec 6, 2024
1 parent 8bad6f6 commit c620fda
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
27 changes: 27 additions & 0 deletions extensions/web-scripts/blocks-lua.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,33 @@ define(function() {
}
return [code, Blockly.JavaScript.ORDER_MEMBER];
},
"lha_color": function(block) {
var field = block.getFieldValue('FIELD');
var value = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
var o = "utils.parseRgbHex(" + value + ")";
var indices = {"r": 1, "g": 2, "b": 3, "h": 11, "s": 12, "v": 13};
var n = indices[field];
if (n > 10) {
n = n - 10;
var o = "utils.rgbToHsv(" + o + ")";
}
var code = 'select(' + n + ', ' + o + ')';
return [code, Blockly.JavaScript.ORDER_MEMBER];
},
"lha_color_hsv": function(block) {
var h = Blockly.Lua.valueToCode(block, 'HUE', Blockly.JavaScript.ORDER_NONE);
var s = Blockly.Lua.valueToCode(block, 'SATURATION', Blockly.JavaScript.ORDER_NONE);
var v = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
var code = 'utils.formatRgbHex(utils.hsvToRgb(' + h + ', ' + s + ', ' + v + '))';
return [code, Blockly.JavaScript.ORDER_MEMBER];
},
"lha_color_rgb": function(block) {
var r = Blockly.Lua.valueToCode(block, 'RED', Blockly.JavaScript.ORDER_NONE);
var g = Blockly.Lua.valueToCode(block, 'GREEN', Blockly.JavaScript.ORDER_NONE);
var b = Blockly.Lua.valueToCode(block, 'BLUE', Blockly.JavaScript.ORDER_NONE);
var code = 'utils.formatRgbHex(' + r + ', ' + g + ', ' + b + ')';
return [code, Blockly.JavaScript.ORDER_MEMBER];
},
// -- Experimental --------
"lha_get_field": function(block) {
var name = block.getFieldValue('NAME');
Expand Down
61 changes: 58 additions & 3 deletions extensions/web-scripts/blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@

"lha_to_string": {
"tooltip" : "A string representing the value",
"message0": "to String %1",
"message0": "to string %1",
"args0": [{
"type": "input_value",
"name": "VALUE"
Expand All @@ -145,7 +145,7 @@
},
"lha_time": {
"tooltip" : "The current time in seconds since epoch",
"message0": "get time",
"message0": "time",
"args0": [],
"output": null,
"colour": "$lhaExpressionColor"
Expand Down Expand Up @@ -202,7 +202,7 @@
},
"lha_date": {
"tooltip" : "The date time field value from the time in seconds since epoch",
"message0": "get date field %1 from %2",
"message0": "date field %1 from %2",
"args0": [{
"type": "field_dropdown",
"name": "FIELD",
Expand All @@ -224,6 +224,61 @@
"output": null,
"colour": "$lhaExpressionColor"
},
"lha_color": {
"tooltip" : "The color component from 0 to 1",
"message0": "color component %1 from %2",
"args0": [{
"type": "field_dropdown",
"name": "FIELD",
"options": [
[ "red", "r" ],
[ "green", "g" ],
[ "blue", "b" ],
[ "hue", "h" ],
[ "saturation", "s" ],
[ "value", "v" ]
]
}, {
"type": "input_value",
"name": "VALUE"
}],
"output": null,
"colour": "$lhaExpressionColor"
},
"lha_color_hsv": {
"tooltip" : "The color from HSV components",
"message0": "hsv %1 %2 %3",
"args0": [{
"type": "input_value",
"name": "HUE"
}, {
"type": "input_value",
"name": "SATURATION"
}, {
"type": "input_value",
"name": "VALUE"
}],
"inputsInline": true,
"output": null,
"colour": "$lhaExpressionColor"
},
"lha_color_rgb": {
"tooltip" : "The color from RGB components",
"message0": "rgb %1 %2 %3",
"args0": [{
"type": "input_value",
"name": "RED"
}, {
"type": "input_value",
"name": "GREEN"
}, {
"type": "input_value",
"name": "BLUE"
}],
"inputsInline": true,
"output": null,
"colour": "$lhaExpressionColor"
},

"lha_log": {
"tooltip" : "Logs the specified message",
Expand Down
3 changes: 3 additions & 0 deletions extensions/web-scripts/toolbox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<block type="lha_date"></block>
<block type="lha_parse_time"></block>
<block type="lha_format_time"></block>
<block type="lha_color"></block>
<block type="lha_color_hsv"></block>
<block type="lha_color_rgb"></block>
</category>
<category name="Experimental" colour="0">
<block type="lha_log"></block>
Expand Down
13 changes: 10 additions & 3 deletions lha/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,16 @@ function utils.rgbToHsv(r, g, b)
return h, s, v
end

local function floatToByte(v)
return math.floor(v * 255) & 255
end

local function byteToFloat(v)
return (v & 255) / 255
end

function utils.formatRgbHex(r, g, b)
return string.format('#%02X%02X%02X', math.floor(r * 255), math.floor(g * 255), math.floor(b * 255))
return string.format('#%02X%02X%02X', floatToByte(r), floatToByte(g), floatToByte(b))
end

function utils.parseRgbHex(rgbHex)
Expand All @@ -381,8 +389,7 @@ function utils.parseRgbHex(rgbHex)
end
local rgb = hex:decode(rgbHex)
local r, g, b = string.byte(rgb, 1, 3)
return r / 255, g / 255, b / 255
return byteToFloat(r), byteToFloat(g), byteToFloat(b)
end


return utils

0 comments on commit c620fda

Please sign in to comment.