Skip to content

Commit

Permalink
ADDED: GetImageColor() raysan5#2024
Browse files Browse the repository at this point in the history
  • Loading branch information
raysan5 committed Oct 3, 2021
1 parent 92417c4 commit 9b3d74d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ RLAPI Color *LoadImagePalette(Image image, int maxPaletteSize, int *colorCount);
RLAPI void UnloadImageColors(Color *colors); // Unload color data loaded with LoadImageColors()
RLAPI void UnloadImagePalette(Color *colors); // Unload colors palette loaded with LoadImagePalette()
RLAPI Rectangle GetImageAlphaBorder(Image image, float threshold); // Get image alpha border rectangle
RLAPI Color GetImageColor(Image image, int x, int y); // Get image pixel color at (x, y) position

// Image drawing functions
// NOTE: Image software-rendering functions (CPU)
Expand Down
103 changes: 103 additions & 0 deletions src/rtextures.c
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,109 @@ Rectangle GetImageAlphaBorder(Image image, float threshold)
return crop;
}

// Get image pixel color at (x, y) position
Color GetImageColor(Image image, int x, int y)
{
Color color = { 0 };

if ((x >=0) && (x < image.width) && (y >= 0) && (y < image.height))
{
switch (image.format)
{
case PIXELFORMAT_UNCOMPRESSED_GRAYSCALE:
{
color.r = ((unsigned char *)image.data)[y*image.width + x];
color.g = ((unsigned char *)image.data)[y*image.width + x];
color.b = ((unsigned char *)image.data)[y*image.width + x];
color.a = 255;

} break;
case PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA:
{
color.r = ((unsigned char *)image.data)[(y*image.width + x)*2];
color.g = ((unsigned char *)image.data)[(y*image.width + x)*2];
color.b = ((unsigned char *)image.data)[(y*image.width + x)*2];
color.a = ((unsigned char *)image.data)[(y*image.width + x)*2 + 1];

} break;
case PIXELFORMAT_UNCOMPRESSED_R5G5B5A1:
{
unsigned short pixel = ((unsigned short *)image.data)[y*image.width + x];

color.r = (unsigned char)((float)((pixel & 0b1111100000000000) >> 11)*(255/31));
color.g = (unsigned char)((float)((pixel & 0b0000011111000000) >> 6)*(255/31));
color.b = (unsigned char)((float)((pixel & 0b0000000000111110) >> 1)*(255/31));
color.a = (unsigned char)((pixel & 0b0000000000000001)*255);

} break;
case PIXELFORMAT_UNCOMPRESSED_R5G6B5:
{
unsigned short pixel = ((unsigned short *)image.data)[y*image.width + x];

color.r = (unsigned char)((float)((pixel & 0b1111100000000000) >> 11)*(255/31));
color.g = (unsigned char)((float)((pixel & 0b0000011111100000) >> 5)*(255/63));
color.b = (unsigned char)((float)(pixel & 0b0000000000011111)*(255/31));
color.a = 255;

} break;
case PIXELFORMAT_UNCOMPRESSED_R4G4B4A4:
{
unsigned short pixel = ((unsigned short *)image.data)[y*image.width + x];

color.r = (unsigned char)((float)((pixel & 0b1111000000000000) >> 12)*(255/15));
color.g = (unsigned char)((float)((pixel & 0b0000111100000000) >> 8)*(255/15));
color.b = (unsigned char)((float)((pixel & 0b0000000011110000) >> 4)*(255/15));
color.a = (unsigned char)((float)(pixel & 0b0000000000001111)*(255/15));

} break;
case PIXELFORMAT_UNCOMPRESSED_R8G8B8A8:
{
color.r = ((unsigned char *)image.data)[(y*image.width + x)*4];
color.g = ((unsigned char *)image.data)[(y*image.width + x)*4 + 1];
color.b = ((unsigned char *)image.data)[(y*image.width + x)*4 + 2];
color.a = ((unsigned char *)image.data)[(y*image.width + x)*4 + 3];

} break;
case PIXELFORMAT_UNCOMPRESSED_R8G8B8:
{
color.r = (unsigned char)((unsigned char *)image.data)[(y*image.width + x)*3];
color.g = (unsigned char)((unsigned char *)image.data)[(y*image.width + x)*3 + 1];
color.b = (unsigned char)((unsigned char *)image.data)[(y*image.width + x)*3 + 2];
color.a = 255;

} break;
case PIXELFORMAT_UNCOMPRESSED_R32:
{
color.r = (unsigned char)(((float *)image.data)[y*image.width + x]*255.0f);
color.g = 0;
color.b = 0;
color.a = 255;

} break;
case PIXELFORMAT_UNCOMPRESSED_R32G32B32:
{
color.r = (unsigned char)(((float *)image.data)[(y*image.width + x)*3]*255.0f);
color.g = (unsigned char)(((float *)image.data)[(y*image.width + x)*3 + 1]*255.0f);
color.b = (unsigned char)(((float *)image.data)[(y*image.width + x)*3 + 2]*255.0f);
color.a = 255;

} break;
case PIXELFORMAT_UNCOMPRESSED_R32G32B32A32:
{
color.r = (unsigned char)(((float *)image.data)[(y*image.width + x)*4]*255.0f);
color.g = (unsigned char)(((float *)image.data)[(y*image.width + x)*4]*255.0f);
color.b = (unsigned char)(((float *)image.data)[(y*image.width + x)*4]*255.0f);
color.a = (unsigned char)(((float *)image.data)[(y*image.width + x)*4]*255.0f);

} break;
default: TRACELOG(LOG_WARNING, "Compressed image format does not support color reading"); break;
}
}
else TRACELOG(LOG_WARNING, "Requested image pixel (%i, %i) out of bounds", x, y);

return color;
}

//------------------------------------------------------------------------------------
// Image drawing functions
//------------------------------------------------------------------------------------
Expand Down

0 comments on commit 9b3d74d

Please sign in to comment.