Skip to content

Conversation

@TommiKabelitz
Copy link

@TommiKabelitz TommiKabelitz commented Nov 19, 2025

This allows specifying the color and font (through FontId) of the tick labels for each axis on a plot
image

All feedback and criticism of the API and implementation are much appreciated. I will note, to ensure that the fading of the tick label color is respected, I did the following

let text_color = if let Some(color) = self.hints.tick_label_color {
    color.gamma_multiply(strength.sqrt())
} else {
    super::color_from_strength(ui, strength)
};

and I don't love the duplication of the gamma_multiply, I just didn't want to touch the color_from_strength function without the all clear.

pub(super) placement: Placement,
pub(super) label_spacing: Rangef,
pub(super) tick_label_color: Option<egui::Color32>,
pub(super) tick_label_font: Option<egui::FontId>,
Copy link
Contributor

Choose a reason for hiding this comment

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

import egui::FontId directly

pub(super) min_thickness: f32,
pub(super) placement: Placement,
pub(super) label_spacing: Rangef,
pub(super) tick_label_color: Option<egui::Color32>,
Copy link
Contributor

Choose a reason for hiding this comment

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

import egui::Color32 directly

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
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
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
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
Contributor

Choose a reason for hiding this comment

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

works for me


/// Set the font of the axis tick labels.
///
/// To change the color of the tick labels see [`Self::tick_label_color`].
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment is not necessary for this func

/// As labels get close, they will fade in color until they become invisible. See
/// [`Self::label_spacing`].
///
/// To change the font of the tick labels see [`Self::tick_label_font`].
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment is not necessary for this func

let label_font_id = self
.hints
.tick_label_font
.clone()
Copy link
Contributor

Choose a reason for hiding this comment

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

2x clones?
Is there something else possible?

Copy link
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.

I could be mistaken, but I don't think so. Cloning FontId is pretty cheap as it is just an f32 and the FontFamily enum which is an Arc.

I think you need to clone in both cases as layout_no_wrap expects an owned FontId. When the font has not been specified we use the global one so you need to clone. When it is specified, you have to clone as we only have a reference to self and so cannot move it out of the shared reference.

The implications of changing layout_no_wrap to accept an owned value look to be significant at a glance.

@TommiKabelitz TommiKabelitz requested a review from bircni November 26, 2025 00:51
bircni

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to specify the color of the axis tick labels

2 participants