< index
< 5. Image toolkit

=====================================
5.6 Getting the color or transparency of a pixel
=====================================

You can read the colors from an image with :

C++ : TCODColor TCODImage::getPixel(int x, int y) const
C   : TCOD_color_t TCOD_image_get_pixel(TCOD_image_t image,int x, int y)
Py  : image_get_pixel(image, x, y)

ParameterDescription
imageIn the C and python version, the image handler, obtained with the load function.
x,yThe pixel coordinates inside the image.
0 <= x < width
0 <= y < height
Example :

C++ : TCODImage *pix = new TCODImage(80,50);
      TCODColor col=pix->getPixel(40,25);
C   : TCOD_image_t pix = TCOD_image_new(80,50);
      TCOD_color_t col=TCOD_image_get_pixel(pix,40,25);
Py  : pix = litbcod.image_new(80,50)
      col=litbcod.image_get_pixel(pix,40,25)


If you have set a key color for this image with setKeyColor, or if this image was created from a 32 bits PNG file (with alpha layer), you can get the pixel transparency with :

C++ : int TCODImage::getAlpha(int x,int y) const
C   : int TCOD_image_get_alpha(TCOD_image_t image,int x, int y)
Py  : image_get_alpha(image, x, y)

This function returns a value between 0 (transparent pixel) and 255 (opaque pixel).
ParameterDescription
imageIn the C and python version, the image handler, obtained with the load function.
x,yThe pixel coordinates inside the image.
0 <= x < width
0 <= y < height
Example :

C++ : int alpha=pix->getAlpha(40,25);
C   : int alpha=TCOD_image_get_alpha(pix,40,25);
Py  : alpha=litbcod.image_get_alpha(pix,40,25)


You can also use this simpler version (for images with alpha layer, returns true only if alpha == 0) :

C++ : bool TCODImage::isPixelTransparent(int x,int y) const
C   : bool TCOD_image_is_pixel_transparent(TCOD_image_t image,int x, int y)
Py  : image_is_pixel_transparent(image, x, y)

ParameterDescription
imageIn the C and python version, the image handler, obtained with the load function.
x,yThe pixel coordinates inside the image.
0 <= x < width
0 <= y < height
Example :

C++ : bool transparent=pix->isPixelTransparent(40,25);
C   : bool transparent=TCOD_image_is_pixel_transparent(pix,40,25);
Py  : transparent=litbcod.image_is_pixel_transparent(pix,40,25)

insert a comment