Skip to content

Commit

Permalink
fix(mix): return valid colors with transparent values at weight extre…
Browse files Browse the repository at this point in the history
…mes (#409)
  • Loading branch information
delucis authored Jan 2, 2022
1 parent 8acf0f4 commit ed9febe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/mix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ test('mix blue and green', () => {
);
expect(mix('blue', 'green', 1)).toMatchInlineSnapshot(`"rgba(0, 128, 0, 1)"`);
});

test('mix with transparent and weight 0%', () => {
expect(mix('transparent', 'blue', 0)).toMatchInlineSnapshot(
`"rgba(0, 0, 0, 0)"`
);
});

test('mix with transparent and weight 100%', () => {
expect(mix('red', 'rgba(255, 0, 0, 0)', 1)).toMatchInlineSnapshot(
`"rgba(255, 0, 0, 0)"`
);
});
10 changes: 6 additions & 4 deletions src/mix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ function mix(color1: string, color2: string, weight: number): string {
// The formula is copied from the original Sass implementation:
// http://sass-lang.com/documentation/Sass/Script/Functions.html#mix-instance_method
const alphaDelta = a2 - a1;
const x = weight * 2 - 1;
const y = x * alphaDelta === -1 ? x : x + alphaDelta;
const z = 1 + x * alphaDelta;
const weight2 = (y / z + 1) / 2.0;
const normalizedWeight = weight * 2 - 1;
const combinedWeight =
normalizedWeight * alphaDelta === -1
? normalizedWeight
: normalizedWeight + alphaDelta / (1 + normalizedWeight * alphaDelta);
const weight2 = (combinedWeight + 1) / 2;
const weight1 = 1 - weight2;

const r = (r1 * weight1 + r2 * weight2) * 255;
Expand Down

0 comments on commit ed9febe

Please sign in to comment.