Skip to content

Commit

Permalink
feat: Add anti-aliasing to corners Aloxaf#232 (Aloxaf#234)
Browse files Browse the repository at this point in the history
* Implement blur/anti-aliasing on edges of window frame Aloxaf#232

* fix missing -1 for top right corner, add example directory and update picture to readme

I also added example.sh, I think it could also be excluded.

* Delete example.sh

* Create example.sh
  • Loading branch information
JoostScheffer authored Nov 22, 2023
1 parent cf3668c commit e195b5e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Silicon is an alternative to [Carbon](https://github.com/dawnlabs/carbon) implem

It can render your source code into a beautiful image.

<img width="66%" src="http://storage.aloxaf.cn/silicon.png?v=2">
<img width="66%" src="example/example.png">

## Why Silicon

Expand Down
Binary file added example/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions example/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn factorial(n: u64) -> u64 {
match n {
0 => 1,
_ => n * factorial(n - 1),
}
}

fn main() {
println!("10! = {}", factorial(10));
}
1 change: 1 addition & 0 deletions example/example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
silicon example.rs -o example.png
31 changes: 24 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,31 +269,48 @@ pub(crate) fn copy_alpha(src: &RgbaImage, dst: &mut RgbaImage, x: u32, y: u32) {
pub(crate) fn round_corner(image: &mut DynamicImage, radius: u32) {
// draw a circle with given foreground on given background
// then split it into four pieces and paste them to the four corner of the image
//
// the circle is drawn on a bigger image to avoid the aliasing
// later it will be scaled to the correct size
// we add +1 (to the radius) to make sure that there is also space for the border to mitigate artefacts when scaling
// note that the +1 isn't added to the radius when drawing the circle
let mut circle =
RgbaImage::from_pixel(radius * 2 + 1, radius * 2 + 1, Rgba([255, 255, 255, 0]));
RgbaImage::from_pixel((radius + 1) * 4, (radius + 1) * 4, Rgba([255, 255, 255, 0]));

let width = image.width();
let height = image.height();

// use the bottom right pixel to get the color of the foreground
let foreground = image.get_pixel(width - 1, height - 1);

// TODO: need a blur on edge
draw_filled_circle_mut(
&mut circle,
(radius as i32, radius as i32),
radius as i32,
(((radius + 1) * 2) as i32, ((radius + 1) * 2) as i32),
radius as i32 * 2,
foreground,
);

let part = crop_imm(&circle, 0, 0, radius, radius);
// scale down the circle to the correct size
let circle = resize(
&circle,
(radius + 1) * 2,
(radius + 1) * 2,
FilterType::Triangle,
);

// top left
let part = crop_imm(&circle, 1, 1, radius, radius);
image.copy_from(&*part, 0, 0).unwrap();

let part = crop_imm(&circle, radius + 1, 0, radius, radius);
// top right
let part = crop_imm(&circle, radius + 1, 1, radius, radius - 1);
image.copy_from(&*part, width - radius, 0).unwrap();

let part = crop_imm(&circle, 0, radius + 1, radius, radius);
// bottom left
let part = crop_imm(&circle, 1, radius + 1, radius, radius);
image.copy_from(&*part, 0, height - radius).unwrap();

// bottom right
let part = crop_imm(&circle, radius + 1, radius + 1, radius, radius);
image
.copy_from(&*part, width - radius, height - radius)
Expand Down

0 comments on commit e195b5e

Please sign in to comment.