GMS 2 Color Components from Buffer

Hello, I am using buffer_get_surface to read color data for a sprite. However, I do not know how to get the color components from the color itself. I tried using color_get, which worked when I was using draw_getpixel, but seems to be always returning 0 in this situation.

Here is the relevant code:
Code:
var c = buffer_read(buffer, buffer_u32);
var r = colour_get_red(c);
var g = colour_get_green(c);
var b = colour_get_blue(c);
Everything except getting the color components is working correctly, as far as I can tell. When I ran this code in the script, it returned a valid color value, but 0 for the rgb components. I manually checked the color values to the pixels that it should be returning and they match up.

Code:
show_debug_message("R"+string(r)+" G"+string(g)+" B"+string(b)+" C"+string(c));
Please tell me if there's a different way to do this (maybe with bit functions?) and why my current code doesn't work. If my code should work, but doesn't, I'll post more so hopefully I can find the problem.

Thank you!
 

Simon Gust

Member
a pixel from a surface has actually 4 components, red, green, blue and alpha. And each value of the buffer is a single component. Here in your code you just read the red component. So each component takes up 1 byte -> buffer_u8.
Code:
var r = buffer_read(buffer, buffer_u8); // red
var g = buffer_read(buffer, buffer_u8); // green
var b = buffer_read(buffer, buffer_u8); // blue
var a = buffer_read(buffer, buffer_u8); // alpha
The allignement of the buffer should be 1.
 
I'm aware. I'm asking how I could take the full pixel data and break it down into it's components.

Based on my understanding, you're suggesting I just read them separately, but isn't there a way to use the full color data I pulled from the buffer and modify that to return the color components? (I don't need alpha at all). If not, I can just do it they way you posted above, but I'd still like to know.
 

Simon Gust

Member
I'm aware. I'm asking how I could take the full pixel data and break it down into it's components.

Based on my understanding, you're suggesting I just read them separately, but isn't there a way to use the full color data I pulled from the buffer and modify that to return the color components? (I don't need alpha at all). If not, I can just do it they way you posted above, but I'd still like to know.
There is no way to get the color hex of a surface unless you use surface_getpixel(). buffer_get_surface() always gives you a batch of channels. I wish it wasn't like that.
 
I saw a coding video where he found the alpha value by bit-masking:
Code:
var a = c & 255;
I did some research and I know how it works, but not well enough to apply that to the rest of it. Any ideas?
 

Simon Gust

Member
I saw a coding video where he found the alpha value by bit-masking:
Code:
var a = c & 255;
I did some research and I know how it works, but not well enough to apply that to the rest of it. Any ideas?
How do you want to aquire the variable c?
The built-in functions color_get... should work I believe.
It would be:
Code:
var red = c & 255;
var grn = c & 65280;
var blu = c & 16711680;
var alp = c & 4278190080;
manually.
 
EDIT: Okay, I managed to do it that way. However, it's important to note that those values must be modified in order to return 0-255 (which I didn't specify, but I did want).

Here is my final code for anyone searching later:
Code:
var c = buffer_read(buffer, buffer_u32);
var r = (c & 0xff);
var g = ((c & 0xff00)/256);
var b = ((c & 0xff0000)/65536);
The dividing shifts the bits over to make them 0-255.
 
Last edited:

Simon Gust

Member
EDIT: Okay, I managed to do it that way. However, it's important to note that those values must be modified in order to return 0-255 (which I didn't specify, but I did want).

Here is my final code for anyone searching later:
Code:
var c = buffer_read(buffer, buffer_u32);
var r = (c & 0xff);
var g = ((c & 0xff00)/256);
var b = ((c & 0xff0000)/65536);
The dividing shifts the bits over to make them 0-255.
There is a shift function for bits too if you like
Code:
var r = (c & 0xff);
var g = (c & 0xff00) >> 8;
var b = (c & 0xff0000) >> 16;
A tip also, is to open the windows calculator and go to the programmer design, there you can set individual bits and see what they are in decimal and hexadecimal.
 
Top