ADDED: GetImageColor() #2024

This commit is contained in:
Ray 2021-10-03 12:55:52 +02:00
parent 92417c4485
commit 9b3d74db6b
2 changed files with 104 additions and 0 deletions

View File

@ -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)

View File

@ -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
//------------------------------------------------------------------------------------