# How to get the color of a point at x,y-coordinate?

Hi,

I’d like to know how to get the color of a point in different situations.

1. get color at coord(x,y) of the main screen
2. get color at coord(x,y) of an image loaded to memory
3. get color at coord(x,y) of a sprite

If these functions already exist - in which format do they return the color?

That’s something which is quite “low level”… Why do you need to do this?

Yes, I already thought it might be quite a low level question, that’s why I thought the functions do already exist and I’m just not wise enough to find them.
However, after asking my question for 3 different conditions, I hope I can provide examples for all these.

1. get color at coord(x,y) of the main screen
Imagine the screen filled with randomly positioned dots (using primitive drawing functions) - some are green and the others are red.
The player is in control of an entity, able to move around on screen.
When the playersprite is in collision with a green dot, the score is raised, when the playersprite is in collision with a blue dot, the score is lowered.

2. get color at coord(x,y) of an image loaded to memory
This is the most important to me atm.
I’d like to load a 256 color greyscale heigthmap image into the memory. This image doesn’t need to be displayed itself.
I would have to step pixel by pixel through the whole image to generate 3D coordinates out of it.
The 2D x and y coordinates would be used for the 3D x and z and the 3d y-value are to be read out of the colorvalue of the specified point.
Lets assume we check the point(1,1) and get the rgb(12,12,12) as a result, then the y-coord would be something like rgbr(colorvalueofpoint(1,1))=12, so the 3D coordinates would come out as x=1,y=12,z=1.

Although GD doesn’t have full 3D functionality it might allow a 3D attempt using voxels.

1. get color at coord(x,y) of a sprite
Well, I don’t have an example. I just thought, maybe if there are no solutions regarding images, there might be a solution using sprites instead.

I have a specific need for an answer to this question. I want to get the color value at a specific point so I can create a behavior that moves my sprite towards the direction with less luminosity. I can calc luminosity if I have the color. This is for a simulation of perception behaviors project.

The extension “read pixels” will give you the RGB values (along with alpha) which you can combine into the string “red value;green value; blue value” like “222;123;0”

This would get the color at the cursor.

Once the extension is installed, in the expressions section

1 Like