commit 8c787c16481dc2fe715e802b88ca68ddece84930 Author: Anri Kennel Date: Fri Sep 22 10:45:17 2023 +0200 Ajouter svgObsidianFix diff --git a/svgObsidianFix b/svgObsidianFix new file mode 100644 index 0000000..9cbc3f4 --- /dev/null +++ b/svgObsidianFix @@ -0,0 +1,52 @@ +window.addEventListener("load", () => main()); + +const main = () => { + for (const item of document.getElementsByTagName("img")) { + if (!item.src.startsWith("data:image/svg+xml;base64,")) { + // Exclude image who aren't SVG and base64 encoded + break; + } + + /** Convert to grayscale */ + const colorToGrayscale = (color) => { + return 0.3 * color.r + 0.59 * color.g + 0.11 * color.b; + }; + + /** Extract color using canvas2d */ + const extractColors = (image) => { + const canvas = document.createElement("canvas"); + canvas.width = image.width; + canvas.height = image.height; + const ctx = canvas.getContext("2d"); + ctx.drawImage(image, 0, 0); + const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); + const pixelData = imageData.data; + + const colors = []; + for (let i = 0; i < pixelData.length; i += 4) { + if (pixelData[i + 3] > 0) { + colors.push({ + r: pixelData[i], + g: pixelData[i + 1], + b: pixelData[i + 2], + }); + } + } + + return colors; + }; + + // Extract colors + const colors = extractColors(item); + + // Calculate the average grayscale value + const grayscaleValues = colors.map(colorToGrayscale); + const totalGrayscale = grayscaleValues.reduce((acc, val) => acc + val, 0); + const averageGrayscale = totalGrayscale / grayscaleValues.length; + + console.log(averageGrayscale); + if (averageGrayscale < 128) { + item.style = "filter: invert(1);"; + } + } +};