Colors in the MacroLibX
Colors in the MacroLibX are presented in an int
format just like the minilibx.
It therefore requires some tricky things in order to obtain an int which can contain the ARGB
values.
One improvement over minilibx is that MacroLibX fully supports transparency, whether in pixel put, images or string put.
🌈 The color integer standard
As mentioned above, MacroLibX uses an ARGB
color format, which in int
is equivalent to 0xAARRGGBB
.
This ARGB
is composed by 4 values :
A
, the alpha of the color (it’s transparency), between 0 and 255R
, the red component, between 0 and 255G
, the green component, between 0 and 255B
, the blue component, between 0 and 255
You can directly encode colors using hexadecimal in C/C++ like so :
But sometime we cannot use the usefull hexadecimal to encode colors…
🔐 Encode and decode colors
We can use two methods to encode and decode colors:
- bit-shifting
char
/int
conversion
Bit-shifting
Since each byte contains 2^8 = 256
values (1 byte = 8 bits), and ARGB
values range from 0 to 255,
we can perfectly fit a integer (as an int
is 4 bytes, on 64bits systems…).
In order to set the values programatically we can use bit-shifting. Let’s create a function which does precisely that for us :
Because ints are stored from right to left, we need to bit-shift each value the according amount of bits backwards.
We can also do the exact opposite and retrieve integer values from an encoded ARGB
integer :
char
/ int
conversion
As mentioned above ARGB
contains 4 bytes, and 4 bytes can be seen as 4 unsigned char
.
We can therefore use 4 unsigned char
to extract and insert ARGB
components into our colors :