hue-map
Version:
Interpolate gradients based on common palettes
110 lines (109 loc) • 8.65 kB
JavaScript
var f = Object.defineProperty;
var m = (t, r, e) => r in t ? f(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
var h = (t, r, e) => (m(t, typeof r != "symbol" ? r + "" : r, e), e);
const y = {
jet: [[0, 33791], [0.125, 3975935], [0.375, 100663295], [0.625, 4294902015], [0.875, 4194304255], [1, 2147483903]],
hsv: [[0, 4278190335], [0.169, 4261348095], [0.173, 4160684799], [0.337, 16516351], [0.341, 16517887], [0.506, 33161215], [0.671, 33619455], [0.675, 134282751], [0.839, 4278254591], [0.843, 4278253055], [1, 4278191871]],
hot: [[0, 255], [0.3, 3858759935], [0.6, 4291952895], [1, 4294967295]],
spring: [[0, 4278255615], [1, 4294902015]],
summer: [[0, 8414975], [1, 4294928127]],
autumn: [[0, 4278190335], [1, 4294902015]],
winter: [[0, 65535], [1, 16744703]],
bone: [[0, 255], [0.376, 1414821119], [0.753, 2848508159], [1, 4294967295]],
copper: [[0, 255], [0.804, 4288702207], [1, 4291264511]],
greys: [[0, 255], [1, 4294967295]],
yignbu: [[0, 136141055], [0.125, 624203007], [0.25, 576628991], [0.375, 496091391], [0.5, 1102497023], [0.625, 2144189439], [0.75, 3353982207], [0.875, 3992508927], [1, 4294957567]],
greens: [[0, 4463615], [0.125, 7154943], [0.25, 596329983], [0.375, 1101749759], [0.5, 1959032575], [0.625, 2715393023], [0.75, 3353985279], [0.875, 3858096383], [1, 4160550399]],
yiorrd: [[0, 2147493631], [0.125, 3170903807], [0.25, 3810139391], [0.375, 4232981247], [0.5, 4253891839], [0.625, 4273097983], [0.75, 4275664639], [0.875, 4293763327], [1, 4294954239]],
bluered: [[0, 65535], [1, 4278190335]],
rdbu: [[0, 84585727], [0.35, 1787426815], [0.5, 3200171775], [0.6, 3702162687], [0.7, 3868285695], [1, 2987007231]],
picnic: [[0, 65535], [0.1, 865730559], [0.2, 1724710911], [0.3, 2580348927], [0.4, 3435986943], [0.5, 4294967295], [0.6, 4291624959], [0.7, 4288282623], [0.8, 4284927231], [0.9, 4284901119], [1, 4278190335]],
rainbow: [[0, 2516605695], [0.125, 51455], [0.25, 1703935], [0.375, 10027007], [0.5, 754947839], [0.625, 2550071551], [0.75, 4293525759], [0.875, 4285464831], [1, 4278190335]],
portland: [[0, 204702719], [0.25, 176732927], [0.5, 4073928959], [0.75, 4069472511], [1, 3642629887]],
blackbody: [[0, 255], [0.2, 3858759935], [0.4, 3872522495], [0.7, 4294967295], [1, 2697527295]],
earth: [[0, 33535], [0.1, 11842815], [0.2, 684861695], [0.4, 3873846015], [0.6, 2017858815], [1, 4294967295]],
electric: [[0, 255], [0.15, 503342335], [0.4, 2013291775], [0.6, 2690253055], [0.8, 3871867135], [1, 4294630655]],
alpha: [[0, 4294967040], [1, 4294967295]],
viridis: [[0, 1140937983], [0.13, 1194097407], [0.25, 995199999], [0.38, 745639679], [0.5, 563121663], [0.63, 665682431], [0.75, 1556636671], [0.88, 2866557695], [1, 4259784191]],
inferno: [[0, 1279], [0.13, 520898815], [0.25, 1427074559], [0.38, 2283956991], [0.5, 3124123135], [0.63, 3814274047], [0.75, 4186704639], [0.88, 4190712575], [1, 4244612351]],
magma: [[0, 1279], [0.13, 470828287], [0.25, 1326611455], [0.38, 2166718975], [0.5, 3040246527], [0.63, 3847251199], [0.75, 4219953663], [0.88, 4274161663], [1, 4244488191]],
plasma: [[0, 218662911], [0.13, 1258529279], [0.25, 2097391871], [0.38, 2820839167], [0.5, 3410393599], [0.63, 3849018879], [0.75, 4170465791], [0.88, 4257425663], [1, 4042859007]],
warm: [[0, 2097198079], [0.13, 2885729279], [0.25, 3674254079], [0.38, 4278223615], [0.5, 4282338047], [0.63, 4286251263], [0.75, 3937403135], [0.88, 3202613503], [1, 2482962687]],
cool: [[0, 2097198079], [0.13, 1946213119], [0.25, 1649077759], [0.38, 1150478335], [0.5, 13420031], [0.63, 16225023], [0.75, 16734463], [0.88, 687802623], [1, 2482962687]],
"rainbow-soft": [[0, 2097198079], [0.1, 3338712319], [0.2, 4278221311], [0.3, 4285268223], [0.4, 3737256191], [0.5, 2533294335], [0.6, 16726015], [0.7, 16160511], [0.8, 849862399], [0.9, 1731455999], [1, 2080422655]],
bathymetry: [[0, 672804095], [0.13, 993090303], [0.25, 1078758399], [0.38, 1064212479], [0.5, 1217306367], [0.63, 1437508607], [0.75, 2026808319], [0.88, 3152456959], [1, 4261334271]],
cdom: [[0, 789528319], [0.13, 1461147391], [0.25, 2182898687], [0.38, 2871615743], [0.5, 3460519679], [0.63, 3865728255], [0.75, 4069877759], [0.88, 4190210047], [1, 4276990207]],
chlorophyll: [[0, 304354559], [0.13, 423569919], [0.25, 408632319], [0.38, 225921279], [0.5, 311709951], [0.63, 1353538047], [0.75, 2227469055], [0.88, 2950537983], [1, 3623473407]],
density: [[0, 906896639], [0.13, 1494700287], [0.25, 1848476927], [0.38, 2018358015], [0.5, 2020726271], [0.63, 1939334399], [0.75, 2260329471], [0.88, 2983650303], [1, 3874615807]],
"freesurface-blue": [[0, 503607039], [0.13, 789491967], [0.25, 690875647], [0.38, 425972991], [0.5, 1149487359], [0.63, 1922876927], [0.75, 2645937151], [0.88, 3369130239], [1, 4058901759]],
"freesurface-red": [[0, 1007227647], [0.13, 1678842879], [0.25, 2383683071], [0.38, 2972392447], [0.5, 3226943487], [0.63, 3447548415], [0.75, 3634533631], [0.88, 3821519359], [1, 4058901759]],
oxygen: [[0, 1074071039], [0.13, 1778782207], [0.25, 2417625087], [0.38, 2822767615], [0.5, 3160671487], [0.63, 3465022463], [0.75, 3702397439], [0.88, 3889638655], [1, 4177422847]],
par: [[0, 856955135], [0.13, 1512055807], [0.25, 2167153407], [0.38, 2672040447], [0.5, 3059946495], [0.63, 3347453695], [0.75, 3567985663], [0.88, 3721541375], [1, 3791473663]],
phase: [[0, 2439582463], [0.13, 3091670783], [0.25, 3124392959], [0.38, 2689055231], [0.5, 1851906815], [0.63, 846963967], [0.75, 528707327], [0.88, 1300308735], [1, 2439582463]],
salinity: [[0, 706243839], [0.13, 556966655], [0.25, 257593855], [0.38, 678857215], [0.5, 999458815], [0.63, 1336901375], [0.75, 2026596607], [0.88, 3252511999], [1, 4260338431]],
temperature: [[0, 69415935], [0.13, 389249791], [0.25, 1429970431], [0.38, 2169475071], [0.5, 2942272255], [0.63, 3731908095], [0.75, 4187112191], [0.88, 4190388735], [1, 3908721663]],
turbidity: [[0, 572464127], [0.13, 1093806591], [0.25, 1648702719], [0.38, 2203662847], [0.5, 2708487167], [0.63, 3112977151], [0.75, 3400423679], [0.88, 3637608191], [1, 3925257215]],
"velocity-blue": [[0, 287326463], [0.13, 590640383], [0.25, 491887871], [0.38, 527540991], [0.5, 848341503], [0.63, 1471000831], [0.75, 2512698879], [0.88, 3420312575], [1, 4277921535]],
"velocity-green": [[0, 388174847], [0.13, 406857471], [0.25, 190787071], [0.38, 662381567], [0.5, 1603407103], [0.63, 2560955135], [0.75, 3384428031], [0.88, 3923282431], [1, 4294823423]],
cubehelix: [[0, 255], [0.07, 369441791], [0.13, 1006922239], [0.2, 1828816895], [0.27, 2701169663], [0.33, 3523383039], [0.4, 4211833855], [0.47, 4280115711], [0.53, 4281746943], [0.6, 4283772671], [0.67, 4286063359], [0.73, 4288488959], [0.8, 4055841279], [0.87, 3772538367], [0.93, 3673263871], [1, 3825059583]]
}, u = (t, r, e) => t + (r - t) * e, x = (t, r, e) => [
Math.round(u(t[0], r[0], e)),
Math.round(u(t[1], r[1], e)),
Math.round(u(t[2], r[2], e)),
Math.round(u(t[3], r[3], e))
], g = (t) => {
const r = t.toString(16).padStart(8, "0");
return Array.from(
{ length: 4 },
(e, n) => Number(`0x${r.slice(2 * n, 2 * (n + 1))}`)
);
};
class M {
constructor(r) {
h(this, "colors");
this.colors = r;
}
format(r = "cssHex") {
return this.colors.map((e) => b(e, r));
}
get length() {
return this.colors.length;
}
}
const S = ({ map: t = "viridis", steps: r = 10 } = {}) => {
const n = (typeof t == "string" ? y[t] : t).map(([s, o]) => ({
index: Math.round(s * (r - 1)),
color: typeof o == "number" ? g(o) : o
})).filter(({ index: s }, o, c) => {
if (s === r - 1 && r > 1)
return c.filter((i, d) => i.index === s && d > o).length < 1;
if (s === 0)
return c.findIndex((i) => i.index === s) === o;
const l = c.filter((i) => i.index === s), a = l[Math.floor(l.length / 2)];
return o === c.indexOf(a);
}), p = n.flatMap((s, o) => {
if (o === n.length - 1)
return [n[o].color];
const c = n[o + 1].index - n[o].index, l = n[o].color, a = n[o + 1].color;
return Array.from({ length: c }, (i, d) => x(l, a, d / c));
});
return new M(p);
}, b = (t, r) => {
if (r === "float")
return t.map((e) => e / 255);
if (r === "cssHex")
return `#${t.map((e) => e.toString(16).padStart(2, "0")).join("")}`.toUpperCase();
if (r === "cssRGBA")
return `rgba(${t.join(", ")})`;
if (r === "rgba")
return t;
if (r === "number")
return parseInt(t.map((e) => e.toString(16).padStart(2, "0")).join(""), 16);
throw new Error(`Unknown color format: "${r}"`);
};
export {
b as convertRGBA,
S as createPalette,
S as default,
y as maps
};