Skip to content

Commit

Permalink
font: port over radial gradient fills
Browse files Browse the repository at this point in the history
  • Loading branch information
wez committed Aug 22, 2023
1 parent c41ae92 commit 7af6115
Show file tree
Hide file tree
Showing 3 changed files with 389 additions and 14 deletions.
39 changes: 39 additions & 0 deletions color-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ impl SrgbaPixel {
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
pub struct SrgbaTuple(pub f32, pub f32, pub f32, pub f32);

impl SrgbaTuple {
pub fn premultiply(self) -> Self {
let SrgbaTuple(r, g, b, a) = self;
Self(r * a, g * a, b * a, a)
}

pub fn demultiply(self) -> Self {
let SrgbaTuple(r, g, b, a) = self;
if a != 0. {
Self(r / a, g / a, b / a, a)
} else {
self
}
}

pub fn interpolate(self, other: Self, k: f64) -> Self {
let k = k as f32;

let SrgbaTuple(r0, g0, b0, a0) = self.premultiply();
let SrgbaTuple(r1, g1, b1, a1) = other.premultiply();

let r = SrgbaTuple(
r0 + k * (r1 - r0),
g0 + k * (g1 - g0),
b0 + k * (b1 - b0),
a0 + k * (a1 - a0),
);

r.demultiply()
}
}

impl ToDynamic for SrgbaTuple {
fn to_dynamic(&self) -> Value {
self.to_string().to_dynamic()
Expand All @@ -237,6 +269,13 @@ impl FromDynamic for SrgbaTuple {
}
}

impl From<SrgbaPixel> for SrgbaTuple {
fn from(pixel: SrgbaPixel) -> SrgbaTuple {
let (r, g, b, a) = pixel.as_srgba_tuple();
SrgbaTuple(r, g, b, a)
}
}

impl From<(f32, f32, f32, f32)> for SrgbaTuple {
fn from((r, g, b, a): (f32, f32, f32, f32)) -> SrgbaTuple {
SrgbaTuple(r, g, b, a)
Expand Down
8 changes: 6 additions & 2 deletions wezterm-font/src/hbwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,12 @@ impl ColorLine {
color_stops: color_stops
.into_iter()
.map(|stop| ColorStop {
offset: stop.offset,
color: hb_color_to_srgba_pixel(stop.color),
offset: stop.offset.into(),
color: if stop.is_foreground != 0 {
SrgbaPixel::rgba(0xff, 0xff, 0xff, 0xff)
} else {
hb_color_to_srgba_pixel(stop.color)
},
})
.collect(),
extend: hb_extend_to_cairo(extend),
Expand Down
Loading

0 comments on commit 7af6115

Please sign in to comment.