Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anti-aliasing to corners #232 #234

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement blur/anti-aliasing on edges of window frame #232
  • Loading branch information
JoostScheffer committed May 20, 2023
commit 329e58c45a1c9e810f71bb333405373dd0d89f71
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);
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