diff --git a/crates/op-editor-ui/src/util.rs b/crates/op-editor-ui/src/util.rs index f872836e3..774bd045a 100644 --- a/crates/op-editor-ui/src/util.rs +++ b/crates/op-editor-ui/src/util.rs @@ -82,6 +82,29 @@ pub fn color_to_hex_with_alpha(c: Color) -> String { } } +/// A colour at `factor` of its current alpha — the Rust analogue of a Tailwind +/// `/NN` opacity modifier (e.g. `bg-muted/60`). RELATIVE: it MULTIPLIES the +/// existing alpha. For an absolute set, use `Color::with_alpha` instead. +pub fn alpha(c: Color, factor: f32) -> Color { + Color { + a: c.a * factor, + ..c + } +} + +/// `top` (a translucent colour) composited over the opaque `base`, yielding an +/// opaque colour — so a `bg-accent/30`-over-`bg-card` hover fill paints in a +/// single pass. +pub fn over(base: Color, top: Color) -> Color { + let a = top.a; + Color { + r: base.r * (1.0 - a) + top.r * a, + g: base.g * (1.0 - a) + top.g * a, + b: base.b * (1.0 - a) + top.b * a, + a: base.a, + } +} + /// Format a numeric property value for a panel input: drop a trailing `.0` /// for whole numbers, otherwise show two decimals. pub fn format_panel_number(value: f32) -> String { diff --git a/crates/op-editor-ui/src/widgets/git_panel_clone.rs b/crates/op-editor-ui/src/widgets/git_panel_clone.rs index fd48ea0a4..864d885e7 100644 --- a/crates/op-editor-ui/src/widgets/git_panel_clone.rs +++ b/crates/op-editor-ui/src/widgets/git_panel_clone.rs @@ -245,8 +245,5 @@ impl GitPanel<'_> { /// A colour at `factor` of its current alpha (Tailwind `/NN`). fn alpha(c: Color, factor: f32) -> Color { - Color { - a: c.a * factor, - ..c - } + crate::util::alpha(c, factor) } diff --git a/crates/op-editor-ui/src/widgets/git_panel_empty.rs b/crates/op-editor-ui/src/widgets/git_panel_empty.rs index 634e735ab..27bd7e996 100644 --- a/crates/op-editor-ui/src/widgets/git_panel_empty.rs +++ b/crates/op-editor-ui/src/widgets/git_panel_empty.rs @@ -347,21 +347,12 @@ impl GitPanel<'_> { /// A colour at `factor` of its current alpha — the Rust analogue of a /// Tailwind `/NN` opacity modifier (e.g. `bg-muted/60`). fn alpha(c: Color, factor: f32) -> Color { - Color { - a: c.a * factor, - ..c - } + crate::util::alpha(c, factor) } /// `top` (a translucent colour) composited over the opaque `base`, /// yielding an opaque colour — so a `bg-accent/30`-over-`bg-card` hover /// fill paints in a single pass. fn over(base: Color, top: Color) -> Color { - let a = top.a; - Color { - r: base.r * (1.0 - a) + top.r * a, - g: base.g * (1.0 - a) + top.g * a, - b: base.b * (1.0 - a) + top.b * a, - a: base.a, - } + crate::util::over(base, top) } diff --git a/crates/op-editor-ui/src/widgets/git_panel_menus.rs b/crates/op-editor-ui/src/widgets/git_panel_menus.rs index 113f1cbe6..f54dcbad3 100644 --- a/crates/op-editor-ui/src/widgets/git_panel_menus.rs +++ b/crates/op-editor-ui/src/widgets/git_panel_menus.rs @@ -898,10 +898,7 @@ impl GitPanel<'_> { /// A colour at `factor` of its current alpha (Tailwind `/NN`). fn alpha(c: Color, factor: f32) -> Color { - Color { - a: c.a * factor, - ..c - } + crate::util::alpha(c, factor) } /// Rough rendered width of a 12px label without a backend — CJK glyphs diff --git a/crates/op-editor-ui/src/widgets/git_panel_ready.rs b/crates/op-editor-ui/src/widgets/git_panel_ready.rs index 0c7fc0160..53adcdf84 100644 --- a/crates/op-editor-ui/src/widgets/git_panel_ready.rs +++ b/crates/op-editor-ui/src/widgets/git_panel_ready.rs @@ -963,10 +963,7 @@ impl GitPanel<'_> { /// A colour at `factor` of its current alpha (Tailwind `/NN`). fn alpha(c: Color, factor: f32) -> Color { - Color { - a: c.a * factor, - ..c - } + crate::util::alpha(c, factor) } /// The first whitespace-delimited token of an author name — TS diff --git a/crates/op-editor-ui/src/widgets/git_panel_ssh_keys.rs b/crates/op-editor-ui/src/widgets/git_panel_ssh_keys.rs index 330ac8210..7f44b0838 100644 --- a/crates/op-editor-ui/src/widgets/git_panel_ssh_keys.rs +++ b/crates/op-editor-ui/src/widgets/git_panel_ssh_keys.rs @@ -174,8 +174,5 @@ impl GitPanel<'_> { /// A colour at `factor` of its current alpha (Tailwind `/NN`). fn alpha(c: Color, factor: f32) -> Color { - Color { - a: c.a * factor, - ..c - } + crate::util::alpha(c, factor) } diff --git a/crates/op-editor-ui/src/widgets/git_panel_tracked_picker.rs b/crates/op-editor-ui/src/widgets/git_panel_tracked_picker.rs index e10589dcb..92870ae43 100644 --- a/crates/op-editor-ui/src/widgets/git_panel_tracked_picker.rs +++ b/crates/op-editor-ui/src/widgets/git_panel_tracked_picker.rs @@ -385,8 +385,5 @@ fn popup_anchor(popup: Rect) -> Rect { /// A colour at `factor` of its current alpha (Tailwind `/NN`). fn alpha(c: Color, factor: f32) -> Color { - Color { - a: c.a * factor, - ..c - } + crate::util::alpha(c, factor) }