Skip to content

Commit

Permalink
Correct rgba() parsing to use float alpha channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed May 20, 2022
1 parent 41fdb96 commit ddd43e0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/common/Color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ describe('Color', () => {
});
it('should convert the rgba() format to an IColor', () => {
assert.deepEqual(css.toColor('rgba(0, 0, 0, 0)'), { css: 'rgba(0, 0, 0, 0)', rgba: 0x00000000 });
assert.deepEqual(css.toColor('rgba(80, 0, 0, 80)'), { css: 'rgba(80, 0, 0, 80)', rgba: 0x50000050 });
assert.deepEqual(css.toColor('rgba(0, 80, 0, 80)'), { css: 'rgba(0, 80, 0, 80)', rgba: 0x00500050 });
assert.deepEqual(css.toColor('rgba(0, 0, 80, 80)'), { css: 'rgba(0, 0, 80, 80)', rgba: 0x00005050 });
assert.deepEqual(css.toColor('rgba(255, 255, 255, 255)'), { css: 'rgba(255, 255, 255, 255)', rgba: 0xffffffff });
assert.deepEqual(css.toColor('rgba(80, 0, 0, 0.5)'), { css: 'rgba(80, 0, 0, 0.5)', rgba: 0x50000080 });
assert.deepEqual(css.toColor('rgba(0, 80, 0, 0.5)'), { css: 'rgba(0, 80, 0, 0.5)', rgba: 0x00500080 });
assert.deepEqual(css.toColor('rgba(0, 0, 80, 0.5)'), { css: 'rgba(0, 0, 80, 0.5)', rgba: 0x00005080 });
assert.deepEqual(css.toColor('rgba(255, 255, 255, 1)'), { css: 'rgba(255, 255, 255, 1)', rgba: 0xffffffff });
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/common/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ export namespace css {
};
}
}
const rgbaMatch = css.match(/rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(,\s*(\d{1,3})\s*)?\)/);
const rgbaMatch = css.match(/rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(,\s*(0|1|\d?\.(\d+))\s*)?\)/);
if (rgbaMatch) { // rgb() or rgba()
return {
css,
rgba: channels.toRgba(
parseInt(rgbaMatch[1]), // r
parseInt(rgbaMatch[2]), // g
parseInt(rgbaMatch[3]), // b
rgbaMatch[5] === undefined ? 0xFF : parseInt(rgbaMatch[5]) // a?
Math.round((rgbaMatch[5] === undefined ? 1 : parseFloat(rgbaMatch[5])) * 0xFF) // a?
)
};
}
Expand Down

0 comments on commit ddd43e0

Please sign in to comment.