Difference between revisions of "ImageData:getPixel"

(Have API docs describe what the API does (instead of what it doesn't do, or what the author thinks the user ought to do).)
(Updated for 11.0)
Line 2: Line 2:
  
 
Valid x and y values start at 0 and go up to image width and height minus 1. Non-integer values are floored.
 
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.}}
 
{{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 ==
Line 12: Line 14:
 
{{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-255).}}
+
{{param|number|r|The red component (0-1).}}
{{param|number|g|The green component (0-255).}}
+
{{param|number|g|The green component (0-1).}}
{{param|number|b|The blue component (0-255).}}
+
{{param|number|b|The blue component (0-1).}}
{{param|number|a|The alpha component (0-255).}}
+
{{param|number|a|The alpha component (0-1).}}
  
 
== Examples ==
 
== Examples ==

Revision as of 20:28, 1 April 2018

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.

O.png 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

number x
The position of the pixel on the x-axis.
number y
The position of the pixel on the y-axis.

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

Iterates over the pixels of an image and stores them in a sequence.

local image = love.image.newImageData( 'path/to/Image.png' )
local pixels = {}
for x = 1, image:getWidth() do
    for y = 1, image:getHeight() do
        -- Pixel coordinates range from 0 to image width - 1 / height - 1.
        local pixel = image:getPixel( x - 1, y - 1 )
        pixels[#pixels + 1] = pixel
    end
end
return pixels

See Also


Other Languages