Skip to content

Commit ce1bc6a

Browse files
committed
Fix color parsing on RN Web
1 parent 90db9fc commit ce1bc6a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,22 @@ 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+
const 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+
});
1533
});

package/src/skia/web/JsiSkColor.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ 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, 6), 16); // TODO(deanm): Stricter parsing.
246+
const opacity = parseInt(str.substr(7), 16);
247+
return [
248+
(iv & 0xff0000) >> 16,
249+
(iv & 0xff00) >> 8,
250+
iv & 0xff,
251+
opacity / 255,
252+
];
244253
}
245254

246255
return null;

0 commit comments

Comments
 (0)