createImage method
Generate a ARGB image based on normalized gradient. The width and height of the image are specified as input parameters. The output is a Flutter Image widget.
Implementation
Image createImage(int width, int height) {
// prepare bitmap structure
GuiBitmapBuffer gbmp = GuiBitmapBuffer(
width: width,
height: height,
bitsPerPixel: GuiBitmapBuffer.bitsPerPixelArgb);
Uint8List data = gbmp.imageData;
int offset = 0;
// check if width and height are valid
if (width < 1) width = 1;
if (height < 1) height = 1;
// set start of stop idx to stop 0.0
int curStopIdx = mapStop0Idx;
// get begin gradient
double begYStop = mapStops[curStopIdx];
List<GuiGradientColor> begGrad = colormap[begYStop]!;
// get end gradient
curStopIdx++;
double endYStop = mapStops[curStopIdx];
List<GuiGradientColor> endGrad = colormap[endYStop]!;
double yStopIncrement = (height > 1) ? (1.0 / (height - 1)) : 1.0;
double xStopIncrement = (width > 1) ? (1.0 / (width - 1)) : 1.0;
// For each row
double yStop = 0;
for (int y = 0; y < height; y++, yStop += yStopIncrement) {
// get offset for row
offset = gbmp.imageRowOffset(y);
// get gradient stop
while (yStop > endYStop) {
begYStop = endYStop;
begGrad = endGrad;
curStopIdx++;
if (curStopIdx >= mapStops.length) break;
endYStop = mapStops[curStopIdx];
endGrad = colormap[endYStop]!;
}
List<GuiGradientColor> curGrad =
GuiNormalizeGradient.getGradientInBetween(
yStop, begGrad, begYStop, endGrad, endYStop);
// get color range
int curGgcIdx = gradientStop0Idx;
GuiGradientColor begGgc = curGrad[curGgcIdx];
curGgcIdx++;
GuiGradientColor endGgc = curGrad[curGgcIdx];
double xStop = 0.0;
for (int x = 0; x < width; x++, xStop += xStopIncrement) {
while (xStop > endGgc.stop) {
begGgc = endGgc;
curGgcIdx++;
if (curGgcIdx >= curGrad.length) break;
endGgc = curGrad[curGgcIdx];
}
// get color for offset at (x,y)
Color clr =
GuiNormalizeGradient.getColorInBetween(xStop, begGgc, endGgc);
// PixelFormat.rgba8888
data[offset] = clr.blue;
data[offset + 1] = clr.green;
data[offset + 2] = clr.red;
data[offset + 3] = clr.alpha;
// increment offset
offset += gbmp.bytesPerPixel;
}
}
// convert data into image
return gbmp.toImage();
}