Skip to content

Commit

Permalink
Add opacity factor to TextShape (#3916)
Browse files Browse the repository at this point in the history
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo cranky`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

This PR simply allows you to override the opacity of a Galley when you
draw it on screen. Last year I opened #3548 and some changes were
requested to the PR, but unfortunately school got really busy and I
wasn't able to apply them. This PR supersedes #3548 and applys the
changes requested in that PR

Closes #3548
  • Loading branch information
StratusFearMe21 authored Jan 30, 2024
1 parent 527f4bf commit 945a69d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,10 @@ pub struct TextShape {
/// This only affects the glyphs and will NOT replace background color nor strikethrough/underline color.
pub override_text_color: Option<Color32>,

/// If set, the text will be rendered with the given opacity in gamma space
/// Affects everything: backgrounds, glyphs, strikethough, underline, etc.
pub opacity_factor: f32,

/// Rotate text by this many radians clockwise.
/// The pivot is `pos` (the upper left corner of the text).
pub angle: f32,
Expand All @@ -762,6 +766,7 @@ impl TextShape {
underline: Stroke::NONE,
fallback_color,
override_text_color: None,
opacity_factor: 1.0,
angle: 0.0,
}
}
Expand Down Expand Up @@ -792,6 +797,13 @@ impl TextShape {
self.angle = angle;
self
}

/// Render text with this opacity in gamma space
#[inline]
pub fn with_opacity_factor(mut self, opacity_factor: f32) -> Self {
self.opacity_factor = opacity_factor;
self
}
}

impl From<TextShape> for Shape {
Expand Down
1 change: 1 addition & 0 deletions crates/epaint/src/shape_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn adjust_colors(shape: &mut Shape, adjust_color: &impl Fn(&mut Color32)) {
underline,
fallback_color,
override_text_color,
opacity_factor: _,
angle: _,
}) => {
adjust_color(&mut underline.color);
Expand Down
9 changes: 9 additions & 0 deletions crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,13 +1474,18 @@ impl Tessellator {
underline,
override_text_color,
fallback_color,
opacity_factor,
angle,
} = text_shape;

if galley.is_empty() {
return;
}

if *opacity_factor <= 0.0 {
return;
}

if galley.pixels_per_point != self.pixels_per_point {
eprintln!("epaint: WARNING: pixels_per_point (dpi scale) have changed between text layout and tessellation. \
You must recreate your text shapes if pixels_per_point changes.");
Expand Down Expand Up @@ -1548,6 +1553,10 @@ impl Tessellator {
color = *fallback_color;
}

if *opacity_factor < 1.0 {
color = color.gamma_multiply(*opacity_factor);
}

crate::epaint_assert!(color != Color32::PLACEHOLDER, "A placeholder color made it to the tessellator. You forgot to set a fallback color.");

let offset = if *angle == 0.0 {
Expand Down

0 comments on commit 945a69d

Please sign in to comment.