in packages/utilities/fast-colors/src/color-scale.ts [86:141]
public trim(
lowerBound: number,
upperBound: number,
interpolationMode: ColorInterpolationSpace = ColorInterpolationSpace.RGB
): ColorScale {
if (lowerBound < 0 || upperBound > 1 || upperBound < lowerBound) {
throw new Error("Invalid bounds");
}
if (lowerBound === upperBound) {
return new ColorScale([
{ color: this.getColor(lowerBound, interpolationMode), position: 0 },
]);
}
const containedStops: ColorScaleStop[] = [];
for (let i: number = 0; i < this.stops.length; i++) {
if (
this.stops[i].position >= lowerBound &&
this.stops[i].position <= upperBound
) {
containedStops.push(this.stops[i]);
}
}
if (containedStops.length === 0) {
return new ColorScale([
{ color: this.getColor(lowerBound), position: lowerBound },
{ color: this.getColor(upperBound), position: upperBound },
]);
}
if (containedStops[0].position !== lowerBound) {
containedStops.unshift({
color: this.getColor(lowerBound),
position: lowerBound,
});
}
if (containedStops[containedStops.length - 1].position !== upperBound) {
containedStops.push({
color: this.getColor(upperBound),
position: upperBound,
});
}
const range: number = upperBound - lowerBound;
const finalStops: ColorScaleStop[] = new Array(containedStops.length);
for (let i: number = 0; i < containedStops.length; i++) {
finalStops[i] = {
color: containedStops[i].color,
position: (containedStops[i].position - lowerBound) / range,
};
}
return new ColorScale(finalStops);
}