prosperon/scripts/color.js

207 lines
4 KiB
JavaScript
Raw Normal View History

2023-11-29 12:40:13 -06:00
var Color = {
2024-09-26 11:36:09 -05:00
white: [255, 255, 255],
black: [0, 0, 0],
blue: [0, 0, 255],
green: [0, 255, 0],
yellow: [255, 255, 0],
red: [255, 0, 0],
gray: [181, 181, 181],
cyan: [0, 255, 255],
purple: [162, 93, 227],
orange: [255, 144, 64],
magenta: [255, 0, 255],
2023-11-29 12:40:13 -06:00
};
Color.editor = {};
Color.editor.ur = Color.green;
2024-09-26 11:36:09 -05:00
Color.tohtml = function (v) {
var html = v.map(function (n) {
return Number.hex(n * 255);
});
return "#" + html.join("");
};
2023-11-29 12:40:13 -06:00
2024-02-25 17:31:48 -06:00
var esc = {};
esc.reset = "\x1b[0";
2024-09-26 11:36:09 -05:00
esc.color = function (v) {
var c = v.map(function (n) {
return Math.floor(n * 255);
});
var truecolor = "\x1b[38;2;" + c.join(";") + ";";
2023-11-29 12:40:13 -06:00
return truecolor;
2024-09-26 11:36:09 -05:00
};
2023-11-29 12:40:13 -06:00
2024-02-25 17:31:48 -06:00
esc.doc = "Functions and constants for ANSI escape sequences.";
2023-11-29 12:40:13 -06:00
Color.Arkanoid = {
2024-09-26 11:36:09 -05:00
orange: [255, 143, 0],
teal: [0, 255, 255],
green: [0, 255, 0],
red: [255, 0, 0],
blue: [0, 112, 255],
purple: [255, 0, 255],
yellow: [255, 255, 0],
silver: [157, 157, 157],
gold: [188, 174, 0],
2023-11-29 12:40:13 -06:00
};
Color.Arkanoid.Powerups = {
2024-09-26 11:36:09 -05:00
red: [174, 0, 0] /* laser */,
blue: [0, 0, 174] /* enlarge */,
green: [0, 174, 0] /* catch */,
orange: [224, 143, 0] /* slow */,
purple: [210, 0, 210] /* break */,
cyan: [0, 174, 255] /* disruption */,
gray: [143, 143, 143] /* 1up */,
2023-11-29 12:40:13 -06:00
};
Color.Gameboy = {
2024-09-26 11:36:09 -05:00
darkest: [229, 107, 26],
dark: [229, 189, 26],
light: [189, 229, 26],
lightest: [107, 229, 26],
2023-11-29 12:40:13 -06:00
};
Color.Apple = {
2024-09-26 11:36:09 -05:00
green: [94, 189, 62],
yellow: [255, 185, 0],
orange: [247, 130, 0],
red: [226, 56, 56],
purple: [151, 57, 153],
blue: [0, 156, 223],
2023-11-29 12:40:13 -06:00
};
Color.Debug = {
boundingbox: Color.white,
2024-09-26 11:36:09 -05:00
names: [84, 110, 255],
2023-11-29 12:40:13 -06:00
};
Color.Editor = {
2024-09-26 11:36:09 -05:00
grid: [99, 255, 128],
select: [255, 255, 55],
newgroup: [120, 255, 10],
2023-11-29 12:40:13 -06:00
};
/* Detects the format of all colors and munges them into a floating point format */
2024-09-26 11:36:09 -05:00
Color.normalize = function (c) {
var add_a = function (a) {
2023-11-29 12:40:13 -06:00
var n = this.slice();
n.a = a;
return n;
};
2024-09-26 11:36:09 -05:00
2023-11-29 12:40:13 -06:00
for (var p of Object.keys(c)) {
var fmt = "nrm";
2024-09-26 11:36:09 -05:00
if (typeof c[p] !== "object") continue;
2023-11-29 12:40:13 -06:00
if (!Array.isArray(c[p])) {
Color.normalize(c[p]);
continue;
}
2024-09-26 11:36:09 -05:00
2024-06-26 13:12:34 -05:00
c[p][3] = 255;
2024-09-26 11:36:09 -05:00
2023-11-29 12:40:13 -06:00
for (var color of c[p]) {
if (color > 1) {
fmt = "8b";
2024-09-26 11:36:09 -05:00
break;
2023-11-29 12:40:13 -06:00
}
}
2024-09-26 11:36:09 -05:00
switch (fmt) {
2023-11-29 12:40:13 -06:00
case "8b":
2024-09-26 11:36:09 -05:00
c[p] = c[p].map(function (x) {
return x / 255;
});
2023-11-29 12:40:13 -06:00
}
c[p].alpha = add_a;
}
};
Color.normalize(Color);
var ColorMap = {};
2024-09-26 11:36:09 -05:00
ColorMap.makemap = function (map) {
2023-11-29 12:40:13 -06:00
var newmap = Object.create(ColorMap);
Object.assign(newmap, map);
return newmap;
2024-09-26 11:36:09 -05:00
};
2023-11-29 12:40:13 -06:00
ColorMap.Jet = ColorMap.makemap({
2024-09-26 11:36:09 -05:00
0: [0, 0, 131],
0.125: [0, 60, 170],
0.375: [5, 255, 255],
0.625: [255, 255, 0],
0.875: [250, 0, 0],
1: [128, 0, 0],
2023-11-29 12:40:13 -06:00
});
ColorMap.BlueRed = ColorMap.makemap({
2024-09-26 11:36:09 -05:00
0: [0, 0, 255],
1: [255, 0, 0],
2023-11-29 12:40:13 -06:00
});
ColorMap.Inferno = ColorMap.makemap({
2024-09-26 11:36:09 -05:00
0: [0, 0, 4],
0.13: [31, 12, 72],
0.25: [85, 15, 109],
0.38: [136, 34, 106],
0.5: [186, 54, 85],
0.63: [227, 89, 51],
0.75: [249, 140, 10],
0.88: [249, 201, 50],
1: [252, 255, 164],
2023-11-29 12:40:13 -06:00
});
ColorMap.Bathymetry = ColorMap.makemap({
2024-09-26 11:36:09 -05:00
0: [40, 26, 44],
0.13: [59.49, 90],
0.25: [64, 76, 139],
0.38: [63, 110, 151],
0.5: [72, 142, 158],
0.63: [85, 174, 163],
0.75: [120, 206, 163],
0.88: [187, 230, 172],
1: [253, 254, 204],
2023-11-29 12:40:13 -06:00
});
ColorMap.Viridis = ColorMap.makemap({
2024-09-26 11:36:09 -05:00
0: [68, 1, 84],
0.13: [71, 44, 122],
0.25: [59, 81, 139],
0.38: [44, 113, 142],
0.5: [33, 144, 141],
0.63: [39, 173, 129],
0.75: [92, 200, 99],
0.88: [170, 220, 50],
1: [253, 231, 37],
2023-11-29 12:40:13 -06:00
});
Color.normalize(ColorMap);
2024-09-26 11:36:09 -05:00
ColorMap.sample = function (t, map = this) {
2023-11-29 12:40:13 -06:00
if (t < 0) return map[0];
if (t > 1) return map[1];
var lastkey = 0;
for (var key of Object.keys(map).sort()) {
if (t < key) {
var b = map[key];
var a = map[lastkey];
var tt = (key - lastkey) * t;
return a.lerp(b, tt);
}
lastkey = key;
}
return map[1];
2024-09-26 11:36:09 -05:00
};
2023-11-29 12:40:13 -06:00
ColorMap.doc = {
sample: "Sample a given colormap at the given percentage (0 to 1).",
};
return {
Color,
esc,
2024-09-26 11:36:09 -05:00
ColorMap,
};