Skip to content

Commit e4fddcc

Browse files
committed
Improve 0xrrggbbaa parsing
1 parent ce1bc6a commit e4fddcc

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ describe("Geometry", () => {
2424
test("parse #hex with opacity", () => {
2525
const { Skia } = importSkia();
2626

27-
const color = Skia.Color("#80808080");
27+
let color = Skia.Color("#80808080");
2828
expect(color[0]).toBeCloseTo(0.5);
2929
expect(color[1]).toBeCloseTo(0.5);
3030
expect(color[2]).toBeCloseTo(0.5);
3131
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);
3238
});
3339
});

package/src/skia/web/JsiSkColor.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,24 @@ const parseCSSColor = (cssStr: string) => {
242242
} // Covers NaN.
243243
return [(iv & 0xff0000) >> 16, (iv & 0xff00) >> 8, iv & 0xff, 1];
244244
} 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);
245+
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
246+
if (!(iv >= 0 && iv <= 0xffffffff)) {
247+
return null; // Covers NaN.
248+
}
247249
return [
250+
((iv & 0xff000000) >> 24) & 0xff,
248251
(iv & 0xff0000) >> 16,
249252
(iv & 0xff00) >> 8,
250-
iv & 0xff,
251-
opacity / 255,
253+
(iv & 0xff) / 255,
252254
];
255+
/*
256+
if (!(iv >= 0 && iv <= 0xffffffff))
257+
return {}; // Covers NaN.
258+
return {static_cast<uint8_t>(((iv & 0xff000000) >> 24) & 0xff),
259+
static_cast<uint8_t>((iv & 0x00ff0000) >> 16),
260+
static_cast<uint8_t>((iv & 0x0000ff00) >> 8),
261+
static_cast<uint8_t>((iv & 0x000000ff)) / 255.0f};
262+
*/
253263
}
254264

255265
return null;

0 commit comments

Comments
 (0)