Board index » delphi » Convert String Var to Color?

Convert String Var to Color?

How do I convert a color contained a string variable to a font color, such
as:
   var
        s: string;
s := clBlack   // or clWindowText, etc
Edit1.Font.Color := inttostr(s) // this dont work... what will?

The string variable assignment is coming from a text file containing the
color designation.

Thanks, -Bob

 

Re:Convert String Var to Color?


In article <Wm0f8.48454$Ef3.2427641...@newssvr14.news.prodigy.com>, "Bob Pless"

Quote
<pl...@pacbell.net> writes:
>How do I convert a color contained a string variable to a font color, such
>as:
>   var
>        s: string;
>s := clBlack   // or clWindowText, etc
>Edit1.Font.Color := inttostr(s) // this dont work... what will?

IdentToColor()

... or ...

StringToColor()

Find "String" or "Color" in Delphi Help - lots of good things in help <g>

Alan Lloyd
alangll...@aol.com

Re:Convert String Var to Color?


{ #######################################################

Quote
}

Function MyColorToString( Value: Tcolor ) : String ;
  Begin

  Result := Copy( Graphics.ColorToString( Value ), 3, 50 ) ;

End; {MyColorToString}

{ #######################################################

Quote
}

Function MyStringToColor( S : String ) : Tcolor ;
  Begin
  Result := Graphics.StringToColor( 'cl' + S ) ;

End; {MyStringToColor}

On Wed, 27 Feb 2002 08:00:54 GMT, "Bob Pless" <pl...@pacbell.net>
wrote:

Quote
>How do I convert a color contained a string variable to a font color, such
>as:
>   var
>        s: string;
>s := clBlack   // or clWindowText, etc
>Edit1.Font.Color := inttostr(s) // this dont work... what will?

>The string variable assignment is coming from a text file containing the
>color designation.

>Thanks, -Bob

Re:Convert String Var to Color?


In article <Wm0f8.48454$Ef3.2427641...@newssvr14.news.prodigy.com>, Bob
Pless <pl...@pacbell.net> writes
Quote
>How do I convert a color contained a string variable to a font color, such
>as:
>   var
>        s: string;
>s := clBlack   // or clWindowText, etc
>Edit1.Font.Color := inttostr(s) // this dont work... what will?

>The string variable assignment is coming from a text file containing the
>color designation.

It sounds to me like you are trying to store and retrieve colors (or
colours if you speak English rather than Merkan) from a text file. The
Color propery of most objects is of type TColor and can be stored as an
integer. So, what I do is to store them in a text .INI file using the
WriteInteger method of a TIniFile object and retrieve them with its
ReadInteger method. Here is some sample code which saves an object's
DefaultColor property (of type TColor) in an ini file. In this example
it is saving the DefaultColor property of a couple of Color comboboxes
from Raize components, but the principle is the same for anything which
has a Color property of type TColor:

////////////////////////////////////////////////////////////////////////
// On the Appearances tab take the current settings and set them up as//
//the defaults both visually and in the decay.ini file settings.      //
////////////////////////////////////////////////////////////////////////

procedure TfrmConfig.btnMakeDefaultClick(Sender: TObject);
var
  MyIni: TIniFile;
  i: Integer;
begin
  MyIni := TIniFile.Create(ExtractFilePath(ParamStr(0)) + '\myini.ini');
  try
    {Change the DefaultColor property of all TRzColorComboBox objects to
their currently showing colour then set their ItemIndex property to zero
to show the Default setting.}
    for i := 0 to Pred(ComponentCount) do
      if Components[i] is TRzColorComboBox then begin
        TRzColorComboBox(Components[i]).DefaultColor :=
        TRzColorComboBox(Components[i]).SelectedColor;
        TRzColorComboBox(Components[i]).ItemIndex := 0;
      end;
    {Write these values out to the [Defaults] section of the ini file}
    with MyIni do begin
      WriteInteger('Defaults', 'CentreFont', ccbStartAct.DefaultColor);
      WriteInteger('Defaults', 'LeftUpperFont',
ccbNewTime.DefaultColor);
    end;
  finally
    MyIni.Free;
  end;
end;

The ReadInteger and WriteInteger methods then handle all the string to
integer conversions for you.
--
Barney Tyrwhitt-Drake

Re:Convert String Var to Color?


Quote
"Bob Pless" <pl...@pacbell.net> wrote in message

news:Wm0f8.48454$Ef3.2427641971@newssvr14.news.prodigy.com...

Quote
> How do I convert a color contained a string variable to a font color, such

You have StringToColor and ColorToString functions in Graphics unit.

Re:Convert String Var to Color?


Thanks all for your help! Its so simple I feel like a newbie... I had brain
freeze when strtoint didn't work.

-Bob

Other Threads