Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a delphi TColor with PHP

Tags:

php

delphi

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?

like image 306
Flo Avatar asked Jan 24 '23 10:01

Flo


1 Answers

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.

like image 126
Daniel Rikowski Avatar answered Jan 30 '23 04:01

Daniel Rikowski