About palette's. How can I add my own "default " palettes, a

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
H.Hasenack (S&G)
Newbie
Newbie
Posts: 4
Joined: Mon Apr 18, 2016 12:00 am

About palette's. How can I add my own "default " palettes, a

Post by H.Hasenack (S&G) » Fri Jun 17, 2016 11:26 am

I want to add my own "fixed" palette definitions, but would rather not rewrite the tchart code too much .

:shock: Custom palette is currently only halfway implemented since it does not allow me to adda "named" palete in a sensible way.

I have modified some chart sources to allow my series to be linked to a fixed palette entry (through index).
Now I would like to add my own, named, palette definitions to the tchart editor palette list, but steema's code learns me the palette resolving is "hardcoded" through a case statement, rather than through a neatly organized dictionary or something like that.

:roll: See

Code: Select all

class procedure TColorPalettes.GetPalette(Index:Integer; out Palette:TColorArray);
Any tips/hints on how to add my own custom, named palettes from within my own source code?

I would like something like:

Code: Select all

ColorPalettes.AddPalette('MyAppPalette',cMyColorPaletteArray);
and off course it's counbterpart:

Code: Select all

ColorPalettes.DeletePalette('MyAppPalette');
and for completeness,

Code: Select all

ColorPalettes.Clear;
that makes sense.



Thanks

Hans

Yeray
Site Admin
Site Admin
Posts: 9614
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: About palette's. How can I add my own "default " palettes, a

Post by Yeray » Mon Jun 20, 2016 2:48 pm

Hello Hans,

I've added it to the public tracker:
http://bugs.teechart.net/show_bug.cgi?id=1561
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

H.Hasenack (S&G)
Newbie
Newbie
Posts: 4
Joined: Mon Apr 18, 2016 12:00 am

Re: About palette's. How can I add my own "default " palettes, a

Post by H.Hasenack (S&G) » Wed Jun 29, 2016 1:11 pm

I have implemented it already as I cannot wait on you guys to prioritize it. Here are the cganges I made to accomplish the functionality that I require.

It still lacks an UnregisterPalette routine.
ColorPalettes.Delete() and ColorPalettes.Clear will result in memory leaks, but should be fairly easy to fix. (I don't need it)

TeeProcs.pas

Code: Select all

interface
...
type TColorArray=Array of TColor;
{$ifdef HH_PATCH_REGISTERPALETTE}
  PColorArray=^TColorArray;
{$endif}
Chart.pas

Code: Select all

interface 
...
  TColorPalettes=class(TStringList)
  public
    class procedure AddTo(const Items:TStrings; AddDefault:Boolean=False);
    class procedure ApplyPalette(const Chart:TCustomAxisPanel; Index:Integer); overload;
    class procedure ApplyPalette(const Chart:TCustomAxisPanel; const Palette:Array of TColor); overload;
    class procedure GetPalette(Index:Integer; out Palette:TColorArray);
{$ifdef HH_PATCH_REGISTERPALETTE}
    class procedure RegisterPalette(const aName:string; const aPalette: array of TColor);
    destructor Destroy; override;
{$endif}
  end;

{$ifdef HH_PATCH_REGISTERPALETTE}
  function ColorPalettes : TColorPalettes;
{$endif}

var
{$ifdef HH_PATCH_REGISTERPALETTE}
  vColorPalettes : TColorPalettes=nil;
{$else}
  ColorPalettes : TColorPalettes=nil;
{$endif}

implementation
...
{$ifdef HH_PATCH_REGISTERPALETTE}
class procedure TColorPalettes.RegisterPalette(const aName:string; const aPalette: array of TColor);

  procedure lclCopyPalette(VAR aTargetPalette:TColorArray);
  VAR i:integer;
  begin
    SetLength(aTargetPalette,low(aPalette)+length(aPalette));	
    for i:=low(aTargetPalette) to high(aTargetPalette) do
	  aTargetPalette[i]:=clTeeColor; // ensure entire array is initialized
    for i:=low(aPalette) to high(aPalette) do	
      aTargetPalette[i]:=aPalette[i]; // actually copy palette entries
  end;

VAR idx:integer;
    PPalette:PColorArray;
begin
  with ColorPalettes do
  begin
    idx:=IndexOf(aName);
    if idx>=0 then // replace existing named palette
    begin
      PPalette:=PColorArray(Objects[idx]);
      lclCopyPalette(PPalette^);
    end
    else // register new palette
    begin
      new(PPalette);
      AddObject(aName,TObject(PPalette));
      lclCopyPalette(PPalette^);
    end;
  end;
end;

destructor TColorPalettes.Destroy; 
VAR i:integer;
    obj:TObject;
begin
  for i:=0 to Count-1 do
  begin
    obj:=Objects[i];
    if Assigned(obj) then
    begin
      Objects[i]:=nil;
      setlength(PColorArray(obj)^,0);
      Dispose(PColorArray(obj));
    end;
  end;
  inherited;
end;

function ColorPalettes : TColorPalettes;
begin
  if not Assigned(vColorPalettes) then
  begin
    vColorPalettes:=TColorPalettes.Create;
    with vColorPalettes do
    begin
      RegisterPalette(TeeMsg_ThemeTeeChart,[]);                        //    0
      RegisterPalette(TeeMsg_ThemeExcel,ExcelPalette);                 //    1
      RegisterPalette(TeeMsg_ThemeVictorian,VictorianPalette);         //    2
      RegisterPalette(TeeMsg_ThemePastels,PastelsPalette);             //    3
      RegisterPalette(TeeMsg_ThemeSolid,SolidPalette);                 //    4
      RegisterPalette(TeeMsg_ThemeClassic,ClassicPalette);             //    5
      RegisterPalette(TeeMsg_ThemeWeb,WebPalette);                     //    6
      RegisterPalette(TeeMsg_ThemeModern,ModernPalette);               //    7
      RegisterPalette(TeeMsg_ThemeRainbow,RainbowPalette);             //    8
      RegisterPalette(TeeMsg_ThemeWinXP,WindowsXPPalette);             //    9
      RegisterPalette(TeeMsg_ThemeMacOS,MacOSPalette);                 //   10
      RegisterPalette(TeeMsg_ThemeWinVista,WindowsVistaPalette);       //   11
      RegisterPalette(TeeMsg_ThemeGrayScale,GrayScalePalette);         //   12
      RegisterPalette(TeeMsg_ThemeOpera,OperaPalette);                 //   13
      RegisterPalette(TeeMsg_ThemeWarm,WarmPalette);                   //   14
      RegisterPalette(TeeMsg_ThemeCool,CoolPalette);                   //   15
      RegisterPalette(TeeMsg_ThemeBlack,OnBlackPalette);               //   16
      RegisterPalette(TeeMsg_Metro,MetroPalette);                      //   17
      RegisterPalette(TeeMsg_iOSPalette,iOSPalette);                   //   18
      RegisterPalette(TeeMsg_AndroidPalette,AndroidPalette);           //   19
      RegisterPalette(TeeMsg_LookoutPalette,LookoutPalette);           //   20
      RegisterPalette(TeeMsg_SeawashPalette,SeawashPalette);           //   21
    end;
  end;
  Result:=vColorPalettes;
end;
{$endif}

...

class procedure TColorPalettes.GetPalette(Index:Integer; out Palette:TColorArray);

  procedure CopyPalette(const Source:Array of TColor);
  var t : Integer;
      {$IFDEF FMX}
      tmp : Byte;
      {$ENDIF}
  begin
    SetLength(Palette,Length(Source));

    for t:=0 to Length(Source)-1 do
    {$IFDEF FMX}
    begin
      Palette[t]:=Source[t];

      with TAlphaColorRec(Palette[t]) do
      begin
        tmp:=R;
        R:=B;
        B:=tmp;
        A:=$FF;
      end;

    end;
    {$ELSE}
        Palette[t]:={$IFDEF FMX}TAlphaColors.Alpha or {$ENDIF}Source[t];
    {$ENDIF}
  end;

begin
{$ifdef HH_PATCH_REGISTERPALETTE}
  with ColorPalettes do
  begin
    if InRange(Index,0,Count-1) then
      CopyPalette(PColorArray(Objects[Index])^)
    else CopyPalette([]);
  end;

{$else}
  case Index of
    1: CopyPalette(ExcelPalette);
    2: CopyPalette(VictorianPalette);
    3: CopyPalette(PastelsPalette);
    4: CopyPalette(SolidPalette);
    5: CopyPalette(ClassicPalette);
    6: CopyPalette(WebPalette);
    7: CopyPalette(ModernPalette);
    8: CopyPalette(RainbowPalette);
    9: CopyPalette(WindowsXPPalette);
   10: CopyPalette(MacOSPalette);
   11: CopyPalette(WindowsVistaPalette);
   12: CopyPalette(GrayScalePalette);
   13: CopyPalette(OperaPalette);
   14: CopyPalette(WarmPalette);
   15: CopyPalette(CoolPalette);
   16: CopyPalette(OnBlackPalette);
   17: CopyPalette(MetroPalette);
   18: CopyPalette(iOSPalette);
   19: CopyPalette(AndroidPalette);
   20: CopyPalette(LookoutPalette);
   21: CopyPalette(SeawashPalette);
  else
    CopyPalette([]);
  end;
{$endif}
end;
...

initialization
  Randomize;
  CheckTeeSeriesTypes;
  CheckTeeToolTypes;
  CheckTeeAnimationTypes;

{$ifndef HH_PATCH_REGISTERPALETTE}
  ColorPalettes:=TColorPalettes.Create;
  ColorPalettes.Add(TeeMsg_ThemeTeeChart);
  ColorPalettes.Add(TeeMsg_ThemeExcel);
  ColorPalettes.Add(TeeMsg_ThemeVictorian);
  ColorPalettes.Add(TeeMsg_ThemePastels);
  ColorPalettes.Add(TeeMsg_ThemeSolid);
  ColorPalettes.Add(TeeMsg_ThemeClassic);
  ColorPalettes.Add(TeeMsg_ThemeWeb);
  ColorPalettes.Add(TeeMsg_ThemeModern);
  ColorPalettes.Add(TeeMsg_ThemeRainbow);
  ColorPalettes.Add(TeeMsg_ThemeWinXP);
  ColorPalettes.Add(TeeMsg_ThemeMacOS);
  ColorPalettes.Add(TeeMsg_ThemeWinVista);
  ColorPalettes.Add(TeeMsg_ThemeGrayScale);
  ColorPalettes.Add(TeeMsg_ThemeOpera);
  ColorPalettes.Add(TeeMsg_ThemeWarm);
  ColorPalettes.Add(TeeMsg_ThemeCool);
  ColorPalettes.Add(TeeMsg_ThemeBlack);
  ColorPalettes.Add(TeeMsg_Metro);
  ColorPalettes.Add(TeeMsg_iOSPalette);
  ColorPalettes.Add(TeeMsg_AndroidPalette);
  ColorPalettes.Add(TeeMsg_LookoutPalette);
  ColorPalettes.Add(TeeMsg_SeawashPalette);
{$endif}

finalization
{$ifdef HH_PATCH_REGISTERPALETTE}
  FreeAndNil(vColorPalettes);
{$else}
  FreeAndNil(ColorPalettes);
{$endif}
  FreeSeriesTypes;
  FreeAndNil(TeeToolTypes);
  FreeAndNil(TeeAnimationTypes);
end.


Post Reply