# Lazarus

## Programming => General => Topic started by: Ericktux on May 12, 2018, 12:58:40 am

Title: How to use html colors in lazarus
Post by: Ericktux on May 12, 2018, 12:58:40 am
good friends.
when using the colors of this website:
https://html-color-codes.info/

Lazarus shows me other colors, but if I invest them if they work.

Code: Pascal  [Select][+][-]
1.  // #3A01DF  //online color code html
2.
3.   form1.Color:=\$3A01DF;  // show another color
4.   form1.Color:=\$DF013A;  // inverted if it works, why?

How can I use the web codes in lazarus?  :(

PD: attached image
Title: Re: How to use html colors in lazarus
Post by: josh on May 12, 2018, 01:02:50 am
Any of these links help ?

http://wiki.freepascal.org/Colors

http://wiki.freepascal.org/Convert_color_to/from_HTML
Title: Re: How to use html colors in lazarus
Post by: jamie on May 12, 2018, 01:17:12 am
Isn't that simply RGB color?

MyColor := RGBToColor(StrToInt(HTMLColorSTring));

The StrToInt interprets the "\$" at the start and knows to convert from HEX.
Title: Re: How to use html colors in lazarus
Post by: wp on May 12, 2018, 01:23:00 am
Isn't that simply RGB color?
Almost... HTML has R and B interchanged: HTMLColor #RRGGBB, but TColor = \$00BBGGRR

http://wiki.freepascal.org/Convert_color_to/from_HTML
Title: Re: How to use html colors in lazarus
Post by: Ericktux on May 12, 2018, 01:34:27 am
thanks for your help friends, I'll prove it
Title: Re: How to use html colors in lazarus
Post by: jamie on May 12, 2018, 03:15:01 am
Code: Pascal  [Select][+][-]
1. Function FlipColor(Const AValue:TColor):TColor;
2. Type F = Record L,M,H,A:Byte; End;
3. Var
4.   J:Byte;
5. Begin
6.   Result := AValue;
7.    J := F(Result).L; F(Result).L:= F(Result).H; F(Result).H:=J;
8. end;
9.
10. Function HtmlColorToColor(const S:String):TColor;
11. Begin
12.   Result := FlipColor(StrToInt(S));
13. end;
14. Function COlorToHtml(Const AValue:Tcolor):String;
15. Begin
16.   Result := '\$'+IntTOHex(FlipColor(AValue),6);
17. end;
18.

Something like that I guess...
Title: Re: How to use html colors in lazarus
Post by: Thaddy on May 12, 2018, 06:43:04 am
Needs to be packed record or even bitpacked record.
Title: Re: How to use html colors in lazarus
Post by: Ericktux on May 12, 2018, 07:11:07 am
thank you very much to all, at the moment it works like this:

We integrate the unit "ATStringProc_HtmlColor.pas" which you can obtain it from:
http://wiki.freepascal.org/Convert_color_to/from_HTML

Code: Pascal  [Select][+][-]
1. Uses
2. ATStringProc_HtmlColor,
3.
4. var
5.   colorcito: tcolor;
6.   num1: integer;
7.
8.   Form1.Color:=SHtmlColorToColor('#0040FF', num1, colorcito);  // work fine

………
ShtmlColorToColor returns color for lazarus \$FF4000

at the moment it works well  :) :)
Title: Re: How to use html colors in lazarus
Post by: jamie on May 12, 2018, 05:07:41 pm

The RECORD works fine, its single define which works out to a DWORD..

Code was tested before posted..

Title: Re: How to use html colors in lazarus
Post by: Thaddy on May 12, 2018, 06:13:14 pm
Jamie that is verification vs falsification and plain wrong: It happens to work on your system.
Title: Re: How to use html colors in lazarus
Post by: jamie on May 12, 2018, 07:07:08 pm
You make me laugh... are you ever wrong?

I also did an address test between the two using Delphi and they come out as 1 byte apart from
each other..
Title: Re: How to use html colors in lazarus
Post by: voltag on April 18, 2021, 03:09:25 pm
@jamie
I corrected your code a little.
But ATStringProc_HtmlColor its some monstrous.
Code: Pascal  [Select][+][-]
1. uses Graphics;
2. //...
3. function FlipColor(Const AValue:TColor):TColor;
4. type F = Record L,M,H,A:Byte; End;
5. var
6.   J:Byte;
7. begin
8.   //\$F0 FB FF(TColor)
9.   //#FF FB F0(HTML color)
10.   Result := AValue;
11.   J := F(Result).L;
12.   F(Result).L:= F(Result).H;
13.   F(Result).H:=J;
14. end;
15.
16. function HtmlColorToColor(S:String):TColor;
17. var k: integer;
18. begin
19.   if S[1] = '#' then S[1]:= '\$';
20.   Result := FlipColor(StrToInt(S));
21. end;
22.
23. function ColorToHtml(Const AValue:Tcolor):String;
24. begin
25.   Result := '\$'+IntTOHex(FlipColor(AValue),6);
26. end;
27.
28. //example
29. myTColor:=HtmlColorToColor('#FFA0D2');
30.

It is possible that People served the parameter differently

Title: Re: How to use html colors in lazarus
Post by: jamie on April 18, 2021, 03:36:45 pm
you know there are already functions in the fpc lib for flipping things..

leToBe(yourValue)

and so on.
Title: Re: How to use html colors in lazarus
Post by: lainz on April 18, 2021, 05:36:41 pm
You can use BGRABitmap

Code: Pascal  [Select][+][-]
1. uses
2.   BGRABitmapTypes;
3. ...
4. var
5.   p: TBGRAPixel;
6.   c: TColor;
7. ...
8. p.fromString('#e1e1e1');
9. c := p;
Title: Re: How to use html colors in lazarus
Post by: winni on April 18, 2021, 07:15:41 pm
Hi!

Also BGRABitmap:

Code: Pascal  [Select][+][-]
2. ....
3. var  tmp : TBGRAbitmap;
4. ....
5. tmp.swapRedBlue;
6. ...
7.

Winni
Title: Re: How to use html colors in lazarus
Post by: marcov on April 18, 2021, 11:25:11 pm
I use

Code: Pascal  [Select][+][-]
1. result:=(swapendian(dword(x)) shr 8) or (x and \$FF000000)

The generated code size seems about the same if I remove the redundant CONST from flipcolor, and the type of used instructions too.

Hmm,
Code: Pascal  [Select][+][-]
1.   result:=rordword(swapendian(dword(x)),8);

looks shorter on x86 :-)

Quote
movl   %eax,%edx
# Var x located in register edx
shll   \$8,%eax
andl   \$-16711936,%eax
shrl   \$8,%edx
andl   \$16711935,%edx
orl   %edx,%eax
roll   \$16,%eax
rorl   \$8,%eax

Note that the compiler can't seem to peephole a roll  16,x with a rorl 8,x  into one rol 8,x ?
Title: Re: How to use html colors in lazarus
Post by: voltag on April 19, 2021, 12:32:27 am
@marcov , @jamie
Thank you for discovering the wonderful bits manipulation functions.