Recent

Author Topic: SOLVED INI read TColor  (Read 888 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 533
SOLVED INI read TColor
« on: September 06, 2022, 08:30:54 pm »
Hello forum members, how can I read and assign TColor saved in ini

Code: Pascal  [Select][+][-]
  1. [Skin]
  2. Start=$00FAAA2E
  3. End=$0098251F

Code: Pascal  [Select][+][-]
  1. var
  2.   INI: TIniFile;
  3. begin
  4.   if not OpenDialog1.Execute then Exit;
  5.   INI := TIniFile.Create(OpenDialog1.FileName);
  6.   try
  7.     with BCPanel1.Background.Gradient1 do
  8.     begin
  9.       StartColor := INI.ReadString('Skin', 'Start');
  10.       //EndColor := INI.Read;
  11.     end;
  12.   finally
  13.     INI.Free;
  14.   end;
  15. end;    
  16.  
« Last Edit: September 07, 2022, 02:21:11 pm by Pe3s »

Lulu

  • Full Member
  • ***
  • Posts: 226
Re: INI read TColor
« Reply #1 on: September 06, 2022, 08:35:52 pm »
Hi, if StartColor and EndColor are TColor type, in line 9 and 10 write:
Code: Pascal  [Select][+][-]
  1. StartColor := TColor(Ini.ReadString('Skin', 'Start').ToInteger);
  2. EndColor := TColor(Ini.ReadString('Skin', 'End').ToInteger);

But before, you should check the presence of filed Skin, Start and End in your INI file.
wishing you a nice life

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: INI read TColor
« Reply #2 on: September 06, 2022, 08:52:49 pm »
Do not use "With" statements, it may be a trap for your own coding.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: INI read TColor
« Reply #3 on: September 06, 2022, 09:21:18 pm »
I use WITH statements a lot and love them.

 As to the TINIFILE class, it has a READInteger already, there is no need to read the string and convert it.

MyIni.ReadInteger('Section', 'Name', Default_Value_Incase_It_Does_Not_Exist_Yet):Integer;

IT has lots of other types too.

The only true wisdom is knowing you know nothing

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: INI read TColor
« Reply #4 on: September 06, 2022, 09:32:22 pm »
I am not sure if the TINIFile class is good for anything but legacy handling of past century apps.
Wiki says: "Currently only INI files compliant with the more restrictive Windows ini-file format are supported", I guess it also means "no unicode"?
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: INI read TColor
« Reply #5 on: September 06, 2022, 09:52:22 pm »
If you say so, I have an enhanced version of the Tinifile and I pass all kinds of types to it.

It's easy on the eyes if you have to read the file to make manual corrections or for other software to make references to it.

 The other formats are just overwhelmed with too many inner subgroups and requirements to make them work correctly. Let alone bugs in the components that interact with them.

 For simple and quick reads and writes I find INI files work just great, but I guess if you are worried about others at some remote point that insist on using XML/JS/JSON etc., then happy camping.
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: INI read TColor
« Reply #6 on: September 06, 2022, 10:24:43 pm »
... I guess it also means "no unicode"?
You can use UTF8 (which is unicode) in TIniFile just fine.

On *nix, '.conf' is the default mecahnism to store application settings, and this is just like inifiles.

Bart

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: INI read TColor
« Reply #7 on: September 06, 2022, 10:35:36 pm »
If TIniFile handles utf8, I shall give it a try.
INI files is what I use, but I have always used my own routines, because when I tried to use the FPC implementation a long time ago, it was strictly compliant with the MS specifications.

Edit: Here is the thread - 10 years ago.
« Last Edit: September 06, 2022, 10:37:59 pm by CM630 »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: INI read TColor
« Reply #8 on: September 07, 2022, 01:37:12 am »
Here is how I do it....

Read -
Code: Pascal  [Select][+][-]
  1.         BackGndColour:=   StringToColor(Configfile.ReadString('BasicSettings', 'BackGndColour', '0'));
  2.         HiColour :=   StringToColor(Configfile.ReadString('BasicSettings', 'HiColour', '0'));
  3.         TextColour := StringToColor(Configfile.ReadString('BasicSettings', 'TextColour', '0'));
  4.         TitleColour :=  StringToColor(Configfile.ReadString('BasicSettings', 'TitleColour', '0'));
  5.  

Write -
 
Code: Pascal  [Select][+][-]
  1.                ConfigFile.writestring('BasicSettings', 'BackGndColour', ColorToString(BackGndColour));
  2.                 ConfigFile.writestring('BasicSettings', 'HiColour',      ColorToString(HiColour));
  3.                 ConfigFile.writestring('BasicSettings', 'TextColour',    ColorToString(TextColour));
  4.                 ConfigFile.writestring('BasicSettings', 'TitleColour',   ColorToString(TitleColour));
  5.  

BackGndColour, HiColour, TextColour, TitleColour are, of course, TColor

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: INI read TColor
« Reply #9 on: September 07, 2022, 03:46:38 am »
I guess using those would work for those that may have some different colors on their end.

 I believe some of those constants are variable and are red from the system on startup.

 That would mean different desktop colors would apply.
The only true wisdom is knowing you know nothing

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: INI read TColor
« Reply #10 on: September 07, 2022, 02:20:13 pm »
Thank you for your answers and comments  :)

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: SOLVED INI read TColor
« Reply #11 on: September 07, 2022, 05:12:51 pm »
Just for information. In my library: https://github.com/t-edson/MiConfig I've a function to associate a TColor to a TColorButton or TColorBox and then save/read from disk in INI or XML format.

I use this to create settings file and connect to a settings form:

Code: Pascal  [Select][+][-]
  1.  
  2.   ...
  3.   s:=cfgFile.Asoc_TCol('CodExplBack',@FilExplBack, colCodExplBack, clWindow);
  4.   s:=cfgFile.Asoc_TCol('CodExplText',@FilExplText, colCodExplText, clDefault);
  5.   ...
  6.  

Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

 

TinyPortal © 2005-2018