mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
plot-axis: Rebase label opacity calculation on master
This commit is contained in:
@@ -4,13 +4,13 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use epaint::{
|
use epaint::{
|
||||||
emath::{remap_clamp, round_to_decimals, NumExt},
|
emath::{lerp, remap_clamp, round_to_decimals},
|
||||||
Color32, Pos2, Rect, Rgba, Shape, Stroke, TextShape,
|
Color32, Pos2, Rect, Shape, Stroke, TextShape,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Response, Sense, TextStyle, Ui, Widget, WidgetText};
|
use crate::{Response, Sense, TextStyle, Ui, Widget, WidgetText};
|
||||||
|
|
||||||
use super::{transform::PlotTransform, GridMark, MIN_LINE_SPACING_IN_POINTS};
|
use super::{transform::PlotTransform, GridMark};
|
||||||
|
|
||||||
pub(super) type AxisFormatterFn = fn(f64, usize, &RangeInclusive<f64>) -> String;
|
pub(super) type AxisFormatterFn = fn(f64, usize, &RangeInclusive<f64>) -> String;
|
||||||
|
|
||||||
@@ -249,50 +249,53 @@ impl<const AXIS: usize> Widget for AxisWidget<AXIS> {
|
|||||||
for step in self.steps {
|
for step in self.steps {
|
||||||
let text = (self.hints.formatter)(step.value, self.hints.digits, &self.range);
|
let text = (self.hints.formatter)(step.value, self.hints.digits, &self.range);
|
||||||
if !text.is_empty() {
|
if !text.is_empty() {
|
||||||
|
const MIN_TEXT_SPACING: f32 = 20.0;
|
||||||
|
const FULL_CONTRAST_SPACING: f32 = 40.0;
|
||||||
let spacing_in_points =
|
let spacing_in_points =
|
||||||
(transform.dpos_dvalue()[AXIS] * step.step_size).abs() as f32;
|
(transform.dpos_dvalue()[AXIS] * step.step_size).abs() as f32;
|
||||||
|
|
||||||
let line_alpha = remap_clamp(
|
if spacing_in_points <= MIN_TEXT_SPACING {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let line_weight = remap_clamp(
|
||||||
spacing_in_points,
|
spacing_in_points,
|
||||||
(MIN_LINE_SPACING_IN_POINTS as f32 * 2.0)..=300.0,
|
MIN_TEXT_SPACING..=FULL_CONTRAST_SPACING,
|
||||||
0.0..=0.15,
|
0.0..=1.0,
|
||||||
);
|
);
|
||||||
|
|
||||||
if line_alpha > 0.0 {
|
let line_color = color_from_contrast(ui, line_weight);
|
||||||
let line_color = color_from_alpha(ui, line_alpha);
|
let galley = ui
|
||||||
let galley = ui
|
.painter()
|
||||||
.painter()
|
.layout_no_wrap(text, font_id.clone(), line_color);
|
||||||
.layout_no_wrap(text, font_id.clone(), line_color);
|
let text_pos = match AXIS {
|
||||||
let text_pos = match AXIS {
|
X_AXIS => {
|
||||||
X_AXIS => {
|
let y = match self.hints.placement {
|
||||||
let y = match self.hints.placement {
|
Placement::Default => self.rect.min.y,
|
||||||
Placement::Default => self.rect.min.y,
|
Placement::Opposite => self.rect.max.y - galley.size().y,
|
||||||
Placement::Opposite => self.rect.max.y - galley.size().y,
|
};
|
||||||
};
|
let projected_point = super::PlotPoint::new(step.value, 0.0);
|
||||||
let projected_point = super::PlotPoint::new(step.value, 0.0);
|
Pos2 {
|
||||||
Pos2 {
|
x: transform.position_from_point(&projected_point).x
|
||||||
x: transform.position_from_point(&projected_point).x
|
- galley.size().x / 2.0,
|
||||||
- galley.size().x / 2.0,
|
y,
|
||||||
y,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Y_AXIS => {
|
}
|
||||||
let x = match self.hints.placement {
|
Y_AXIS => {
|
||||||
Placement::Default => self.rect.max.x - galley.size().x,
|
let x = match self.hints.placement {
|
||||||
Placement::Opposite => self.rect.min.x,
|
Placement::Default => self.rect.max.x - galley.size().x,
|
||||||
};
|
Placement::Opposite => self.rect.min.x,
|
||||||
let projected_point = super::PlotPoint::new(0.0, step.value);
|
};
|
||||||
Pos2 {
|
let projected_point = super::PlotPoint::new(0.0, step.value);
|
||||||
x,
|
Pos2 {
|
||||||
y: transform.position_from_point(&projected_point).y
|
x,
|
||||||
- galley.size().y / 2.0,
|
y: transform.position_from_point(&projected_point).y
|
||||||
}
|
- galley.size().y / 2.0,
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
}
|
||||||
};
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
ui.painter().add(Shape::galley(text_pos, galley));
|
ui.painter().add(Shape::galley(text_pos, galley));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -300,10 +303,13 @@ impl<const AXIS: usize> Widget for AxisWidget<AXIS> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn color_from_alpha(ui: &Ui, alpha: f32) -> Color32 {
|
fn color_from_contrast(ui: &Ui, contrast: f32) -> Color32 {
|
||||||
if ui.visuals().dark_mode {
|
let bg = ui.visuals().extreme_bg_color;
|
||||||
Rgba::from_white_alpha(alpha).into()
|
let fg = ui.visuals().widgets.open.fg_stroke.color;
|
||||||
} else {
|
let mix = 0.5 * contrast.sqrt();
|
||||||
Rgba::from_black_alpha((4.0 * alpha).at_most(1.0)).into()
|
Color32::from_rgb(
|
||||||
}
|
lerp((bg.r() as f32)..=(fg.r() as f32), mix) as u8,
|
||||||
|
lerp((bg.g() as f32)..=(fg.g() as f32), mix) as u8,
|
||||||
|
lerp((bg.b() as f32)..=(fg.b() as f32), mix) as u8,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user