~jpl8/text-to-piet

75f682d4c437802f205a4fc15c6e3fc136ee06b9 — jpl 1 year, 7 months ago c9016eb
Add color filling
4 files changed, 92 insertions(+), 0 deletions(-)

M Cargo.lock
M Cargo.toml
M src/main.rs
M test.png
M Cargo.lock => Cargo.lock +37 -0
@@ 499,6 499,12 @@ dependencies = [
]

[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"

[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 541,6 547,36 @@ dependencies = [
]

[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
 "libc",
 "rand_chacha",
 "rand_core",
]

[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
 "ppv-lite86",
 "rand_core",
]

[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
 "getrandom",
]

[[package]]
name = "rayon"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 623,6 659,7 @@ version = "0.1.0"
dependencies = [
 "clap",
 "image",
 "rand",
]

[[package]]

M Cargo.toml => Cargo.toml +1 -0
@@ 7,4 7,5 @@ edition = "2021"

[dependencies]
image = "~0.24"
rand = "~0.8"
clap = { version = "~3.1", features = ["derive"] }

M src/main.rs => src/main.rs +54 -0
@@ 1,5 1,8 @@
use std::collections::HashSet;
use std::fs;

use rand::seq::SliceRandom;

use image::{Rgb, RgbImage};
use Instructions::*;



@@ 315,5 318,56 @@ fn colors_to_img(colors: Vec<Color>, img_size: u32, img_name: String) {
    (x, y) = update_x_y_spiral(x, y, 1, &final_dir.clockwise());
    img.put_pixel(x, y, Rgb(color.to_bytes()));

    // Fill in remaining colors
    let filler_spiral = Spiral2D::new(
        img_size - 1,
        img_size - 1,
        Direction::Left,
        img_size,
        colors.len() as u32,
    );

    let all_colors: HashSet<[u8; 3]> = vec![
        Color::LightRed.to_bytes(),
        Color::LightYellow.to_bytes(),
        Color::LightGreen.to_bytes(),
        Color::LightCyan.to_bytes(),
        Color::LightBlue.to_bytes(),
        Color::LightMagenta.to_bytes(),
        Color::Red.to_bytes(),
        Color::Yellow.to_bytes(),
        Color::Green.to_bytes(),
        Color::Cyan.to_bytes(),
        Color::Blue.to_bytes(),
        Color::Magenta.to_bytes(),
        Color::White.to_bytes(),
        Color::Black.to_bytes(),
        Color::DarkRed.to_bytes(),
        Color::DarkYellow.to_bytes(),
        Color::DarkGreen.to_bytes(),
        Color::DarkCyan.to_bytes(),
        Color::DarkBlue.to_bytes(),
        Color::DarkMagenta.to_bytes(),
    ]
    .into_iter()
    .collect();

    for (x, y, _) in filler_spiral {
        let neighbors: HashSet<[u8; 3]> = vec![
            img.get_pixel_checked(if x == 0 { 0 } else { x - 1 }, y),
            img.get_pixel_checked(x + 1, y),
            img.get_pixel_checked(x, if y == 0 { 0 } else { y - 1 }),
            img.get_pixel_checked(x, y + 1),
        ]
        .into_iter()
        .map(|c| if let Some(rgb) = c { rgb.0 } else { [0, 0, 0] })
        .collect();

        let legal_colors: Vec<&[u8; 3]> = all_colors.difference(&neighbors).collect();
        let picked_color = *legal_colors.choose(&mut rand::thread_rng()).unwrap();

        img.put_pixel(x, y, Rgb(*picked_color));
    }

    img.save(img_name).expect("Failed to save image");
}

M test.png => test.png +0 -0