I have some colors that seem to come from a Delphi TColor variable (e.g 8388608, 128, 12632256). I need to convert those colors to their rgb values with a PHP script. How can this be done in PHP?
The Delphi TColor
type is an integer whose bits contain the actual RGB values like this:
ssBBGGRR
Extracting RGB values:
You can filter out the parts by doing a bit-wise AND
with 0xFF
, while shifting the bits to the right.
Delphi code:
R := Color and $FF;
G := (Color shr 8) and $FF;
B := (Color shr 16) and $FF;
PHP code:
R = Color & 0xFF;
G = (Color >> 8) & 0xFF;
B = (Color >> 16) & 0xFF;
Warning about system colors:
There are special colors which are derived from the system colors. (like button and window colors)
Those colors don't actually have valid RGB values set. You can detect them by checking the first eight bits. If they are non-zero you have a special system color. You can also cast the color to an integer. If it is negative, it's a system color.
In that case the R part contains the system color index.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With