Skip to content

Commit 965ea9c

Browse files
gavrelinaemilk
andauthored
Add grid_color and grid_fade API (#235)
## Summary - **`grid_color(Color32)`** — sets the base color for grid lines independently of `text_color()`, while preserving the strength-based fading. - **`grid_fade(f32)`** — controls the contrast between dense and sparse grid lines. `0.0` = uniform (all lines same opacity), `0.5` = default (sqrt), `1.0` = maximum fade. The zoom-based density logic is always preserved. ## Motivation When embedding `egui_plot` in an application with its own design system, the grid color needs to be independent of text color. The current behavior derives grid color from `ui.visuals().text_color()`, which couples grid styling to text styling. Similarly, the strength curve (hardcoded `sqrt`) may not suit all visual designs — some applications want less contrast between grid levels, or uniform grid lines. ## Changes Single file change in `plot.rs`: - Added `grid_color: Option<Color32>` field with `.grid_color()` builder method - Added `grid_strength_exponent: f32` field (default `0.5`) with `.grid_fade()` builder method - `paint_grid_direction` uses the custom base color and exponent when set, falls back to existing behavior otherwise --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
1 parent c7feb89 commit 965ea9c

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

egui_plot/src/plot.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub struct Plot<'a> {
128128
show_grid: Vec2b,
129129
grid_spacing: Rangef,
130130
grid_spacers: [GridSpacer<'a>; 2],
131+
grid_color: Option<Color32>,
132+
grid_strength_exponent: f32,
131133
clamp_grid: bool,
132134

133135
sense: Sense,
@@ -180,6 +182,8 @@ impl<'a> Plot<'a> {
180182
show_grid: true.into(),
181183
grid_spacing: Rangef::new(8.0, 300.0),
182184
grid_spacers: [crate::grid::log_grid_spacer(10), crate::grid::log_grid_spacer(10)],
185+
grid_color: None,
186+
grid_strength_exponent: 0.5,
183187
clamp_grid: false,
184188

185189
sense: egui::Sense::click_and_drag(),
@@ -628,6 +632,33 @@ impl<'a> Plot<'a> {
628632
self
629633
}
630634

635+
/// Set the base color for grid lines.
636+
///
637+
/// By default, grid lines derive their color from [`egui::Visuals::text_color`].
638+
/// This override lets you control the grid color independently of text styling.
639+
/// The color is still modulated by line strength (fading for denser grid lines).
640+
#[inline]
641+
pub fn grid_color(mut self, color: Color32) -> Self {
642+
self.grid_color = Some(color);
643+
self
644+
}
645+
646+
/// Controls the contrast between dense and sparse grid lines.
647+
///
648+
/// - `0.0`: all visible grid lines have the same opacity (uniform).
649+
/// - `0.5`: default — moderate fade for denser lines.
650+
/// - `1.0`: maximum fade — dense lines are much fainter than sparse ones.
651+
///
652+
/// The zoom-based density logic is always preserved; this only controls
653+
/// how much the opacity varies between grid levels.
654+
///
655+
/// Default: `0.5`.
656+
#[inline]
657+
pub fn grid_fade(mut self, fade: f32) -> Self {
658+
self.grid_strength_exponent = fade;
659+
self
660+
}
661+
631662
/// Add this plot to an axis link group so that this plot will share the
632663
/// bounds with other plots in the same group. A plot cannot belong to
633664
/// more than one axis group.
@@ -1487,7 +1518,8 @@ impl<'a> Plot<'a> {
14871518

14881519
let line_strength = remap_clamp(spacing_in_points, self.grid_spacing, 0.0..=1.0);
14891520

1490-
let line_color = crate::colors::color_from_strength(ui, line_strength);
1521+
let base_color = self.grid_color.unwrap_or_else(|| ui.visuals().text_color());
1522+
let line_color = base_color.gamma_multiply(line_strength.powf(self.grid_strength_exponent));
14911523

14921524
let mut p0 = pos_in_gui;
14931525
let mut p1 = pos_in_gui;

0 commit comments

Comments
 (0)