Skip to content

Commit

Permalink
Added functionality to draw pixel and read color
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Jun 17, 2018
1 parent d4e6d92 commit b438465
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 15 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,42 @@ Draw a line between two points:

`alpha` - alpha channel 0..255. Optional parameter for rgba textures

#### drawpixels.pixel(buffer_info, x, y, red, green, blue, alpha)
Draw a pixel:

`buffer_info` - buffer information

`x` - x position of pixel

`y` - y position of pixel

`red` - red channel of the color 0..255

`green` - green channel of the color 0..255

`blue` - blue channel of the color 0..255

`alpha` - alpha channel 0..255. Optional parameter for rgba textures

#### drawpixels.color(buffer_info, x, y)
Read color from a position in the buffer:

`buffer_info` - buffer information

`x` - x position to get color from

`y` - y position to get color from

RETURNS:

`red` - red channel of the color 0..255

`green` - green channel of the color 0..255

`blue` - blue channel of the color 0..255

`alpha` - alpha channel 0..255. Optional parameter for rgba textures

--------

If you have any questions or suggestions contact me: [email protected]
46 changes: 46 additions & 0 deletions drawpixels/src/drawpixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,50 @@ static int draw_line(lua_State* L) {
return 0;
}

static int draw_pixel(lua_State* L) {
int top = lua_gettop(L) + 4;

read_and_validate_buffer_info(L, 1);
int32_t x = luaL_checknumber(L, 2);
int32_t y = luaL_checknumber(L, 3);
uint32_t r = luaL_checknumber(L, 4);
uint32_t g = luaL_checknumber(L, 5);
uint32_t b = luaL_checknumber(L, 6);
uint32_t a = 0;
if (lua_isnumber(L, 7) == 1)
{
a = luaL_checknumber(L, 7);
}

putpixel(x, y, r, g, b, a);

assert(top == lua_gettop(L));
return 0;
}

static int read_color(lua_State* L) {
int top = lua_gettop(L) + 4;

read_and_validate_buffer_info(L, 1);
int32_t x = luaL_checknumber(L, 2);
int32_t y = luaL_checknumber(L, 3);

if (!in_buffer(x, y)) {
assert(top == lua_gettop(L));
return 0;
}

int i = xytoi(x, y);
lua_pushnumber(L, buffer_info.bytes[i]);
lua_pushnumber(L, buffer_info.bytes[i + 1]);
lua_pushnumber(L, buffer_info.bytes[i + 2]);
if (buffer_info.channels == 4) {
lua_pushnumber(L, buffer_info.bytes[i + 3]);
}
assert(top + buffer_info.channels == lua_gettop(L));
return buffer_info.channels;
}

//https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
static int draw_circle(lua_State* L) {
int top = lua_gettop(L) + 4;
Expand Down Expand Up @@ -426,6 +470,8 @@ static const luaL_reg Module_methods[] = {
{"fill", fill_texture},
{"rect", draw_rect},
{"filled_rect", draw_filled_rect},
{"pixel", draw_pixel},
{"color", read_color},
{0, 0}
};

Expand Down
9 changes: 8 additions & 1 deletion example/canvas.script
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ local function color_vector_to_bytes(color)
return color.x * 255, color.y * 255, color.z * 255, color.w * 255
end

local function bytes_to_color_vector(r, g, b, a)
return vmath.vector4(r / 255, g / 255, b / 255, a / 255)
end

function on_input(self, action_id, action)
if action_id == hash("touch") then
local pos = vmath.vector3(action.x, action.y, 0)
Expand Down Expand Up @@ -97,7 +101,10 @@ function on_input(self, action_id, action)
while length > 0 do
local r, g, b, a = color_vector_to_bytes(self.current_color)
if self.current_tool == "pencil" then
drawpixels.filled_rect(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 4, 4, r, g, b, a)
drawpixels.pixel(self.buffer_info, self.touch_pos.x, self.touch_pos.y, r, g, b, a)
elseif self.current_tool == "colorpicker" then
local r,g,b,a = drawpixels.color(self.buffer_info, self.touch_pos.x, self.touch_pos.y)
self.current_color = bytes_to_color_vector(r, g, b, a)
elseif self.current_tool == "circle" then
drawpixels.circle(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 40, r, g, b, a)
elseif self.current_tool == "line" and self.last_pos and next(self.last_pos) then
Expand Down
3 changes: 3 additions & 0 deletions example/draw_pixels.atlas
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ images {
images {
image: "/example/images/line.png"
}
images {
image: "/example/images/gimp-tool-color-picker.png"
}
margin: 0
extrude_borders: 2
inner_padding: 0
Binary file added example/images/gimp-tool-color-picker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 69 additions & 14 deletions example/toolbox.gui
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ nodes {
}
nodes {
position {
x: -46.804
x: -64.0
y: 10.711
z: 0.0
w: 1.0
Expand Down Expand Up @@ -729,7 +729,7 @@ nodes {
}
nodes {
position {
x: -46.804
x: -64.0
y: -34.704
z: 0.0
w: 1.0
Expand Down Expand Up @@ -784,7 +784,7 @@ nodes {
}
nodes {
position {
x: -105.0
x: -133.0
y: 10.0
z: 0.0
w: 1.0
Expand All @@ -802,7 +802,7 @@ nodes {
w: 1.0
}
size {
x: 60.0
x: 24.0
y: 30.0
z: 0.0
w: 1.0
Expand Down Expand Up @@ -851,8 +851,8 @@ nodes {
w: 1.0
}
scale {
x: 1.0
y: 1.0
x: 0.8
y: 0.8
z: 1.0
w: 1.0
}
Expand All @@ -863,14 +863,14 @@ nodes {
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
x: 0.3019608
y: 0.3019608
z: 0.3019608
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "Clear"
text: "CLR"
font: "system_font"
id: "text"
xanchor: XANCHOR_NONE
Expand Down Expand Up @@ -957,7 +957,7 @@ nodes {
}
nodes {
position {
x: -120.0
x: -133.0
y: -35.0
z: 0.0
w: 1.0
Expand Down Expand Up @@ -1012,7 +1012,7 @@ nodes {
}
nodes {
position {
x: -160.0
x: -167.0
y: -35.0
z: 0.0
w: 1.0
Expand Down Expand Up @@ -1067,7 +1067,7 @@ nodes {
}
nodes {
position {
x: -160.0
x: -167.0
y: 10.0
z: 0.0
w: 1.0
Expand Down Expand Up @@ -1177,7 +1177,7 @@ nodes {
}
nodes {
position {
x: -83.0
x: -99.0
y: -35.0
z: 0.0
w: 1.0
Expand Down Expand Up @@ -1230,6 +1230,61 @@ nodes {
template_node_child: false
size_mode: SIZE_MODE_AUTO
}
nodes {
position {
x: -99.0
y: 10.711
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 24.0
y: 24.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: "draw_pixels/gimp-tool-color-picker"
id: "colorpicker"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
parent: "toolbox"
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_AUTO
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_LEGACY
max_nodes: 512
5 changes: 5 additions & 0 deletions example/toolbox.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ local function use_line()
msg.post("canvas", "change_tool", { tool = "line" })
end

local function use_colorpicker()
msg.post("canvas", "change_tool", { tool = "colorpicker" })
end

local function clear()
msg.post("canvas", "clear")
end
Expand All @@ -51,6 +55,7 @@ function init(self)
self.nodes[gui.get_node("filled_rect")] = use_filled_rect
self.nodes[gui.get_node("rotated_rect")] = use_rotated_rect
self.nodes[gui.get_node("line")] = use_line
self.nodes[gui.get_node("colorpicker")] = use_colorpicker
for i = 1, 10 do
self.nodes[gui.get_node("color" .. i)] = pick_color
end
Expand Down

0 comments on commit b438465

Please sign in to comment.