对渐变应用伽马校正
可以通过将 TRUE 传递给该画笔的 PathGradientBrush::SetGammaCorrection 方法,为渐变画笔启用灰度校正。 可以通过将 FALSE 传递给 PathGradientBrush::SetGammaCorrection 方法来禁用伽玛更正。 默认情况下禁用 gemma 矫正。
以下示例创建一个线性渐变画笔,并使用该画笔填充两个矩形。 第一个矩形在没有伽玛校正的情况下填充,第二个矩形填充伽玛校正。
LinearGradientBrush linGrBrush(
Point(0, 10),
Point(200, 10),
Color(255, 255, 0, 0), // Opaque red
Color(255, 0, 0, 255)); // Opaque blue
graphics.FillRectangle(&linGrBrush, 0, 0, 200, 50);
linGrBrush.SetGammaCorrection(TRUE);
graphics.FillRectangle(&linGrBrush, 0, 60, 200, 50);
下图显示了两个填充的矩形。 上面的矩形没有进行 gamma 矫正,中间显得很暗。 下面的矩形进行了 gamma 矫正,亮度看上去更均匀。
以下示例基于star形状的路径创建路径渐变画笔。 代码使用路径渐变画笔, (默认) 禁用了灰度校正来填充路径。 然后,代码将 TRUE 传递给 PathGradientBrush::SetGammaCorrection 方法,以启用路径渐变画笔的伽玛校正。 对 Graphics::TranslateTransform 的调用设置 Graphics 对象的世界转换,以便后续对 Graphics::FillPath 的调用填充位于第一个star右侧的star。
// Put the points of a polygon in an array.
Point points[] = {Point(75, 0), Point(100, 50),
Point(150, 50), Point(112, 75),
Point(150, 150), Point(75, 100),
Point(0, 150), Point(37, 75),
Point(0, 50), Point(50, 50)};
// Use the array of points to construct a path.
GraphicsPath path;
path.AddLines(points, 10);
// Use the path to construct a path gradient brush.
PathGradientBrush pthGrBrush(&path);
// Set the color at the center of the path to red.
pthGrBrush.SetCenterColor(Color(255, 255, 0, 0));
// Set the colors of the points in the array.
Color colors[] = {Color(255, 0, 0, 0), Color(255, 0, 255, 0),
Color(255, 0, 0, 255), Color(255, 255, 255, 255),
Color(255, 0, 0, 0), Color(255, 0, 255, 0),
Color(255, 0, 0, 255), Color(255, 255, 255, 255),
Color(255, 0, 0, 0), Color(255, 0, 255, 0)};
int count = 10;
pthGrBrush.SetSurroundColors(colors, &count);
// Fill the path with the path gradient brush.
graphics.FillPath(&pthGrBrush, &path);
pthGrBrush.SetGammaCorrection(TRUE);
graphics.TranslateTransform(200.0f, 0.0f);
graphics.FillPath(&pthGrBrush, &path);
下图显示了上述代码的输出。 右侧star具有伽马校正。 请注意,左侧star没有伽玛校正,其区域显示为深色。