Stupid Color conversion problem

Hi everybody, I'm making an HTML editor and got stuck while converting colors to hexadecimal values.
I'm using this code:


This code seems to work with almost all colors. However, when the color is either clRed, clBlue or clGreen, the conversion is done incorrectly.

how can in solve this problem?

thanks everybody.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The color is actually stored in BGR form in the low order 3 bytes, so you will need to rearrange it...


pin_plunderAuthor Commented:
to easy for you, but ... how can i do that?
Like this:

  c : integer;
  s : String;

c := ColorToRGB(lblColorTexto.Font.Color);
c := (c and $FF) SHL 16 + // Red
     (c and $FF00) + // Green
     (c and $FF0000) SHR 16;  // Blue

s := IntToHex(c,6);




Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

pin_plunderAuthor Commented:
You saved my life again.

Why is this so complicated. I looked in the Delphi Help for the shl & shr operators, but didn't undertand a word.

The code you provided works and I'm glad about that, but could you please explain me what does it does exactly?

thanks a lot
A TColor stores its red, green and blue components in BGR order (rather than the RGB order you might expect).

So, to get the hex RGB string we need to rearrange the order of the RGB components from BGR to RGB. Each color component occupies one byte (8 bits) which is masked by $ff for R, $FF00 for B and $FF0000 for G. The shift operators move the R and B components to the correct places and OR (well, add) them back together again.

SHL and SHR do bitwise shifts. eg:

(Binary) 11110000 SHR 4 = 00001111

Make sense now?


pin_plunderAuthor Commented:
yes. Thanks a lot.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.