Skip to content

Commit 5902eb9

Browse files
gavrelinaemilk
authored andcommitted
Add grid_color and grid_fade API (emilk#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 c008e48 commit 5902eb9

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,
@@ -182,6 +184,8 @@ impl<'a> Plot<'a> {
182184
show_grid: true.into(),
183185
grid_spacing: Rangef::new(8.0, 300.0),
184186
grid_spacers: [crate::grid::log_grid_spacer(10), crate::grid::log_grid_spacer(10)],
187+
grid_color: None,
188+
grid_strength_exponent: 0.5,
185189
clamp_grid: false,
186190

187191
sense: egui::Sense::click_and_drag(),
@@ -637,6 +641,33 @@ impl<'a> Plot<'a> {
637641
self
638642
}
639643

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

14971528
let line_strength = remap_clamp(spacing_in_points, self.grid_spacing, 0.0..=1.0);
14981529

1499-
let line_color = crate::colors::color_from_strength(ui, line_strength);
1530+
let base_color = self.grid_color.unwrap_or_else(|| ui.visuals().text_color());
1531+
let line_color = base_color.gamma_multiply(line_strength.powf(self.grid_strength_exponent));
15001532

15011533
let mut p0 = pos_in_gui;
15021534
let mut p1 = pos_in_gui;

0 commit comments

Comments
 (0)