Skip to content

Commit

Permalink
Add support for transparency (Aloxaf#31)
Browse files Browse the repository at this point in the history
* Add support for alpha channel

* Fix compatibility with shadow

* Small fix

* Update README

* Use RGBA instead of ARGB
  • Loading branch information
Aloxaf authored Aug 8, 2019
1 parent 4ea9203 commit d8085ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pikaur -S silicon
sudo pacman -S --needed pkgconf freetype2 fontconfig libxcb xclip
```

## Basic Usage
## Examples

Read code from file

Expand Down Expand Up @@ -94,7 +94,15 @@ silicon main.rs -o main.png --highlight-lines '1; 3-4'
Custom the image

```bash
silicon ./target/test.rs -o test.png --shadow-color '#555555' --background '#ffffff' --shadow-blur-radius 30 --no-window-controls
silicon ./target/test.rs -o test.png \
--shadow-color '#555555' --background '#ffffff' \
--shadow-blur-radius 30 --no-window-controls
```

Transparent background

```bash
silicon ./target/test.rs -o test.png --background '#ffffff00'
```

see `silicon --help` for detail
2 changes: 1 addition & 1 deletion src/blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn box_blur_horz(
val_r += isize::from(bb[0]) - isize::from(fv[0]);
val_g += isize::from(bb[1]) - isize::from(fv[1]);
val_b += isize::from(bb[2]) - isize::from(fv[2]);
val_a += isize::from(bb[2]) - isize::from(fv[3]);
val_a += isize::from(bb[3]) - isize::from(fv[3]);

frontbuf[ti] = [
round(val_r as f32 * iarr) as u8,
Expand Down
28 changes: 25 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,30 @@ pub fn init_syntect() -> (SyntaxSet, ThemeSet) {
)
}

pub(crate) trait ToRgba {
pub trait ToRgba {
type Target;
fn to_rgba(&self) -> Self::Target;
}

/// Parse hex color (#RRGGBB or #RRGGBBAA)
impl ToRgba for str {
type Target = Result<Rgba<u8>, std::num::ParseIntError>;
fn to_rgba(&self) -> Self::Target {
let rgb = u32::from_str_radix(&self[1..], 16)?;
let mut rgb = u32::from_str_radix(&self[1..], 16)?;

let alpha = if self.len() <= 7 {
0xff
} else {
let alpha = (rgb & 0xff) as u8;
rgb >>= 8;
alpha
};

Ok(Rgba([
((rgb >> 16) & 0xff) as u8,
((rgb >> 8) & 0xff) as u8,
(rgb & 0xff) as u8,
0xff,
alpha,
]))
}
}
Expand Down Expand Up @@ -285,3 +295,15 @@ pub(crate) fn draw_filled_circle_mut<I>(
}
}
}

#[cfg(test)]
mod tests {
use crate::utils::ToRgba;
use image::Rgba;

#[test]
fn to_rgba() {
assert_eq!("#abcdef".to_rgba().unwrap(), Rgba([0xab, 0xcd, 0xef, 0xff]));
assert_eq!("#abcdef00".to_rgba().unwrap(), Rgba([0xab, 0xcd, 0xef, 0x00]));
}
}

0 comments on commit d8085ed

Please sign in to comment.