mwg API
    Preparing search index...

    Function paletteRangeMapping

    • Builds a PaletteMapping that recolours every entry of reference into a shade of the min -> mid -> max gradient, the shape a team colour or any other [color_range]-style remap is defined by. reference is the exact list of colours the art was painted with, so pixels outside it - anti-aliased edges, shading, outlines - are left alone by an 'exact' remap, which is the mode this pairs with.

      The reference's first entry is its anchor, and it is the one colour whose shade is fixed by the range alone: it becomes mid. Every other entry is placed by its own brightness rather than by its position, so the rest of the list needs no order - a darker entry blends towards min and a brighter one towards max, each in proportion to how its average compares with the anchor's. That is what lets one mapping reproduce art as shaded as the reference it was painted from, and what the first entry has to be: the base shade the art was built around.

      Brightness and the blend are computed the way the engine they mirror does, a floored integer average and a truncated blend, which is what makes the shades equal the engine's byte for byte rather than merely close. The engine's own black-and-white special cases fall out of the two branches here rather than being spelled out: an anchor with an average of 0 has no ratio to divide by, so every entry takes the brighter branch and mid is the top of the scale instead of the middle of it, and a white anchor is at least as bright as everything, so every entry takes the darker one.

      Parameters

      Returns PaletteMapping

      import { paletteRangeMapping, recolorTexture } from '@datamoc/mw_games/two-d/render';
      import type { Texture2D } from '@datamoc/mw_games/two-d/render';

      declare const texture: Texture2D;
      declare const magentaReference: readonly number[]; // the exact colours the art was painted with

      const redTeam = paletteRangeMapping(magentaReference, { min: 0x000000, mid: 0xff0000, max: 0xffffff });
      const recolored = recolorTexture(texture, redTeam); // 'exact': only those pixels are repainted