Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook (preview only)

Author Topic: [SOLVED] Convert Color List Box to #HEX  (Read 492 times)

• Hero Member
• Posts: 929
[SOLVED] Convert Color List Box to #HEX
« on: April 16, 2019, 02:30:56 am »
Hi.

Using the TColorlist on Windows 7 with LAZ 2.0

Double-clicking on the red color gives me the number...  255

I am trying to convert it to a #hex number such as #FF0000 (html) in my code

I can't find it yet, can someone help?
« Last Edit: April 16, 2019, 02:38:33 am by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!

LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

• Hero Member
• Posts: 929
Re: Convert Color List Box to #HEX
« Reply #1 on: April 16, 2019, 02:33:16 am »
Let me give it a try first

http://wiki.freepascal.org/Convert_color_to/from_HTML
« Last Edit: April 16, 2019, 02:38:41 am by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!

LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

• Hero Member
• Posts: 929
Re: Convert Color List Box to #HEX
« Reply #2 on: April 16, 2019, 02:37:52 am »
There it was.... pretty easy...

Code: Pascal  [Select]
1. [
2. procedure TForm3.lstColorDblClick(Sender: TObject);
3. var
4.   N: Longint;
5.   Result: String;
6. begin
7.   if Color=clNone then
8.     begin
9.         Result:= '';
10.         exit;
11.     end;
12.
13.
14.   N:= ColorToRGB(lstColor.Selected);
15.   Result:= '#'+
16.     IntToHex(Red(N), 2)+
17.     IntToHex(Green(N), 2)+
18.     IntToHex(Blue(N), 2);
19.
20.   showmessage(Result);
21.
22. end;
23. /code]
Can't Type - Forgetful - Had Stroke = Forgive this old man!

LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

• Hero Member
• Posts: 1808
Re: [SOLVED] Convert Color List Box to #HEX
« Reply #3 on: April 16, 2019, 02:55:40 am »
Shouldn't that be:
Code: Pascal  [Select]
1. procedure TForm3.lstColorDblClick(Sender: TObject);
2. var
3.   N: Longint;
4.   Result: String;
5. begin
6.   if lstColor.Selected = clNone then
7.     begin
8.         Result:= '';
9.         exit;
10.     end;
11.
12.   N:= ColorToRGB(lstColor.Selected);
13.   Result:= '#'+
14.     IntToHex(Red(N), 2)+
15.     IntToHex(Green(N), 2)+
16.     IntToHex(Blue(N), 2);
17.
18.   showmessage(Result);
19.
20. end;

And this might be quicker:
Code: Pascal  [Select]
1.   N:= ColorToRGB(lstColor.Selected);
2.   TmpStr := IntToHex(n,8);
3.   Result:= '#'+ Copy(TmpStr, 3, 6);
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

• Hero Member
• Posts: 929
Re: [SOLVED] Convert Color List Box to #HEX
« Reply #4 on: April 16, 2019, 01:56:11 pm »
@lucamar

Yes, some of this code is better.
I got the original from WIKI.

Also, double-checking the "clNone"... the ColorList Box doesn't even display the clNone. So, I am just removing it. It will never be needed. Except maybe another color color.

UPDATED CODE:

Code: Pascal  [Select]
1. procedure TForm3.lstColorDblClick(Sender: TObject);
2. var
3.   N: Longint;
4.   Results: String;
5.   TmpStr: String;
6.
7. begin
8.
9.   N:= ColorToRGB(lstColor.Selected);
10.   TmpStr := IntToHex(n,8);
11.   Results:= '#'+ Copy(TmpStr, 3, 6);
12.
13.  txtSnippets.SelText:='&' + Results + ':';
14. end;
15.

THANKS!
Can't Type - Forgetful - Had Stroke = Forgive this old man!

LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

• Hero Member
• Posts: 1808
Re: [SOLVED] Convert Color List Box to #HEX
« Reply #5 on: April 16, 2019, 02:10:53 pm »
Dealing with strings can be wasteful both in time and space, so you may want to try this:
Code: Pascal  [Select]
1. procedure TForm3.lstColorDblClick(Sender: TObject);
2. var
3.   N: Longint;
4.   TmpStr: String;
5. begin
6.   N:= ColorToRGB(lstColor.Selected);
7.   TmpStr := IntToHex(n,8);
8.   txtSnippets.SelText:='&#38;#'+ Copy(TmpStr, 3, 6) + ':';
9. end;

I know: nitpicking. I'll stop here
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

• Hero Member
• Posts: 929
Re: [SOLVED] Convert Color List Box to #HEX
« Reply #6 on: April 16, 2019, 02:25:18 pm »
Dealing with strings can be wasteful both in time and space, so you may want to try this:
Code: Pascal  [Select]
1. procedure TForm3.lstColorDblClick(Sender: TObject);
2. var
3.   N: Longint;
4.   TmpStr: String;
5. begin
6.   N:= ColorToRGB(lstColor.Selected);
7.   TmpStr := IntToHex(n,8);
8.   txtSnippets.SelText:='&#38;#'+ Copy(TmpStr, 3, 6) + ':';
9. end;

I know: nitpicking. I'll stop here

naw... want my code to be efficient and simple as can be
Can't Type - Forgetful - Had Stroke = Forgive this old man!

LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

• Hero Member
• Posts: 929
Re: [SOLVED] Convert Color List Box to #HEX
« Reply #7 on: April 16, 2019, 02:30:18 pm »
Nope... using...

Code: Pascal  [Select]
1. txtSnippets.SelText:='&#38;#38;#'+ Copy(TmpStr, 3, 6) + ':';

just prints out "&#38;#" + number

My original is fine... '#' + number

That is how html is used.

Can't Type - Forgetful - Had Stroke = Forgive this old man!

LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

• Hero Member
• Posts: 1808
Re: [SOLVED] Convert Color List Box to #HEX
« Reply #8 on: April 16, 2019, 02:33:50 pm »
Nope... using...

Code: Pascal  [Select]
1. txtSnippets.SelText:='&#38;#38;#'+ Copy(TmpStr, 3, 6) + ':';

just prints out "&#38;#" + number

My original is fine... '#' + number

That is how html is used.

Yeah, that was some mishap of the forum software or some hidden typo I made
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

• Hero Member
• Posts: 929
Re: [SOLVED] Convert Color List Box to #HEX
« Reply #9 on: April 16, 2019, 11:48:54 pm »
Oops... found a bug in LUCAMAR's code

If I double=click on the red color in TColorList, I get 0000FF (in html that is actually Blue)

Red is FF0000

So, for now I going back to my original code

Code: Pascal  [Select]
1.
2. procedure TForm3.lstColorDblClick(Sender: TObject);
3. var
4.   N: Longint;
5.   Results: String;
6.   //TmpStr: String;
7.
8. begin
9.
10.   N:= ColorToRGB(lstColor.Selected);
11.   Results:= '#'+
12.     IntToHex(Red(N), 2)+
13.     IntToHex(Green(N), 2)+
14.     IntToHex(Blue(N), 2);
15.
16.   txtSnippets.SelText:='&' + Results + ':';
17.
18. end;
19.
20.

It needs the three IntToHEX lines
That dictates the correct RGB order.
This works.
« Last Edit: April 16, 2019, 11:52:08 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!

LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

• Hero Member
• Posts: 1808
Re: [SOLVED] Convert Color List Box to #HEX
« Reply #10 on: April 17, 2019, 01:21:35 am »
Oops... found a bug in LUCAMAR's code

Yeah. That happens when one doesn't test his code

Instead try this much more simple (and tested!) implementation:
Code: Pascal  [Select]
1.  procedure TForm3.lstColorDblClick(Sender: TObject);
2. var
3.   R, G, B: Byte;
4. begin
5.   RedGreenBlue(lstColor.Selected, R, G, B);
6.   txtSnippets.SelText:= Format('#%.2x%.2x%.2x;', [R, G, B]);
7. end;

ETA: Do note that to convert system colors (clWindow, clWindowText etc.) you still have to use ColorToRGB() to get the real color before calling RedGreenBlue().
« Last Edit: April 17, 2019, 01:43:36 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.