| 29 | // True when nearly every visible pixel is near-white, so the logo would vanish |
| 30 | // on a white plate. |
| 31 | const isLightLogo = (image: HTMLImageElement): boolean => { |
| 32 | const canvas = document.createElement('canvas'); |
| 33 | canvas.width = LOGO_SAMPLE_SIZE; |
| 34 | canvas.height = LOGO_SAMPLE_SIZE; |
| 35 | const context = canvas.getContext('2d'); |
| 36 | |
| 37 | if (!context) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | context.drawImage(image, 0, 0, LOGO_SAMPLE_SIZE, LOGO_SAMPLE_SIZE); |
| 42 | const { data } = context.getImageData( |
| 43 | 0, |
| 44 | 0, |
| 45 | LOGO_SAMPLE_SIZE, |
| 46 | LOGO_SAMPLE_SIZE, |
| 47 | ); |
| 48 | let visible = 0; |
| 49 | let light = 0; |
| 50 | |
| 51 | for (let i = 0; i < data.length; i += 4) { |
| 52 | if (data[i + 3] >= 128) { |
| 53 | visible += 1; |
| 54 | |
| 55 | if (Math.min(data[i], data[i + 1], data[i + 2]) >= 224) { |
| 56 | light += 1; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return visible > 0 && light / visible >= 0.95; |
| 62 | }; |
| 63 | |
| 64 | // A tool's real logo, from the dataset when it has one and from the API's icon |
| 65 | // service otherwise, falling back to the initial only when both are missing or |