blob: fa035abd5fe634118239ca57907c9ffc508f36f2 [file] [log] [blame]
Kevin Lubick217056c2018-09-20 17:39:31 -04001
Kevin Lubick1a05fce2018-11-20 12:51:16 -05002// Adds any extra JS functions/helpers we want to add to CanvasKit.
Kevin Lubick217056c2018-09-20 17:39:31 -04003// Wrapped in a function to avoid leaking global variables.
4(function(CanvasKit){
5
6 function clamp(c) {
7 return Math.round(Math.max(0, Math.min(c || 0, 255)));
8 }
9
10 // Colors are just a 32 bit number with 8 bits each of a, r, g, b
11 // The API is the same as CSS's representation of color rgba(), that is
12 // r,g,b are 0-255, and a is 0.0 to 1.0.
Kevin Lubick1a05fce2018-11-20 12:51:16 -050013 // if a is omitted, it will be assumed to be 1.0
Kevin Lubick217056c2018-09-20 17:39:31 -040014 CanvasKit.Color = function(r, g, b, a) {
Kevin Lubick1a05fce2018-11-20 12:51:16 -050015 if (a === undefined) {
16 a = 1;
17 }
Kevin Lubick217056c2018-09-20 17:39:31 -040018 return (clamp(a*255) << 24) | (clamp(r) << 16) | (clamp(g) << 8) | (clamp(b) << 0);
19 }
20}(Module)); // When this file is loaded in, the high level object is "Module";