Page 1 of 1

TColorGrid - Palette/Color Range Difference

Posted: Mon Aug 15, 2011 12:58 pm
by 9244864
Hello !

I do not really understand the difference between Palette and ColorRange and how do the work.

First of all:
Why 2 switches UsePalette and UseColorRange?
Can I use both together. I thought one excludes the other.

The formular for color calculation of the palette I found:

Code: Select all

Procedure TSurfaceSeries.CreateDefaultPalette(NumSteps:Longint);
The problem is I want is to show positive and negative values in different colors.
( MinValue is full Green
MaxValue is full Red
0 is Red/Green mix 50%)
other values procentual Red/Green mix
)

With the palette formular I cannot do this (not so easy).
For my task the ColorRange works better.

So my idear is to use the Pallette but fill the colors with the ColorRange formular.
But I cannot find the formular for the ColorRange, because I have no source code.

Best regards
Andreas

Re: TColorGrid - Palette/Color Range Difference

Posted: Tue Aug 16, 2011 7:37 am
by yeray
Hello Andy,
Andy wrote:Why 2 switches UsePalette and UseColorRange?
Can I use both together. I thought one excludes the other.
This was probably to keep backwards compatibility. But as you've noticed, the Palette and the ColorRange can't be used together.
Andy wrote:The formular for color calculation of the palette I found:

Code: Select all

Procedure TSurfaceSeries.CreateDefaultPalette(NumSteps:Longint);
The problem is I want is to show positive and negative values in different colors.
( MinValue is full Green
MaxValue is full Red
0 is Red/Green mix 50%)
other values procentual Red/Green mix
)

With the palette formular I cannot do this (not so easy).
For my task the ColorRange works better.

So my idear is to use the Pallette but fill the colors with the ColorRange formular.
But I cannot find the formular for the ColorRange, because I have no source code.
You can use a custom palette as described here. You may also be interested in checking the examples at All Features\Welcome!\Aspect\Custom Palettes, All Features\Welcome!\Chart Styles\Extended\Contour\Palette and Color range and All Features\Welcome!\Chart Styles\Extended\Surface in the new features demo available at TeeChart's program group.
In your case, I think using a ColorRange should be enough.

Re: TColorGrid - Palette/Color Range Difference

Posted: Wed Aug 17, 2011 8:24 am
by 9244864
Hello Yeray,
thank you for your help.
Yeray wrote:
Andy wrote:Why 2 switches UsePalette and UseColorRange?
Can I use both together. I thought one excludes the other.
This was probably to keep backwards compatibility. But as you've noticed, the Palette and the ColorRange can't be used together.
Oh, now I understand it.
Yeray wrote:
Andy wrote:
The problem is I want is to show positive and negative values in different colors.
( MinValue is full Green
MaxValue is full Red
0 is Red/Green mix 50%)
other values procentual Red/Green mix
)

With the palette formular I cannot do this (not so easy).
For my task the ColorRange works better.

So my idear is to use the Pallette but fill the colors with the ColorRange formular.
But I cannot find the formular for the ColorRange, because I have no source code.
You can use a custom palette as described here. You may also be interested in checking the examples at All Features\Welcome!\Aspect\Custom Palettes, All Features\Welcome!\Chart Styles\Extended\Contour\Palette and Color range and All Features\Welcome!\Chart Styles\Extended\Surface in the new features demo available at TeeChart's program group.
In your case, I think using a ColorRange should be enough.
Now I make out my main problem:

How can I calculate gradient colors (maybe with midcolor)?
Whats the formular TChart uses for this task?
Is it a simple linear interplolation of each R/G/B value or more complex?

By Andreas

Re: TColorGrid - Palette/Color Range Difference

Posted: Wed Aug 17, 2011 11:43 am
by yeray
Hello Andreas,

Let me start for the end:
Andy wrote:Whats the formular TChart uses for this task?
Is it a simple linear interplolation of each R/G/B value or more complex?
The ColorRange is basically a linear interpolation. The MinValue is drawn in the EndColor, the MaxValue is drawn in the StartColor and the other values are drawn in a mixed color (IValueRangeInv is the inverse of the Values Range):

Code: Select all

  if UseColorRange then
  begin
    if IValueRangeInv=0 then
       result:=EndColor
    else
    begin
      tmp:=AValue-MandatoryValueList.MinValue;

      if tmp<0 then result:=EndColor
      else
      if AValue>MandatoryValueList.MaxValue then
         result:=StartColor
      else
      begin
        tmp:=tmp*IValueRangeInv;
        result:=RangePercent(Min(1.0,tmp));
      end;
    end;
  end
Andy wrote:How can I calculate gradient colors (maybe with midcolor)?
The following example seems to work fine for me here:

Code: Select all

uses TeeSurfa;

procedure TForm1.FormCreate(Sender: TObject);
var x, z, zmin, zmax: Integer;
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  zmin:=-5;
  zmax:=5;

  with Chart1.AddSeries(TColorGridSeries) as TColorGridSeries do
  begin
    IrregularGrid:=true;
    for x:=0 to 6-1 do
      for z:=zmin to zmax-1 do
        AddXYZ(x, z, z);

    StartColor:=clRed;
    EndColor:=clGreen;
  end;
end;
However, if your zmin and zmax don't have the same absolute value, using ColorRange you won't have the 50% Green 50% Red at 0.
If this is the problem, a solution would be using two ColorGrid series, both using ColorRange. You could calculate the 50% Green 50% Red color (IZeroColor) and you could use one series to draw the positive values and the other for the negative:

Code: Select all

uses TeeSurfa;

procedure TForm1.FormCreate(Sender: TObject);
var x, z, xmin, xmax, zmin, zmax,
    IStartColor, IEndColor, IZeroColor, IEndRed, IEndGreen, IEndBlue,
    IRangeRed, IRangeGreen, IRangeBlue: Integer;
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  xmin:=0;
  xmax:=10;
  zmin:=-5;
  zmax:=10;

  IStartColor:=clRed;
  IEndColor  :=clGreen;
  IEndRed    :=GetRValue(IEndColor);
  IEndGreen  :=GetGValue(IEndColor);
  IEndBlue   :=GetBValue(IEndColor);
  IRangeRed  :=Integer(GetRValue(IStartColor))-IEndRed;
  IRangeGreen:=Integer(GetGValue(IStartColor))-IEndGreen;
  IRangeBlue :=Integer(GetBValue(IStartColor))-IEndBlue;
  IZeroColor :=RGB(IEndRed  +Round(0.5*IRangeRed),
                   IEndGreen+Round(0.5*IRangeGreen),
                   IEndBlue +Round(0.5*IRangeBlue));

  with Chart1.AddSeries(TColorGridSeries) as TColorGridSeries do
  begin
    IrregularGrid:=true;
    Pen.Visible:=false;
    for x:=xmin to xmax-1 do
      for z:=zmin to 0 do
        AddXYZ(x, z, z);

    StartColor:=IZeroColor;
    EndColor:=IEndColor;
  end;

  with Chart1.AddSeries(TColorGridSeries) as TColorGridSeries do
  begin
    IrregularGrid:=true;
    Pen.Visible:=false;
    for x:=xmin to xmax-1 do
      for z:=0 to zmax-1 do
        AddXYZ(x, z, z);

    StartColor:=IStartColor;
    EndColor:=IZeroColor;
  end;
end;