Difference between revisions of "ImageData:getPixel"
(→Function: Added notice to input parameters not being interpolated, to thwart any assumptions.) |
m (Changed the example.) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
Gets the color of a pixel at a specific position in the image. | Gets the color of a pixel at a specific position in the image. | ||
− | Valid x and y values start at 0 and go up to image width and height minus 1. | + | Valid x and y values start at 0 and go up to image width and height minus 1. Non-integer values are floored. |
+ | |||
+ | In versions prior to [[11.0]], color component values were within the range of 0 to 255 instead of 0 to 1. | ||
+ | {{notice|Prior to [[0.10.2]], this function does not properly handle non-integer coordinates, and may produce an invalid result when non-integer values are passed.}} | ||
== Function == | == Function == | ||
=== Synopsis === | === Synopsis === | ||
Line 8: | Line 11: | ||
</source> | </source> | ||
=== Arguments === | === Arguments === | ||
− | {{param|number|x|The position of the pixel on the x-axis | + | {{param|number|x|The position of the pixel on the x-axis.}} |
− | {{param|number|y|The position of the pixel on the y-axis | + | {{param|number|y|The position of the pixel on the y-axis.}} |
=== Returns === | === Returns === | ||
− | {{param|number|r|The red component (0- | + | {{param|number|r|The red component (0-1).}} |
− | {{param|number|g|The green component (0- | + | {{param|number|g|The green component (0-1).}} |
− | {{param|number|b|The blue component (0- | + | {{param|number|b|The blue component (0-1).}} |
− | {{param|number|a|The alpha component (0- | + | {{param|number|a|The alpha component (0-1).}} |
== Examples == | == Examples == | ||
− | + | When the mouse is clicked, reads the red, green, and blue value of the pixel under the mouse and uses it as the background color. | |
<source lang="lua"> | <source lang="lua"> | ||
− | local | + | local imagedata = love.image.newImageData('path/to/Image.png') |
− | local | + | local image = love.graphics.newImage(imagedata) |
− | + | ||
− | + | function love.mousepressed(mx, my) | |
− | + | if 0 <= mx and mx < image:getWidth() | |
− | local | + | and 0 <= my and my < image:getHeight() then |
− | + | local r, g, b = imagedata:getPixel(mx, my) | |
+ | love.graphics.setBackgroundColor(r, g, b) | ||
end | end | ||
end | end | ||
− | + | ||
+ | function love.draw() | ||
+ | love.graphics.draw(image, 0, 0) | ||
+ | end | ||
</source> | </source> | ||
+ | |||
== See Also == | == See Also == | ||
* [[parent::ImageData]] | * [[parent::ImageData]] |
Latest revision as of 09:25, 14 January 2019
Gets the color of a pixel at a specific position in the image.
Valid x and y values start at 0 and go up to image width and height minus 1. Non-integer values are floored.
In versions prior to 11.0, color component values were within the range of 0 to 255 instead of 0 to 1.
Prior to 0.10.2, this function does not properly handle non-integer coordinates, and may produce an invalid result when non-integer values are passed. |
Function
Synopsis
r, g, b, a = ImageData:getPixel( x, y )
Arguments
Returns
number r
- The red component (0-1).
number g
- The green component (0-1).
number b
- The blue component (0-1).
number a
- The alpha component (0-1).
Examples
When the mouse is clicked, reads the red, green, and blue value of the pixel under the mouse and uses it as the background color.
local imagedata = love.image.newImageData('path/to/Image.png')
local image = love.graphics.newImage(imagedata)
function love.mousepressed(mx, my)
if 0 <= mx and mx < image:getWidth()
and 0 <= my and my < image:getHeight() then
local r, g, b = imagedata:getPixel(mx, my)
love.graphics.setBackgroundColor(r, g, b)
end
end
function love.draw()
love.graphics.draw(image, 0, 0)
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info