[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# Stupid Color conversion problem

Posted on 2000-02-09
Medium Priority
139 Views
Hi everybody, I'm making an HTML editor and got stuck while converting colors to hexadecimal values.
I'm using this code:

IntToHex(ColorToRGB(lblColorTexto.Font.Color),6)

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.
0
Question by:pin_plunder
• 3
• 3

LVL 12

Expert Comment

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

Cheers,

Raymond.
0

Author Comment

ID: 2506251
to easy for you, but ... how can i do that?
0

LVL 12

Accepted Solution

rwilson032697 earned 40 total points
ID: 2506272
Like this:

var
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);

Cheers,

Raymond.

0

Author Comment

ID: 2506481
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
0

LVL 12

Expert Comment

ID: 2506741
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?

Cheers,

Raymond.
0

Author Comment

ID: 2507255
yes. Thanks a lot.
....
again
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
###### Suggested Courses
Course of the Month9 days, 11 hours left to enroll