From 3d2d7cfea3da5a73c034dca1152ce296d9ea242a Mon Sep 17 00:00:00 2001 From: Nyeogmi Date: Mon, 22 Apr 2024 18:34:32 -0700 Subject: [PATCH] Generate a Rust palette --- slop/palette/index.mjs | 30 ++++++++++++++----- src/lib.rs | 3 ++ src/palette.rs | 66 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 src/palette.rs diff --git a/slop/palette/index.mjs b/slop/palette/index.mjs index 6db7580..2ff27a7 100644 --- a/slop/palette/index.mjs +++ b/slop/palette/index.mjs @@ -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); \ No newline at end of file +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) \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 3c18475..37c23df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,4 @@ +mod palette; + +pub use palette::PALETTE; pub type VResult = Result; diff --git a/src/palette.rs b/src/palette.rs new file mode 100644 index 0000000..3824ef9 --- /dev/null +++ b/src/palette.rs @@ -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], +];