Skip to content

Commit

Permalink
feat: 文字の色を指定できるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
apple-yagi committed Jan 14, 2024
1 parent 80eaa6d commit 3d85ac8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.16", features = ["derive"] }
image = "0.24.7"
imageproc = "0.23.0"
rusttype = "0.9.3"
41 changes: 37 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
use std::env;
use std::process::exit;

use clap::Parser;
use image::{ImageBuffer, Rgba};
use imageproc::drawing::draw_text_mut;
use rusttype::{point, Font, Rect, Scale};

#[derive(Parser)]
struct Args {
/// 絵文字にする文字列
first: String,

/// 文字の色 (デフォルト: pink)
/// 指定できる色 : pink, yellow, black, red, green, blue
#[arg(short, long, default_value = "pink")]
color: String,
}

fn main() {
let args: Vec<String> = env::args().collect();
let text = &args[2];
let args = Args::parse();
let text = &args.first;
let color_result = get_color(&args.color);
let color = match color_result {
Ok(color) => color,
Err(err) => {
println!("{}", err);
exit(1)
}
};

let imgx = 500;
let imgy = 500;
Expand Down Expand Up @@ -57,7 +77,7 @@ fn main() {

draw_text_mut(
&mut imgbuf,
Rgba([255, 255, 255, 255]),
color,
center_x as i32,
center_y as i32,
scale,
Expand All @@ -66,4 +86,17 @@ fn main() {
);

imgbuf.save(text.to_owned() + ".png").unwrap();
exit(0)
}

fn get_color(color: &str) -> Result<Rgba<u8>, &str> {
match color {
"pink" => Ok(Rgba([255u8, 0u8, 255u8, 255u8])),
"yellow" => Ok(Rgba([255u8, 255u8, 0u8, 255u8])),
"black" => Ok(Rgba([0u8, 0u8, 0u8, 255u8])),
"red" => Ok(Rgba([255u8, 0u8, 0u8, 255u8])),
"green" => Ok(Rgba([0u8, 255u8, 0u8, 255u8])),
"blue" => Ok(Rgba([0u8, 0u8, 255u8, 255u8])),
_ => Err("指定できる色 : pink, yellow, black, red, green, blue, orange"),
}
}

0 comments on commit 3d85ac8

Please sign in to comment.