-
Notifications
You must be signed in to change notification settings - Fork 452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle SkColor creation from rgba tuple #2144
Open
louix
wants to merge
8
commits into
Shopify:main
Choose a base branch
from
louix:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
354dab9
Handle SkColor creation from rgba tuple
louix eb38acb
Merge branch 'main' into main
wcandillon 8a956be
Add tests
wcandillon efc970f
Merge pull request #1 from Shopify/louix/main
louix cad5a03
Merge branch 'main' into main
wcandillon 017d46f
Fix Float32Array check
louix 4c45ea1
Add offscreen canvas test
louix 7e747d6
Add exhaustive web case (to match C++)
louix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { surface } from "../setup"; | ||
|
||
describe("Colors", () => { | ||
it("should support int, string, array, and Float32Array", async () => { | ||
const result = await surface.eval((Skia) => { | ||
const areEqualFloat32Arrays = (...arrays: Float32Array[]) => { | ||
// Check if all arrays have the same length | ||
const allSameLength = arrays.every( | ||
(array) => array.length === arrays[0].length | ||
); | ||
if (!allSameLength) { | ||
return false; | ||
} | ||
|
||
// Compare elements across all arrays for each index | ||
for (let i = 0; i < arrays[0].length; i++) { | ||
if (!arrays.every((array) => array[i] === arrays[0][i])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const c1 = Skia.Color("cyan"); | ||
const c2 = Skia.Color([0, 1, 1, 1]); | ||
const c3 = Skia.Color(0xff00ffff); | ||
const c4 = Skia.Color(Float32Array.of(0, 1, 1, 1)); | ||
|
||
const r = areEqualFloat32Arrays(c1, c2, c3, c4); | ||
if (!r) { | ||
console.log({ c1, c2, c3, c4 }); | ||
} | ||
return r; | ||
}); | ||
expect(result).toBe(true); | ||
}); | ||
it("should render the same color regardless of constructor input types", async () => { | ||
// given (different inputs representing the same color | ||
const colors = [ | ||
{ kind: "string" as const, value: "red" }, | ||
{ kind: "float32Array" as const, value: [1, 0, 0, 1] }, | ||
{ kind: "number" as const, value: 0xffff0000 }, | ||
{ kind: "array" as const, value: [1, 0, 0, 1] }, | ||
]; | ||
|
||
// when (for each input we draw a colored canvas and encode it to bytes) | ||
const buffers = await Promise.all( | ||
colors.map((color) => | ||
surface | ||
.drawOffscreen( | ||
(Skia, canvas, ctx) => { | ||
const c = | ||
ctx.color.kind === "float32Array" | ||
? // we cannot pass in a Float32Array via ctx, need to construct it inside | ||
new Float32Array(ctx.color.value) | ||
: ctx.color.value; | ||
canvas.drawColor(Skia.Color(c)); | ||
}, | ||
{ color } | ||
) | ||
.then((image) => image.encodeToBytes()) | ||
) | ||
); | ||
|
||
// then (expect the encoded bytes are equal) | ||
for (let i = 1; i < buffers.length; i++) { | ||
const prev = buffers[i - 1]; | ||
const curr = buffers[i]; | ||
expect(prev.length).toBe(curr.length); | ||
for (let j = 0; j < prev.length; j++) { | ||
expect(prev[j]).toBe(curr[j]); | ||
} | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes that's fair