Generate a Rust palette

This commit is contained in:
Pyrex 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; 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) { function dump(fname, colors, width, height, scalar) {
if (colors.length != width * height) { if (colors.length != width * height) {
@ -156,12 +161,6 @@ function dump(fname, colors, width, height, scalar) {
} }
let pngdata = []; 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 y = 0; y < height * scalar; y++) {
for (var x = 0; x < width * scalar; x++) { for (var x = 0; x < width * scalar; x++) {
let i = Math.floor(y / scalar) * width + Math.floor(x / scalar); 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)); 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)

View File

@ -1 +1,4 @@
mod palette;
pub use palette::PALETTE;
pub type VResult<T> = Result<T, anyhow::Error>; pub type VResult<T> = Result<T, anyhow::Error>;

66
src/palette.rs Normal file
View File

@ -0,0 +1,66 @@
pub const PALETTE: [[u8; 3]; 64] = [
[0, 0, 0],
[0, 0, 255],
[0, 255, 0],
[0, 255, 255],
[255, 0, 0],
[255, 0, 255],
[255, 255, 0],
[255, 255, 255],
[32, 45, 44],
[41, 60, 67],
[18, 22, 32],
[40, 34, 49],
[27, 17, 29],
[54, 42, 47],
[46, 33, 23],
[66, 60, 43],
[39, 92, 148],
[55, 83, 107],
[110, 45, 90],
[79, 50, 74],
[130, 103, 52],
[100, 85, 68],
[80, 133, 85],
[88, 106, 80],
[59, 115, 206],
[76, 108, 153],
[165, 53, 114],
[124, 70, 99],
[188, 157, 22],
[154, 137, 89],
[57, 194, 137],
[109, 161, 129],
[39, 187, 255],
[123, 67, 240],
[186, 66, 177],
[239, 60, 80],
[255, 170, 48],
[186, 255, 0],
[69, 255, 115],
[2, 241, 228],
[150, 190, 255],
[185, 113, 226],
[220, 118, 175],
[249, 134, 85],
[242, 204, 114],
[194, 255, 116],
[100, 255, 173],
[105, 242, 252],
[201, 169, 248],
[221, 193, 228],
[250, 156, 143],
[240, 197, 180],
[230, 237, 137],
[233, 243, 193],
[116, 253, 234],
[182, 247, 242],
[243, 222, 238],
[237, 228, 236],
[254, 235, 224],
[245, 241, 240],
[245, 254, 225],
[250, 253, 246],
[211, 250, 250],
[230, 247, 244],
];