Bookstore

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

Author Topic: Color from unique string  (Read 2566 times)

Dibo

• Hero Member
• Posts: 1046
Color from unique string
« on: March 19, 2013, 02:08:13 pm »
Hi,

I'm using TChart . I dynamicaly create TLineSeries where count depend on list count. I would like that each series have different color depend on string item. For example:
Code: [Select]
`Series1.SeriesColor := GetUniqueColor('table1')Series2.SeriesColor := GetUniqueColor('table2')Series3.SeriesColor := GetUniqueColor('table3')etc...`Can't find any interesting function in Graphics unit

Regards

wp

• Hero Member
• Posts: 6650
Re: Color from unique string
« Reply #1 on: March 19, 2013, 02:39:06 pm »
There is no "official" function for that. But you can use a random color
Code: [Select]
`uses LCLIntf;  Series1.SeriesColor := rgb(random(256), random(256), random(256));`or use an array of predefined colors:
Code: [Select]
`const  NUMCOLORS = 10;  SERIESCOLORS: array[0..NUMCOLORS-1] of TColor = (    clRed, clBlue, clOlive, //... etc   );function GetSeriesColor(AIndex: integer): TColor;begin  Result := SERIESCOLORS[AIndex mod NUMCOLORS];end;Series1.SeriesColor := GetSeriesColor(0);Series2.SeriesColor := GetSeriesColor(1);// etc.`Note that in the last solution the colors are repeated after the predefined colors are used up.

A variation would be to read the colors from an external file which gives the user the opportunity to change the colors:

Code: [Select]
`var  SeriesColors: array of TColor;procedure ReadColorsFromFile(AFileName: string);var  List: TStringList;begin  List := TStringList.Create;  try    List.ReadFromFile(AFileName);    SetLength(SeriesColors, List.Count);    for i:=0 to List.Count-1 do      SeriesColors[i] := StringToColor(List[i]);  finally    List.Free;  end;`