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

Better handle additive colors in plots #3387

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions crates/ecolor/src/color32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ impl Color32 {
Self([r, g, b, 0])
}

/// Is the alpha=0 ?
#[inline(always)]
pub fn is_additive(self) -> bool {
self.a() == 0
}

/// Premultiplied RGBA
#[inline(always)]
pub const fn to_array(&self) -> [u8; 4] {
Expand Down
6 changes: 6 additions & 0 deletions crates/ecolor/src/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ impl Rgba {
Self([r, g, b, 0.0])
}

/// Is the alpha=0 ?
#[inline(always)]
pub fn is_additive(self) -> bool {
self.a() == 0.0
}

/// Multiply with e.g. 0.5 to make us half transparent
#[inline(always)]
pub fn multiply(self, alpha: f32) -> Self {
Expand Down
14 changes: 11 additions & 3 deletions crates/egui_plot/src/items/rect_elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,16 @@ pub(super) trait RectElement {

pub(super) fn highlighted_color(mut stroke: Stroke, fill: Color32) -> (Stroke, Color32) {
stroke.width *= 2.0;
let fill = Rgba::from(fill);
let fill_alpha = (2.0 * fill.a()).at_most(1.0);
let fill = fill.to_opaque().multiply(fill_alpha);

let mut fill = Rgba::from(fill);
if fill.is_additive() {
// Make slightly brighter
fill = 1.3 * fill;
} else {
// Make more opaque:
let fill_alpha = (2.0 * fill.a()).at_most(1.0);
fill = fill.to_opaque().multiply(fill_alpha);
}

(stroke, fill.into())
}