Skip to content

Commit 56d7ff0

Browse files
authored
Merge pull request #1085 from Shopify/fix/color-parsing
Fix color parsing on RN Web
2 parents e94320d + 3f5cbaf commit 56d7ff0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

package/src/skia/__tests__/Geometry.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,28 @@ describe("Geometry", () => {
1212
expect(result.width).toBe(140);
1313
expect(result.height).toBe(60);
1414
});
15+
16+
test("parse #hex without opacity", () => {
17+
const { Skia } = importSkia();
18+
19+
const color = Skia.Color("#808080");
20+
expect(color[0]).toBeCloseTo(0.5);
21+
expect(color[1]).toBeCloseTo(0.5);
22+
expect(color[2]).toBeCloseTo(0.5);
23+
});
24+
test("parse #hex with opacity", () => {
25+
const { Skia } = importSkia();
26+
27+
let color = Skia.Color("#80808080");
28+
expect(color[0]).toBeCloseTo(0.5);
29+
expect(color[1]).toBeCloseTo(0.5);
30+
expect(color[2]).toBeCloseTo(0.5);
31+
expect(color[3]).toBeCloseTo(0.5);
32+
33+
color = Skia.Color("#ff800080");
34+
expect(color[0]).toBeCloseTo(1);
35+
expect(color[1]).toBeCloseTo(0.5);
36+
expect(color[2]).toBeCloseTo(0);
37+
expect(color[3]).toBeCloseTo(0.5);
38+
});
1539
});

package/src/skia/web/JsiSkColor.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ const parseCSSColor = (cssStr: string) => {
241241
return null;
242242
} // Covers NaN.
243243
return [(iv & 0xff0000) >> 16, (iv & 0xff00) >> 8, iv & 0xff, 1];
244+
} else if (str.length === 9) {
245+
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
246+
if (!(iv >= 0 && iv <= 0xffffffff)) {
247+
return null; // Covers NaN.
248+
}
249+
return [
250+
((iv & 0xff000000) >> 24) & 0xff,
251+
(iv & 0xff0000) >> 16,
252+
(iv & 0xff00) >> 8,
253+
(iv & 0xff) / 255,
254+
];
244255
}
245256

246257
return null;

0 commit comments

Comments
 (0)