Greasy Fork 还支持 简体中文。

WIRED Image Converter

Resimleri PixelPlace paletine dönüştüren ve Dithering yapan motor.

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://updategf.qytechs.cn/scripts/562314/1732640/WIRED%20Image%20Converter.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         WIRED Image Converter
// @version      1.0
// @description  Resimleri PixelPlace paletine dönüştüren ve Dithering yapan motor.
// @author       WIRED
// @license      MIT
// ==/UserScript==

class WiredImageConverter {
    static getClosestColor(r, g, b, palette) {
        let closestColor = null;
        let closestDistance = Number.MAX_VALUE;
        let closestIndex = -1;

        for (let i = 0; i < palette.colors.length; i++) {
            let bigint = palette.colors[i];
            let p_r = (bigint >> 16) & 255;
            let p_g = (bigint >> 8) & 255;
            let p_g_raw = (bigint >> 8) & 255;
            let p_b = bigint & 255;

            let distance = Math.sqrt(
                Math.pow(r - p_r, 2) +
                Math.pow(g - p_r, 2) +
                Math.pow(b - p_b, 2)
            );

            if (distance < closestDistance) {
                closestDistance = distance;
                closestColor = {r: p_r, g: p_g_raw, b: p_b};
                closestIndex = palette.indexes[i];
            }
        }
        return {color: closestColor, index: closestIndex};
    }

    static applyDither(img_data, w, h, palette) {
        const dithered = new Uint8ClampedArray(img_data.data.length);
        for (let y = 0; y < h; y++) {
            for (let x = 0; x < w; x++) {
                const idx = (y * w + x) * 4;
                const r = img_data.data[idx];
                const g = img_data.data[idx + 1];
                const b = img_data.data[idx + 2];
                const a = img_data.data[idx + 3];

                dithered[idx + 3] = a;
                if (a < 128) continue;

                const closest = this.getClosestColor(r, g, b, palette);
                if (closest.color) {
                    dithered[idx] = closest.color.r;
                    dithered[idx + 1] = closest.color.g;
                    dithered[idx + 2] = closest.color.b;
                }
            }
        }
        return new ImageData(dithered, w, h);
    }
}