Skip to content

Commit

Permalink
Merge pull request #511 from GuillaumeGomez/hex-conversion
Browse files Browse the repository at this point in the history
Fix hex conversion with alpha
  • Loading branch information
GuillaumeGomez authored May 29, 2023
2 parents d432601 + 11f464d commit ba049dd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/css_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ class CssParser {
continue;
}
if (otherElem.colorKind.startsWith('hex')) {
const alpha = otherElem.hasAlpha || elem.color[3] < 1 ? elem.color[3] * 255 : null;
const alpha = otherElem.hasAlpha || elem.color[3] < 1 ?
Math.round(elem.color[3] * 255) : null;
const values = elem.color.slice(0, 3).map(v => toHex(v));
if (alpha !== null) {
values.push(toHex(alpha));
Expand Down
4 changes: 4 additions & 0 deletions tests/test-js/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ function checkCssParser(x) {
p = new CssParser('#aaa3 #aaa');
p2 = new CssParser('rgb(224, 224, 224) rgb(224, 224, 224)');
x.assert(p2.sameFormatAs(p), '#e0e0e0ff #e0e0e0');

p = new CssParser('rgba(255, 236, 164, 0.06)');
p2 = new CssParser('#aaa');
x.assert(p.sameFormatAs(p2), '#ffeca40f');
}

function checkTuple(x) {
Expand Down

0 comments on commit ba049dd

Please sign in to comment.