-1

I am trying to build a polygon using only two colors for all vertex. But the gdiplus library automatically inserts an white center color blending all the figure. I would like to disable the center color, instead workarounding it by using SetCenterColor() available in PathGradientBrush class. Shifting the default position using SetCenterPoint() to a far way position is very inelegant. Is that possible?

Thanks

enter image description here

A sample follows:

CMyGDIPlus gdi(this);  // use your class instead
using namespace Gdiplus;
Graphics & graphics = gdi.GetGraphics();
graphics.SetSmoothingMode(SmoothingModeNone);

Gdiplus::Rect gRect;
graphics.GetVisibleClipBounds(&gRect);

int i;
int colorSize = 4;
GraphicsPath path;
Point arrPoint[4];
Color arrColors[4];

arrPoint[0].X = gRect.GetLeft();
arrPoint[0].Y = gRect.GetTop();

arrPoint[1].X = gRect.GetRight();
arrPoint[1].Y = gRect.GetTop()+100;

arrPoint[2].X = gRect.GetRight();
arrPoint[2].Y = gRect.GetBottom();

arrPoint[3].X = gRect.GetLeft();
arrPoint[3].Y = gRect.GetBottom()-100;

for(i = 0; i < colorSize; i++)
{
    if(i < 2)
        arrColors[i].SetFromCOLORREF(RGB(0, 128, 0));   // green
    else
        arrColors[i].SetFromCOLORREF(RGB(0, 0, 192));   // blue
}

path.AddLines(arrPoint, 4);

PathGradientBrush pathBrush(&path);

pathBrush.SetSurroundColors(arrColors, &colorSize);
pathBrush.SetGammaCorrection(TRUE);

graphics.FillPath(&pathBrush, &path);
  • Please be specific about your issue, provide a [mcve], and properly tag the question. It doesn't appear to be related to MFC at all. If this is indeed about GDI+ you may want to add the [tag:winapi] tag. – IInspectable Dec 28 '19 at 15:36
  • I think I am already being specific enough since that I don't want any 'center color' set at all, whatever my drawing is about; I need the gradient color just going from the supplied vertex colors for each other. The API don't give this option, I saw a lot of samples with programmers workarounding this situation, for example: https://stackoverflow.com/questions/33415696/n-point-gradient-brush-polygon-fill – Paulo Eduardo Pilon Dec 28 '19 at 16:33
  • No similar case of disabling the center point was found. The example you provided may be the most effective way, by calculating the CenterColor and setting the CenterPoint.. – Strive Sun Dec 30 '19 at 07:51
  • strive-sun-msft. Yes, but when I calculate the average color of vertical line joining points (see figure) it is not exactly match the actual blended color existing in the middle of the path. And we have the gamma correction set to "on"... – Paulo Eduardo Pilon Dec 30 '19 at 22:39

1 Answers1

0

You only need to calculate the color value of the center point.

To average the values (e.g. (r1 + r2) / 2). This works better for lightening/darkening colors and creating gradients.

Refer: Algorithm for Additive Color Mixing for RGB Values

Add : pathBrush.SetCenterColor(Color(0, 128*0.5, 192*0.5));

Debug:

enter image description here

Strive Sun
  • 5,988
  • 1
  • 9
  • 26