Introduction
- Creates a linear gradient between two color values and stores it in a mat with a single row
Code
public static unsafe Mat CreateLinearGradient(int width, Color lowColor, Color highColor)
{
Mat gradient = new Mat(1, width, DepthType.Cv8U, 3);
byte* kGradient = (byte*)gradient.DataPointer.ToPointer();
byte rStart = lowColor.R;
byte gStart = lowColor.G;
byte bStart = lowColor.B;
float rDelta = (highColor.R - lowColor.R) / (width - 1f);
float gDelta = (highColor.G - lowColor.G) / (width - 1f);
float bDelta = (highColor.B - lowColor.B) / (width - 1f);
for (int i = 0; i < width; i++)
{
*kGradient++ = (byte)(bStart + bDelta * i);
*kGradient++ = (byte)(gStart + gDelta * i);
*kGradient++ = (byte)(rStart + rDelta * i);
}
return gradient;
}
Usage
static void Main(string[] args)
{
Mat gradient = CreateLinearGradient(256, Color.Red, Color.Blue);
CvInvoke.Imshow("test", gradient);
CvInvoke.WaitKey(0);
}
Output