Documentation
use cloudmap::cm3;
use cloudmap::cm3_arrow;
use cloudmap::cm3_blue;
use cloudmap::cm3_mud;

fn main() {
    toimage_cm3();
    toimage_arrow();
    toimage_blue();
    toimage_mud();
    println!("Hello, world!");
}
fn toimage_arrow() {
    let imgx = 100;
    let imgy = 100;

    let (ca, cb, cc, cd) = (1.0, 0.9, 0.41, 1.0);
    let scalex = 3.0 / imgx as f32;
    let scaley = 3.0 / imgy as f32;

    let mut imgbuf = image::ImageBuffer::new(imgx, imgy);

    for x in 0..imgx {
        //  print!("        {}", x);
        for y in 0..imgy {
            let pixel = imgbuf.get_pixel_mut(x, y);
            let image::Rgba(data) = *pixel;
            let mut cmv: [u32; 4] = [0, 0, 0, 0];
            let mut u = 0.0;
            let mut v = 0.0;
            let ux = -0.950;
            let vy = 0.050;
            let vv = 0.2;
            if imgx - y <= x {
                // // print!("        up\n");
                u = ((x as f64) * 2.0 - (imgx as f64)) / imgx as f64;
                v = ((y as f64) * 2.0 - (imgx as f64)) / imgx as f64;
                cmv = cm3_arrow(ux, vy, u, v, vv, 0.0, 12);
            } else {
                // print!("        down\n");
                // u = 1.0 - ((y as f64)*1.0-0.5*(imgx as f64))/ imgx as f64;
                // v = ((x as f64)*1.0-0.5*(imgx as f64))/ imgx as f64;
                u = ((y as f64) * 2.0 - (imgx as f64)) / imgx as f64;
                v = ((x as f64) * 2.0 - (imgx as f64)) / imgx as f64;
                cmv = cm3_arrow(ux, vy, v, u, vv, 0.0, 12);
            }

            *pixel = image::Rgba([cmv[0] as u8, cmv[1] as u8, cmv[2] as u8, cmv[3] as u8]);
        }
    }

    // Save the image as “fractal.png”, the format is deduced from the path
    imgbuf.save("cm_arrow.png").unwrap();
}

fn toimage_cm3() {
    let imgx = 100;
    let imgy = 100;

    // let (ca,cb,cc,cd)=(0.427,1.0,0.6069,0.41406);
    let (ca, cb, cc, cd) = (1.0, 0.9, 0.0, 1.0);
    let scalex = 3.0 / imgx as f32;
    let scaley = 3.0 / imgy as f32;

    // Create a new ImgBuf with width: imgx and height: imgy
    let mut imgbuf = image::ImageBuffer::new(imgx, imgy);

    // Iterate over the coordinates and pixels of the image
    for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
        // let r = (0.3 * x as f32) as u8;
        // let b = (0.3 * y as f32) as u8;
        *pixel = image::Rgba([0, 0, 0, 0]);
    }

    // A redundant loop to demonstrate reading image data
    for x in 0..imgx - 1 {
        // print!("        {}\n", x);
        for y in 0..imgy - 1 {
            let pixel = imgbuf.get_pixel_mut(x, y);
            let image::Rgba(data) = *pixel;
            let mut cmv = [0, 0, 0, 0];
            let mut u = 0.0;
            let mut v = 0.0;
            if imgx - y <= x {
                let u = x as f64 / imgx as f64;
                let v = 1.0 - y as f64 / imgx as f64;
                cmv = cm3(u, v, ca, cb, cc, 0.0, 12);
            } else {
                let u = 1.0 - y as f64 / imgx as f64;
                let v = x as f64 / imgx as f64;
                cmv = cm3(u, v, ca, cd, cc, 0.0, 12);
            }

            // print!("{} {} {} ,", cmv[0], cmv[1], cmv[2]);
            *pixel = image::Rgba([cmv[0] as u8, cmv[1] as u8, cmv[2] as u8, cmv[3] as u8]);
        }
    }

    // Save the image as “fractal.png”, the format is deduced from the path
    imgbuf.save("cm.png").unwrap();
}

fn toimage_blue() {
    let imgx = 100;
    let imgy = 100;

    // let (ca,cb,cc,cd)=(0.427,1.0,0.6069,0.41406);
    let (ca, cb, cc, cd) = (1.0, 0.9, 0.0, 1.0);
    let scalex = 3.0 / imgx as f32;
    let scaley = 3.0 / imgy as f32;

    // Create a new ImgBuf with width: imgx and height: imgy
    let mut imgbuf = image::ImageBuffer::new(imgx, imgy);

    // Iterate over the coordinates and pixels of the image
    for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
        // let r = (0.3 * x as f32) as u8;
        // let b = (0.3 * y as f32) as u8;
        *pixel = image::Rgba([0, 0, 0, 0]);
    }

    // A redundant loop to demonstrate reading image data
    for x in 0..imgx - 1 {
        // print!("        {}\n", x);
        for y in 0..imgy - 1 {
            let pixel = imgbuf.get_pixel_mut(x, y);
            let image::Rgba(data) = *pixel;
            let mut cmv = [0, 0, 0, 0];
            let mut u = 0.0;
            let mut v = 0.0;
            if imgx - y <= x {
                let u = x as f64 / imgx as f64;
                let v = 1.0 - y as f64 / imgx as f64;
                cmv = cm3_blue(u, v, ca, cb, cc, 0.0, 12);
            } else {
                let u = 1.0 - y as f64 / imgx as f64;
                let v: f64 = x as f64 / imgx as f64;
                cmv = cm3_blue(u, v, ca, cd, cc, 0.0, 12);
            }

            // print!("{} {} {} ,", cmv[0], cmv[1], cmv[2]);
            *pixel = image::Rgba([cmv[0] as u8, cmv[1] as u8, cmv[2] as u8, cmv[3] as u8]);
        }
    }

    // Save the image as “fractal.png”, the format is deduced from the path
    imgbuf.save("cm_blue.png").unwrap();
}

fn toimage_mud() {
    let imgx = 100;
    let imgy = 100;

    // let (ca,cb,cc,cd)=(0.427,1.0,0.6069,0.41406);
    let (ca, cb, cc, cd) = (1.0, 0.9, 0.0, 1.0);
    let scalex = 3.0 / imgx as f32;
    let scaley = 3.0 / imgy as f32;

    // Create a new ImgBuf with width: imgx and height: imgy
    let mut imgbuf = image::ImageBuffer::new(imgx, imgy);

    // Iterate over the coordinates and pixels of the image
    for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
        // let r = (0.3 * x as f32) as u8;
        // let b = (0.3 * y as f32) as u8;
        *pixel = image::Rgba([0, 0, 0, 0]);
    }

    // A redundant loop to demonstrate reading image data
    for x in 0..imgx - 1 {
        // print!("        {}\n", x);
        for y in 0..imgy - 1 {
            let pixel = imgbuf.get_pixel_mut(x, y);
            let image::Rgba(data) = *pixel;
            let mut cmv = [0, 0, 0, 0];
            let mut u = 0.0;
            let mut v = 0.0;
            if imgx - y <= x {
                let u = x as f64 / imgx as f64;
                let v = 1.0 - y as f64 / imgx as f64;
                cmv = cm3_mud(u, v, ca, cb, cc, 0.0, 12);
            } else {
                let u = 1.0 - y as f64 / imgx as f64;
                let v = x as f64 / imgx as f64;
                cmv = cm3_mud(u, v, ca, cd, cc, 0.0, 12);
            }

            // print!("{} {} {} ,", cmv[0], cmv[1], cmv[2]);
            *pixel = image::Rgba([cmv[0] as u8, cmv[1] as u8, cmv[2] as u8, cmv[3] as u8]);
        }
    }

    // Save the image as “fractal.png”, the format is deduced from the path
    imgbuf.save("cm_mud.png").unwrap();
}