1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 14:50:03 -04:00

plot-axis: Rebase label opacity calculation on master

This commit is contained in:
JohannesProgrammiert
2023-05-26 11:15:49 +02:00
parent f3b309fd8c
commit 68834dec8f

View File

@@ -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,17 +249,21 @@ 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);
@@ -295,15 +299,17 @@ impl<const AXIS: usize> Widget for AxisWidget<AXIS> {
} }
} }
} }
}
response response
} }
} }
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,
)
} }