Colored Shade

Make connected shaded cell groups colorful to check connectivity.

当前为 2024-06-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         Colored Shade
// @version      24.6.19.1
// @description  Make connected shaded cell groups colorful to check connectivity.
// @author       Leaving Leaves
// @match        https://puzz.link/p*
// @match        https://pzplus.tck.mn/p*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=puzz.link
// @license      GPL
// @namespace    https://greasyfork.org/users/1192854
// ==/UserScript==

'use strict';
let done = false;
let main = function () {
    if (done) { return; }
    done = true;
    ui.puzzle.config.set("disptype_yajilin", 2);    // better vision of Yajilin
    if (!document.querySelector(".config[data-config='irowake']").getElementsByTagName("input")[0].checked) {
        document.querySelector(".config[data-config='irowake']").getElementsByTagName("input")[0].click();
    }
    ui.puzzle.config.set("irowake", 1);

    const GENRENAME = ui.puzzle.info.en;
    const checklist = ui.puzzle.checker.checklist_normal;
    const isOrthoConnected = checklist.some(f => f.name === "checkConnectShade");
    const isDiagConnected = checklist.some(f => f.name === "checkAdjacentShadeCell") &&
        checklist.some(f => f.name === "checkConnectUnshadeRB" || f.name === "checkConnectUnshade");
    ui.puzzle.config.set("irowake", true);
    if (GENRENAME === "Sukoro") { // better vision of connectivity
        ui.puzzle.painter.getBGCellColor = ui.puzzle.pzpr.common.Graphic.prototype.getBGCellColor_qsub2;
    }

    if (isDiagConnected) {
        // for sblkdiag, now it's not often used and may cause confusion
        // if (ui.puzzle.board.sblkdiagmgr) {
        //     ui.puzzle.board.sblkdiagmgr.coloring = true;
        //     ui.puzzle.painter.getShadedCellColor = my_getShadedCellColor;
        // } else {
        let instance = new ui.puzzle.board.klass.AreaShadeGraph();
        instance.enabled = true;
        let board = ui.puzzle.board;
        board.infolist.push(instance);
        board.sblkmgr = instance;
        board.sblkmgr.getSideObjByNodeObj = my_getSideObjByNodeObj;
        board.rebuildInfo();
        // }
    }
    if (isOrthoConnected || isDiagConnected) {
        ui.puzzle.config.list.irowakeblk.val = true;
        ui.puzzle.painter.irowakeblk = true;
        ui.puzzle.board.sblkmgr.coloring = true;
        ui.puzzle.painter.labToRgbStr = (l, a, b) => my_labToRgbStr([l * 2 / 3, a, b]);
        // make it visible
        document.querySelector(".config[data-config='irowakeblk']").style = '';
        document.querySelector(".config[data-config='irowakeblk']").getElementsByTagName("input")[0].click();
    }
};
ui.puzzle.on('ready', main, false);
let initTimer = setInterval(() => {
    if (done) {
        clearInterval(initTimer);
        return;
    }
    main();
}, 1000);

function my_getSideObjByNodeObj(c) {
    let bx = c.bx;
    let by = c.by;
    let list = [];
    let board = ui.puzzle.board;
    for (let dx = -2; dx <= 2; dx += 2) {
        for (let dy = -2; dy <= 2; dy += 2) {
            if (dx !== 0 || dy !== 0) {
                list.push(board.getc(bx + dx, by + dy));
            }
        }
    }
    if (list.some(c => c.isnull)) {
        list = list.filter(c => !c.isnull);
        let fn = cc => { if (!list.includes(cc)) list.push(cc); };
        for (let i = board.minbx + 1; i <= board.maxbx - 1; i += 2) {
            fn(board.getc(i, board.minby + 1));
            fn(board.getc(i, board.maxby - 1));
        }
        for (let j = board.minby + 1; j <= board.maxby - 1; j += 2) {
            fn(board.getc(board.minbx + 1, j));
            fn(board.getc(board.maxbx - 1, j));
        }
    }
    list = list.filter(c => board.sblkmgr.isnodevalid(c));
    return list;
}

function my_getShadedCellColor(c) {
    if (c.qans !== 1)
        return null;
    var t = c.error || c.qinfo;
    if (t === 1) { return ui.puzzle.painter.errcolor1; }
    if (t === 2) { return ui.puzzle.painter.errcolor2; }
    if (c.trial) { return ui.puzzle.painter.trialcolor; }
    // change from sblk to blkdiag
    if (ui.puzzle.execConfig("irowakeblk")) { return c.blkdiag.color; }
    return ui.puzzle.painter.shadecolor;
}

function my_labToRgbStr(lab) {
    let y = (lab[0] + 16) / 116,
        x = lab[1] / 500 + y,
        z = y - lab[2] / 200,
        r, g, b;

    x = 0.95047 * ((x * x * x > 0.008856) ? x * x * x : (x - 16 / 116) / 7.787);
    y = 1.00000 * ((y * y * y > 0.008856) ? y * y * y : (y - 16 / 116) / 7.787);
    z = 1.08883 * ((z * z * z > 0.008856) ? z * z * z : (z - 16 / 116) / 7.787);

    r = x * 3.2406 + y * -1.5372 + z * -0.4986;
    g = x * -0.9689 + y * 1.8758 + z * 0.0415;
    b = x * 0.0557 + y * -0.2040 + z * 1.0570;

    r = (r > 0.0031308) ? (1.055 * Math.pow(r, 1 / 2.4) - 0.055) : 12.92 * r;
    g = (g > 0.0031308) ? (1.055 * Math.pow(g, 1 / 2.4) - 0.055) : 12.92 * g;
    b = (b > 0.0031308) ? (1.055 * Math.pow(b, 1 / 2.4) - 0.055) : 12.92 * b;

    return "rgb(" + [Math.max(0, Math.min(1, r)) * 255,
    Math.max(0, Math.min(1, g)) * 255,
    Math.max(0, Math.min(1, b)) * 255].join(',') + ")";
}