Skip to content

Remove memory allocation for interpolateColors following FIXME comment #1077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions packages/core/src/internal/data-grid/color-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,25 @@ export function interpolateColors(leftColor: string, rightColor: string, val: nu
if (val >= 1) return rightColor;

// Parse to rgba returns straight alpha colors, for interpolation we want pre-multiplied alpha
// FIXME: This can be faster if instead of makign an array we just use variables. No memory allocation.
const left = [...parseToRgba(leftColor)];
left[0] = left[0] * left[3];
left[1] = left[1] * left[3];
left[2] = left[2] * left[3];
const right = [...parseToRgba(rightColor)];
right[0] = right[0] * right[3];
right[1] = right[1] * right[3];
right[2] = right[2] * right[3];
const [lr, lg, lb, la] = parseToRgba(leftColor);
const [rr, rg, rb, ra] = parseToRgba(rightColor);

const leftR = lr * la;
const leftG = lg * la;
const leftB = lb * la;

const rightR = rr * ra;
const rightG = rg * ra;
const rightB = rb * ra;

const hScaler = val;
const nScaler = 1 - val;

const a = left[3] * nScaler + right[3] * hScaler;
const a = la * nScaler + ra * hScaler;
// now we need to divide the alpha back out to get linear alpha back for the final result
const r = Math.floor((left[0] * nScaler + right[0] * hScaler) / a);
const g = Math.floor((left[1] * nScaler + right[1] * hScaler) / a);
const b = Math.floor((left[2] * nScaler + right[2] * hScaler) / a);
const r = Math.floor((leftR * nScaler + rightR * hScaler) / a);
const g = Math.floor((leftG * nScaler + rightG * hScaler) / a);
const b = Math.floor((leftB * nScaler + rightB * hScaler) / a);
return `rgba(${r}, ${g}, ${b}, ${a})`;
}

Expand Down