mirror of https://github.com/Aloxaf/silicon
Add support for transparency (#31)
* Add support for alpha channel * Fix compatibility with shadow * Small fix * Update README * Use RGBA instead of ARGB
This commit is contained in:
parent
4ea9203602
commit
d8085ed250
12
README.md
12
README.md
|
@ -65,7 +65,7 @@ pikaur -S silicon
|
|||
sudo pacman -S --needed pkgconf freetype2 fontconfig libxcb xclip
|
||||
```
|
||||
|
||||
## Basic Usage
|
||||
## Examples
|
||||
|
||||
Read code from file
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
28
src/utils.rs
28
src/utils.rs
|
@ -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,
|
||||
]))
|
||||
}
|
||||
}
|
||||
|
@ -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]));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue