Allow tinting images grayscale
This commit is contained in:
parent
1816ab95a0
commit
d385bc9cce
6 changed files with 20 additions and 5 deletions
|
@ -3,9 +3,8 @@ use anyhow::anyhow;
|
||||||
use image::{Bgra, ImageBuffer};
|
use image::{Bgra, ImageBuffer};
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
cmp::Ordering,
|
|
||||||
fmt,
|
fmt,
|
||||||
hash::{Hash, Hasher},
|
hash::Hash,
|
||||||
sync::atomic::{AtomicUsize, Ordering::SeqCst},
|
sync::atomic::{AtomicUsize, Ordering::SeqCst},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ use util::ResultExt;
|
||||||
pub struct Img<S> {
|
pub struct Img<S> {
|
||||||
style: RefinementCascade<Style>,
|
style: RefinementCascade<Style>,
|
||||||
uri: Option<SharedString>,
|
uri: Option<SharedString>,
|
||||||
|
grayscale: bool,
|
||||||
state_type: PhantomData<S>,
|
state_type: PhantomData<S>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +17,7 @@ pub fn img<S>() -> Img<S> {
|
||||||
Img {
|
Img {
|
||||||
style: RefinementCascade::default(),
|
style: RefinementCascade::default(),
|
||||||
uri: None,
|
uri: None,
|
||||||
|
grayscale: false,
|
||||||
state_type: PhantomData,
|
state_type: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +27,11 @@ impl<S> Img<S> {
|
||||||
self.uri = Some(uri.into());
|
self.uri = Some(uri.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn grayscale(mut self, grayscale: bool) -> Self {
|
||||||
|
self.grayscale = grayscale;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: 'static> Element for Img<S> {
|
impl<S: 'static> Element for Img<S> {
|
||||||
|
@ -64,7 +71,7 @@ impl<S: 'static> Element for Img<S> {
|
||||||
.now_or_never()
|
.now_or_never()
|
||||||
.and_then(ResultExt::log_err)
|
.and_then(ResultExt::log_err)
|
||||||
{
|
{
|
||||||
cx.paint_image(bounds, order, data, false)?;
|
cx.paint_image(bounds, order, data, self.grayscale)?;
|
||||||
} else {
|
} else {
|
||||||
log::warn!("image not loaded yet");
|
log::warn!("image not loaded yet");
|
||||||
// cx.spawn(|this, mut cx| async move {
|
// cx.spawn(|this, mut cx| async move {
|
||||||
|
|
|
@ -191,6 +191,12 @@ fragment float4 polychrome_sprite_fragment(
|
||||||
float clip_distance = quad_sdf(input.position.xy, sprite.content_mask.bounds,
|
float clip_distance = quad_sdf(input.position.xy, sprite.content_mask.bounds,
|
||||||
sprite.content_mask.corner_radii);
|
sprite.content_mask.corner_radii);
|
||||||
float4 color = sample;
|
float4 color = sample;
|
||||||
|
if (sprite.grayscale) {
|
||||||
|
float grayscale = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
|
||||||
|
color.r = grayscale;
|
||||||
|
color.g = grayscale;
|
||||||
|
color.b = grayscale;
|
||||||
|
}
|
||||||
color.a *= saturate(0.5 - clip_distance);
|
color.a *= saturate(0.5 - clip_distance);
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
|
@ -276,6 +276,7 @@ pub struct PolychromeSprite {
|
||||||
pub bounds: Bounds<ScaledPixels>,
|
pub bounds: Bounds<ScaledPixels>,
|
||||||
pub content_mask: ScaledContentMask,
|
pub content_mask: ScaledContentMask,
|
||||||
pub tile: AtlasTile,
|
pub tile: AtlasTile,
|
||||||
|
pub grayscale: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ord for PolychromeSprite {
|
impl Ord for PolychromeSprite {
|
||||||
|
|
|
@ -305,6 +305,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
bounds,
|
bounds,
|
||||||
content_mask,
|
content_mask,
|
||||||
tile,
|
tile,
|
||||||
|
grayscale: false,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -379,6 +380,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
bounds,
|
bounds,
|
||||||
content_mask,
|
content_mask,
|
||||||
tile,
|
tile,
|
||||||
|
grayscale,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::theme::{theme, Theme};
|
use crate::theme::{theme, Theme};
|
||||||
use gpui3::{
|
use gpui3::{
|
||||||
div, img, svg, view, AppContext, ArcCow, Context, Element, IntoAnyElement, ParentElement,
|
div, img, svg, view, AppContext, Context, Element, IntoAnyElement, ParentElement, ScrollState,
|
||||||
ScrollState, SharedString, StyleHelpers, View, ViewContext, WindowContext,
|
SharedString, StyleHelpers, View, ViewContext, WindowContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct CollabPanel {
|
pub struct CollabPanel {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue