Recent

Author Topic: Change Tcolor names to html colors?  (Read 7758 times)

kevinspencer33

  • New Member
  • *
  • Posts: 22
    • Adenochrome
Change Tcolor names to html colors?
« on: February 10, 2010, 05:06:46 pm »
 I'm writting a CSS editor now and I have a little problem converting named tcolors to html color.
 How I can convert these tcolor value of a named color to string as the other colors (example = '$004848A7')?   

davesimplewear

  • Sr. Member
  • ****
  • Posts: 319
    • Davids Freeware
Re: Change Tcolor names to html colors?
« Reply #1 on: February 10, 2010, 09:11:03 pm »
Maybe try this function

function

  TColorToHex( Color : TColor )

    : string;

begin

  Result :=

    { red value }

    IntToHex( GetRValue( Color ), 2 ) +

    { green value }

    IntToHex( GetGValue( Color ), 2 ) +

    { blue value }

    IntToHex( GetBValue( Color ), 2 );

end;

Regards
Dave
All things considered insanity seems the best option

kevinspencer33

  • New Member
  • *
  • Posts: 22
    • Adenochrome
Re: Change Tcolor names to html colors?
« Reply #2 on: February 11, 2010, 01:31:58 pm »
Thanks, I come up with a different algorithm, but yours is smaller and better in general.
 :D
Mine is was:
Code: [Select]
function TForm1.coltohtmlcol(acol:tcolor): string;
var
r,g,b:integer;
r1,g1,b1:string;
begin

  r := red(acol);
  r1:= format('%02x',[r]);
    if RightStr(r1,1) =' ' then r1:='0'+LeftStr(r1,2);
    if (r1='0 ') or (r1=' 0') then r1:='00';

  g := green(acol);
  g1:= format('%02x',[g]);
    if RightStr(g1,1) =' ' then g1:='0'+LeftStr(r1,2);
    if (g1='0 ') or (g1=' 0')then g1:='00';


  b := blue(acol);
  b1:= format('%02x',[b]);

    if RightStr(b1,1) =' ' then b1:='0'+LeftStr(b1,2);
    if (b1='0 ') or (b1=' 0') then b1:='00';

result := '#'+r1+g1+b1;
end;

Mine works, but...

So, thank you again.
« Last Edit: February 11, 2010, 02:55:18 pm by kevinspencer33 »

kevinspencer33

  • New Member
  • *
  • Posts: 22
    • Adenochrome
Re: Change Tcolor names to html colors?
« Reply #3 on: February 11, 2010, 02:54:39 pm »
davesimplewear , I know is supposed to be "pseudocode-ish" but I noticed some errors in your code:
 
  • GetRValue() have to be red() (same for the others Get_Value)
  • is not a good idea to use "Color" as a variable in a function, because it can hide the color property of the form.
  • the "#" is not added

And I  decided to post a function that can compile just in case someone needs this function just can copy an paste it.

Here it is:
Code: [Select]
function coltohtml(acol:tcolor): string;
begin
Result := '#'+

    { red value }

    IntToHex( Red( aCol ), 2 ) +

    { green value }

    IntToHex( green( aCol ), 2 ) +

    { blue value }

    IntToHex( blue( aCol ), 2 );

end;  




 

TinyPortal © 2005-2018