use gpui::{Background, BoxShadow, Hsla, Rgba, linear_color_stop, linear_gradient, point, px};
use crate::color::{channels, rgba, to_hsla};
pub fn shade(color: Rgba, amount: f32) -> Rgba {
let target = if amount >= 0.0 { 1.0 } else { 0.0 };
let t = amount.abs().clamp(0.0, 1.0);
let [r, g, b, a] = channels(color);
rgba(
r + (target - r) * t,
g + (target - g) * t,
b + (target - b) * t,
a,
)
}
fn white(alpha: f32) -> Hsla {
to_hsla(rgba(1.0, 1.0, 1.0, alpha))
}
fn black(alpha: f32) -> Hsla {
to_hsla(rgba(0.0, 0.0, 0.0, alpha))
}
fn luminance(color: Rgba) -> f32 {
let [r, g, b, _] = channels(color);
0.2126 * r + 0.7152 * g + 0.0722 * b
}
pub fn lit(base: Rgba, lift: f32) -> Background {
let (top, bottom) = lit_stops(base, lift);
linear_gradient(
180.0,
linear_color_stop(top, 0.0),
linear_color_stop(bottom, 1.0),
)
}
pub fn lit_at(base: Rgba, lift: f32, opacity: f32) -> Background {
lit(base, lift).opacity(opacity.clamp(0.0, 1.0))
}
pub fn lit_mix(flat: Rgba, base: Rgba, lift: f32, on: f32) -> Background {
let (top, bottom) = lit_stops(base, lift);
linear_gradient(
180.0,
linear_color_stop(crate::color::lerp(flat, top, on), 0.0),
linear_color_stop(crate::color::lerp(flat, bottom, on), 1.0),
)
}
pub const GROUND_LIFT: f32 = 0.035;
pub fn ground(backdrop: Rgba) -> Background {
lit(backdrop, GROUND_LIFT)
}
pub fn ground_at(backdrop: Rgba, fraction: f32) -> Rgba {
let (top, bottom) = lit_stops(backdrop, GROUND_LIFT);
crate::color::lerp(top, bottom, fraction.clamp(0.0, 1.0))
}
pub fn lit_stops(base: Rgba, lift: f32) -> (Rgba, Rgba) {
let bottom = lift * (0.5 + 0.5 * luminance(base));
(shade(base, lift), shade(base, -bottom))
}
pub fn rim(fill: Rgba, dark: bool) -> Rgba {
shade(fill, if dark { -0.35 } else { -0.2 })
}
pub fn raised(dark: bool) -> Vec<BoxShadow> {
let mut shadows = vec![
BoxShadow {
color: white(if dark { 0.08 } else { 0.55 }),
offset: point(px(0.0), px(1.0)),
blur_radius: px(0.0),
spread_radius: px(0.0),
inset: true,
},
BoxShadow {
color: black(if dark { 0.22 } else { 0.1 }),
offset: point(px(0.0), px(1.0)),
blur_radius: px(2.0),
spread_radius: px(0.0),
inset: false,
},
];
if !dark {
shadows.push(BoxShadow {
color: black(0.05),
offset: point(px(0.0), px(-1.0)),
blur_radius: px(0.0),
spread_radius: px(0.0),
inset: true,
});
}
shadows
}
pub fn recessed(dark: bool) -> Vec<BoxShadow> {
vec![BoxShadow {
color: black(if dark { 0.3 } else { 0.1 }),
offset: point(px(0.0), px(1.5)),
blur_radius: px(2.5),
spread_radius: px(0.0),
inset: true,
}]
}
pub fn panel(dark: bool) -> Vec<BoxShadow> {
vec![
BoxShadow {
color: white(if dark { 0.05 } else { 0.7 }),
offset: point(px(0.0), px(1.0)),
blur_radius: px(0.0),
spread_radius: px(0.0),
inset: true,
},
BoxShadow {
color: black(if dark { 0.25 } else { 0.09 }),
offset: point(px(0.0), px(2.0)),
blur_radius: px(10.0),
spread_radius: px(0.0),
inset: false,
},
]
}
pub fn faded(mut shadows: Vec<BoxShadow>, t: f32) -> Vec<BoxShadow> {
let t = t.clamp(0.0, 1.0);
for shadow in &mut shadows {
shadow.color.alpha *= t;
}
shadows
}
pub fn glow(color: Rgba, alpha: f32, radius: f32) -> BoxShadow {
BoxShadow {
color: to_hsla(crate::color::with_alpha(color, alpha)),
offset: point(px(0.0), px(0.0)),
blur_radius: px(radius),
spread_radius: px(radius * 0.25),
inset: false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fading_a_translucent_fill_preserves_its_opacity() {
let base = rgba(0.4, 0.5, 0.6, 0.25);
assert_eq!(lit_at(base, 0.1, 1.0), lit(base, 0.1));
let gpui::BackgroundKind::LinearGradient { stops, .. } = lit_at(base, 0.1, 0.5).kind()
else {
panic!("a lit fill is a gradient");
};
for stop in stops {
let color: Hsla = stop.color.into();
assert_eq!(color.alpha, 0.125);
}
}
}