Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1 | |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame^] | 2 | // Adds any extra JS functions/helpers we want to add to CanvasKit. |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 3 | // 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 Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame^] | 13 | // if a is omitted, it will be assumed to be 1.0 |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 14 | CanvasKit.Color = function(r, g, b, a) { |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame^] | 15 | if (a === undefined) { |
| 16 | a = 1; |
| 17 | } |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 18 | 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"; |