summaryrefslogtreecommitdiffhomepage
path: root/src/rtextures.c
diff options
context:
space:
mode:
authorRay <[email protected]>2021-10-03 12:55:52 +0200
committerRay <[email protected]>2021-10-03 12:55:52 +0200
commit9b3d74db6bd9e38bc1447f636897990bbd698b4b (patch)
treeb894b76f1841eeca80ed6f65b44507e74d6b8dc8 /src/rtextures.c
parent92417c4485cc65f1ebc194eb2001a90d3082d4df (diff)
downloadraylib-9b3d74db6bd9e38bc1447f636897990bbd698b4b.tar.gz
raylib-9b3d74db6bd9e38bc1447f636897990bbd698b4b.zip
ADDED: GetImageColor() #2024
Diffstat (limited to 'src/rtextures.c')
-rw-r--r--src/rtextures.c103
1 files changed, 103 insertions, 0 deletions
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
//------------------------------------------------------------------------------------