Skip to content

Commit

Permalink
Add background image support
Browse files Browse the repository at this point in the history
  • Loading branch information
thecaralice committed Oct 3, 2020
1 parent c3c7f7b commit 3d8c330
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::formatter::{ImageFormatter, ImageFormatterBuilder};
use crate::utils::{ShadowAdder, ToRgba};
use crate::utils::{Background, ShadowAdder, ToRgba};
use anyhow::Error;
use clipboard::{ClipboardContext, ClipboardProvider};
use image::Rgba;
Expand Down Expand Up @@ -55,6 +55,9 @@ type Lines = Vec<u32>;
#[derive(StructOpt, Debug)]
#[structopt(name = "silicon")]
pub struct Config {
/// Background image
#[structopt(long, conflicts_with = "background")]
pub background_image: Option<PathBuf>,
/// Background color of the image
#[structopt(
long,
Expand Down Expand Up @@ -234,7 +237,10 @@ impl Config {

pub fn get_shadow_adder(&self) -> ShadowAdder {
ShadowAdder::new()
.background(self.background)
.background(match &self.background_image {
Some(path) => Background::Image(image::open(path).unwrap().to_rgba()),
None => Background::Solid(self.background),
})
.shadow_color(self.shadow_color)
.blur_radius(self.shadow_blur_radius)
.pad_horiz(self.pad_horiz)
Expand Down
32 changes: 26 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,31 @@ pub(crate) fn add_window_controls(image: &mut DynamicImage) {
copy_alpha(&title_bar, image.as_mut_rgba8().unwrap(), 15, 15);
}

#[derive(Clone, Debug)]
pub enum Background {
Solid(Rgba<u8>),
Image(RgbaImage),
}

impl Default for Background {
fn default() -> Self {
Self::Solid("#abb8c3".to_rgba().unwrap())
}
}

impl Background {
fn to_image(&self, width: u32, height: u32) -> RgbaImage {
match self {
Background::Solid(color) => RgbaImage::from_pixel(width, height, color.to_owned()),
Background::Image(image) => resize(image, width, height, FilterType::Triangle),
}
}
}

/// Add the shadow for image
#[derive(Debug)]
pub struct ShadowAdder {
background: Rgba<u8>,
background: Background,
shadow_color: Rgba<u8>,
blur_radius: f32,
pad_horiz: u32,
Expand All @@ -126,7 +147,7 @@ pub struct ShadowAdder {
impl ShadowAdder {
pub fn new() -> Self {
Self {
background: "#abb8c3".to_rgba().unwrap(),
background: Background::default(),
shadow_color: "#707070".to_rgba().unwrap(),
blur_radius: 50.0,
pad_horiz: 80,
Expand All @@ -137,8 +158,8 @@ impl ShadowAdder {
}

/// Set the background color
pub fn background(mut self, color: Rgba<u8>) -> Self {
self.background = color;
pub fn background(mut self, bg: Background) -> Self {
self.background = bg;
self
}

Expand Down Expand Up @@ -180,8 +201,7 @@ impl ShadowAdder {
let height = image.height() + self.pad_vert * 2;

// create the shadow
let mut shadow = RgbaImage::from_pixel(width, height, self.background);

let mut shadow = self.background.to_image(width, height);
if self.blur_radius > 0.0 {
let rect = Rect::at(
self.pad_horiz as i32 + self.offset_x,
Expand Down

0 comments on commit 3d8c330

Please sign in to comment.