mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 23:00:04 -04:00
plot-axis: Rebase label opacity calculation on master
This commit is contained in:
@@ -4,13 +4,13 @@ use std::{
|
||||
};
|
||||
|
||||
use epaint::{
|
||||
emath::{remap_clamp, round_to_decimals, NumExt},
|
||||
Color32, Pos2, Rect, Rgba, Shape, Stroke, TextShape,
|
||||
emath::{lerp, remap_clamp, round_to_decimals},
|
||||
Color32, Pos2, Rect, Shape, Stroke, TextShape,
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
@@ -249,17 +249,21 @@ impl<const AXIS: usize> Widget for AxisWidget<AXIS> {
|
||||
for step in self.steps {
|
||||
let text = (self.hints.formatter)(step.value, self.hints.digits, &self.range);
|
||||
if !text.is_empty() {
|
||||
const MIN_TEXT_SPACING: f32 = 20.0;
|
||||
const FULL_CONTRAST_SPACING: f32 = 40.0;
|
||||
let spacing_in_points =
|
||||
(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,
|
||||
(MIN_LINE_SPACING_IN_POINTS as f32 * 2.0)..=300.0,
|
||||
0.0..=0.15,
|
||||
MIN_TEXT_SPACING..=FULL_CONTRAST_SPACING,
|
||||
0.0..=1.0,
|
||||
);
|
||||
|
||||
if line_alpha > 0.0 {
|
||||
let line_color = color_from_alpha(ui, line_alpha);
|
||||
let line_color = color_from_contrast(ui, line_weight);
|
||||
let galley = ui
|
||||
.painter()
|
||||
.layout_no_wrap(text, font_id.clone(), line_color);
|
||||
@@ -295,15 +299,17 @@ impl<const AXIS: usize> Widget for AxisWidget<AXIS> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
fn color_from_alpha(ui: &Ui, alpha: f32) -> Color32 {
|
||||
if ui.visuals().dark_mode {
|
||||
Rgba::from_white_alpha(alpha).into()
|
||||
} else {
|
||||
Rgba::from_black_alpha((4.0 * alpha).at_most(1.0)).into()
|
||||
}
|
||||
fn color_from_contrast(ui: &Ui, contrast: f32) -> Color32 {
|
||||
let bg = ui.visuals().extreme_bg_color;
|
||||
let fg = ui.visuals().widgets.open.fg_stroke.color;
|
||||
let mix = 0.5 * contrast.sqrt();
|
||||
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