Generate a Rust palette

This commit is contained in:
2024-04-22 18:34:32 -07:00
parent c49b042b62
commit 3d2d7cfea3
3 changed files with 92 additions and 7 deletions

View File

@ -149,6 +149,11 @@ function generateColors() {
return colors;
}
function clamp(x) {
if (x < 0.0) { return 0.0; }
if (x > 255.0) { return 255.0; }
return Math.round(x);
}
function dump(fname, colors, width, height, scalar) {
if (colors.length != width * height) {
@ -156,12 +161,6 @@ function dump(fname, colors, width, height, scalar) {
}
let pngdata = [];
let clamp = function(x) {
if (x < 0.0) { return 0.0; }
if (x > 255.0) { return 255.0; }
return Math.round(x);
}
for (var y = 0; y < height * scalar; y++) {
for (var x = 0; x < width * scalar; x++) {
let i = Math.floor(y / scalar) * width + Math.floor(x / scalar);
@ -181,4 +180,21 @@ function dump(fname, colors, width, height, scalar) {
png.pack().pipe(fs.createWriteStream(fname));
}
dump("palette.png", generateColors(), 8, 8, 64);
function dumpRust(fname, colors) {
var paletteString = "";
paletteString += "pub const PALETTE: [[u8; 3]; " + palette.length + "] = [\n"
for (var i = 0; i < colors.length; i++) {
var c = colors[i];
paletteString += (" [" +
clamp(255 * c.srgb.r) + ", " +
clamp(255 * c.srgb.g) + ", " +
clamp(255 * c.srgb.b) +
"],\n");
}
paletteString += "];\n"
fs.writeFileSync(fname, paletteString);
}
var palette = generateColors();
dump("palette.png", palette, 8, 8, 64);
dumpRust("../../src/palette.rs", palette)