Skip to content
Merged
32 changes: 29 additions & 3 deletions egui_plot/src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::fmt::Debug;
use std::ops::RangeInclusive;
use std::sync::Arc;

use egui::Color32;
use egui::FontId;
use egui::Pos2;
use egui::Rangef;
use egui::Rect;
Expand All @@ -21,7 +23,6 @@ use emath::remap;

use crate::bounds::PlotBounds;
use crate::bounds::PlotPoint;
use crate::colors;
use crate::grid::GridMark;
use crate::placement::HPlacement;
use crate::placement::Placement;
Expand Down Expand Up @@ -62,6 +63,8 @@ pub struct AxisHints<'a> {
pub(super) min_thickness: f32,
pub(super) placement: Placement,
pub(super) label_spacing: Rangef,
pub(super) tick_label_color: Option<Color32>,
pub(super) tick_label_font: Option<FontId>,
}

impl<'a> AxisHints<'a> {
Expand Down Expand Up @@ -89,6 +92,8 @@ impl<'a> AxisHints<'a> {
Axis::X => Rangef::new(60.0, 80.0), // labels can get pretty wide
Axis::Y => Rangef::new(20.0, 30.0), // text isn't very high
},
tick_label_color: None,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the default?
maybe set it to a default which we already use

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else use Default::default()

Copy link
Copy Markdown
Contributor Author

@TommiKabelitz TommiKabelitz Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the default is whatever is in ui.visuals().text_color() when the plot is actually rendered because this function

/// Determine a color from a 0-1 strength value.
pub fn color_from_strength(ui: &Ui, strength: f32) -> Color32 {
    let base_color = ui.visuals().text_color();
    base_color.gamma_multiply(strength.sqrt())
}

is called to determine the color when the plot is shown.

I am still just calling this function when color is None so the behavior is not changing. I could potentially move the default to the instantiation of AxisHints, but that would change the behavior in cases where ui.visuals().text_color() is changed between creation of the axes and showing the plot.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me

tick_label_font: None,
}
}

Expand Down Expand Up @@ -157,6 +162,20 @@ impl<'a> AxisHints<'a> {
self.label_spacing = range.into();
self
}

/// Set the color of the axis tick labels.
#[inline]
pub fn tick_label_color(mut self, color: impl Into<Color32>) -> Self {
self.tick_label_color = Some(color.into());
self
}

/// Set the font of the axis tick labels.
#[inline]
pub fn tick_label_font(mut self, font: FontId) -> Self {
self.tick_label_font = Some(font);
self
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -274,8 +293,15 @@ impl<'a> AxisWidget<'a> {
// Fade in labels as they get further apart:
let strength = remap_clamp(spacing_in_points, label_spacing, 0.0..=1.0);

let text_color = colors::color_from_strength(ui, strength);
let galley = painter.layout_no_wrap(text, font_id.clone(), text_color);
let text_color = if let Some(color) = self.hints.tick_label_color {
color.gamma_multiply(strength.sqrt())
} else {
super::color_from_strength(ui, strength)
};

let label_font_id = self.hints.tick_label_font.clone().unwrap_or_else(|| font_id.clone());

let galley = painter.layout_no_wrap(text, label_font_id, text_color);
let galley_size = match axis {
Axis::X => galley.size(),
Axis::Y => galley.size() + 2.0 * SIDE_MARGIN * Vec2::X,
Expand Down
Loading