summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/raylib.h1
-rw-r--r--src/rtextures.c103
2 files changed, 104 insertions, 0 deletions
diff --git a/src/raylib.h b/src/raylib.h
index a1150c4d..7554f75f 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -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)
diff --git a/src/rtextures.c b/src/rtextures.c
index ed88c318..132f613b 100644
--- a/src/rtextures.c
+++ b/src/rtextures.c
@@ -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
//------------------------------------------------------------------------------------