1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use clap::Parser;
use lottieconv::Converter;
use rgb::RGBA;
use rlottie::Animation;
use std::{fs::File, path::PathBuf};
#[derive(Parser)]
struct Args {
#[clap(name = "lottieFileName")]
lottie_file_name: PathBuf,
#[clap(name = "bgColor", default_value = "0")]
bg_color: String,
#[clap(long = "non-transparent")]
bg_non_transparent: bool,
#[clap(short = 'o', long = "out", name = "output")]
output: Option<PathBuf>
}
#[derive(Debug)]
struct InvalidHex;
fn color_from_hex(hex: u32, alpha: bool) -> Result<RGBA<u8, bool>, InvalidHex> {
if hex >> 24 != 0 {
return Err(InvalidHex);
}
Ok(RGBA {
r: ((hex >> 16) & 0xFF) as u8,
g: ((hex >> 8) & 0xFF) as u8,
b: (hex & 0xFF) as u8,
a: alpha
})
}
fn main() {
let args = Args::parse();
let path = args.lottie_file_name;
let file_stem = path.file_stem().expect("Missing file stem");
let bg_color = u32::from_str_radix(&args.bg_color, 16).expect("Invalid bgColor");
let bg =
color_from_hex(bg_color, !args.bg_non_transparent).expect("Invalid bgColor");
println!("Converting file {} ...", path.display());
let player = Animation::from_file(&path).expect("Failed to open file");
let gif_path = match args.output {
Some(path) => path,
None => {
let mut gif_path = path.clone();
let mut gif_file_name = file_stem.to_owned();
gif_file_name.push(".gif");
gif_path.set_file_name(gif_file_name);
gif_path
}
};
Converter::new(player)
.gif(
bg,
File::create(&gif_path).expect("Failed to create output file")
)
.and_then(Converter::convert)
.expect("Conversion failed");
}