Skip to content

Commit

Permalink
修复背景色的可选问题
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyia committed Aug 22, 2024
1 parent 189ded1 commit a416df8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { generateGroupAvatar } from '../index.js';
size:600, // 头像的大小
margin: 600 / 20, // 头像之间的间距
borderMargin: 600 / 20, // 头像与边框之间的间距
bgColor: [0, 0, 0, 0], // RGBA
bgColor: [222, 222, 222, 255], // RGBA
})
} catch (error) {
console.log(error);
Expand All @@ -43,7 +43,7 @@ export interface Config {
margin?: number
saveFile?: boolean // 是否保存文件 ,如果你想直接保存文件省去js的保存文件步骤可以将该选项设置为true
savePath?: string // 保存文件的路径
bgFile?: string // 背景图片路径,后续考虑支持传递buffer类型
bgFile?: string // 背景图片路径,如果有背景图优先使用背景图,后续考虑支持传递buffer类型
bgColor?: Array<number> // 长度3或4的数组,0~255
}
```
Expand Down
Binary file removed __test__/group_avatar.png
Binary file not shown.
2 changes: 1 addition & 1 deletion __test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('buffer to buffer', async (t) => {
size:600,
margin: 600 / 20,
borderMargin: 600 / 20,
bgColor: [0, 0, 0, 0], // RGBA
bgColor: [222, 222, 222, 255], // RGBA
})
} catch (error) {
console.log(error);
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export interface Config {
saveFile?: boolean
savePath?: string
bgFile?: string
bgColor: Array<number>
bgColor?: Array<number>
}
export declare function generateGroupAvatar(cfg: Config): Promise<Buffer | null>
33 changes: 17 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct Config{
pub save_file: Option<bool>,
pub save_path: Option<String>,
pub bg_file: Option<String>,
pub bg_color: Vec<u8>,
pub bg_color: Option<Vec<u8>>,
}
impl Config {
// 添加一个默认配置的方法
Expand All @@ -76,29 +76,30 @@ impl Config {
save_file: Some(false),
save_path: Some("group_avatar.png".to_string()),
bg_file: None,
bg_color: Vec::from([222, 222, 222, 255])
bg_color: None

}
}
}
#[napi]
async fn generate_group_avatar(cfg: Config) -> Result<Option<Buffer>> {
let config = Config::new_default();

// 如果 cfg.bg_color 长度不等于3且4 ,就返回js的错误

if cfg.bg_color.len() != 3 && cfg.bg_color.len() != 4 {
return Err(Error::new(
Status::GenericFailure,
format!("bg_color must be rgba 3 or 4 length U8int, but got length{}", cfg.bg_color.len()),
));
}
let bg_color: [ u8; 4 ] = if cfg.bg_color.len() == 3 {
[cfg.bg_color[0], cfg.bg_color[1], cfg.bg_color[2], 255]
let bg_color: [u8; 4] = if let Some(ref colors) = cfg.bg_color {
if colors.len() == 3 {
// 将Vec<u8>转换为[u8; 4]
[colors[0], colors[1], colors[2], 255]
} else if colors.len() == 4 {
[colors[0], colors[1], colors[2], colors[3]]
} else {
return Err(Error::new(
Status::GenericFailure,
format!("bg_color must be rgba 3 or 4 length U8int, but got length{}",colors.len()),
));
}
} else {
[cfg.bg_color[0], cfg.bg_color[1], cfg.bg_color[2], cfg.bg_color[3]]
// 如果cfg_bg_color是None,则使用默认值
[0, 0, 0, 0]
};

// 判断可选配置是否有值,并将值覆盖
let config = Config {
images: if cfg.images.is_empty() { config.images } else { cfg.images },
Expand All @@ -108,7 +109,7 @@ async fn generate_group_avatar(cfg: Config) -> Result<Option<Buffer>> {
save_file: if cfg.save_file.is_some() { cfg.save_file } else { config.save_file },
save_path: if cfg.save_path.is_some() { cfg.save_path } else { config.save_path },
bg_file: config.bg_file,
bg_color: bg_color.to_vec()
bg_color:Some(bg_color.to_vec())
};

napi::tokio::task::spawn(async move {
Expand Down

0 comments on commit a416df8

Please sign in to comment.