linearRgbInRect method
Returns linear RGB for a rectangular region.
Coordinates are given in percent and must be between 0 and 1. Throws ArgumentError if the coordinates are out of range. ColorTriplet by default is in linear RGB color space. Convert to RGB before using the color. See ColorTripletExtensions.toRgb.
Implementation
ColorTriplet linearRgbInRect(
Point<double> topLeftCorner,
Point<double> bottomRightCorner,
) {
if (topLeftCorner.x < 0.0 ||
topLeftCorner.x > 1.0 ||
topLeftCorner.y < 0.0 ||
topLeftCorner.y > 1.0) {
throw ArgumentError('Coordinates must be between [0, 1].');
}
if (bottomRightCorner.x < 0.0 ||
bottomRightCorner.x > 1.0 ||
bottomRightCorner.y < 0.0 ||
bottomRightCorner.y > 1.0) {
throw ArgumentError('Coordinates must be between [0, 1].');
}
if (topLeftCorner.x >= bottomRightCorner.x ||
topLeftCorner.y >= bottomRightCorner.y) {
throw ArgumentError('The bottom-right corner must be right of '
'and below to the top-left corner!');
}
var sum = ColorTriplet(0, 0, 0);
for (var j = 0; j < numCompY; j++) {
for (var i = 0; i < numCompX; i++) {
final horizontalAverage = i == 0
? 1.0
: ((sin(pi * i * bottomRightCorner.x) -
sin(pi * i * topLeftCorner.x)) /
(i * pi * (bottomRightCorner.x - topLeftCorner.x)));
final verticalAverage = j == 0
? 1.0
: ((sin(pi * j * bottomRightCorner.y) -
sin(pi * j * topLeftCorner.y)) /
(j * pi * (bottomRightCorner.y - topLeftCorner.y)));
sum += components[j][i] * horizontalAverage * verticalAverage;
}
}
return sum;
}