1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

[example_web] show loading of an image

Required some redesign of `TextureAllocator` as well as
some improvements to the fetch API.
This commit is contained in:
Emil Ernerfeldt
2020-11-18 21:38:29 +01:00
parent 90cecace0c
commit c6ce0b9e8c
16 changed files with 645 additions and 277 deletions

View File

@@ -77,12 +77,19 @@ pub struct AppOutput {
}
pub trait TextureAllocator {
/// Allocate a user texture (EXPERIMENTAL!)
fn new_texture_srgba_premultiplied(
/// A.locate a new user texture.
fn alloc(&mut self) -> crate::TextureId;
/// Set or change the pixels of a user texture.
fn set_srgba_premultiplied(
&mut self,
id: crate::TextureId,
size: (usize, usize),
pixels: &[crate::Srgba],
) -> crate::TextureId;
srgba_pixels: &[crate::Srgba],
);
/// Free the given texture.
fn free(&mut self, id: crate::TextureId);
}
/// A place where you can store custom data in a way that persists when you restart the app.

View File

@@ -41,7 +41,7 @@ impl ColorTest {
ui.label("Use a color picker to ensure this color is (255, 165, 0) / #ffa500");
ui.wrap(|ui| {
ui.style_mut().spacing.item_spacing.y = 0.0; // No spacing between gradients
let g = Gradient::one_color(Srgba::new(255, 165, 0, 255));
let g = Gradient::one_color(Srgba::from_rgb(255, 165, 0));
self.vertex_gradient(ui, "orange rgb(255, 165, 0) - vertex", WHITE, &g);
self.tex_gradient(
ui,
@@ -125,7 +125,7 @@ impl ColorTest {
ui,
tex_allocator,
RED,
(TRANSPARENT, Srgba::new(0, 0, 255, 0)),
(TRANSPARENT, Srgba::from_rgba_premultiplied(0, 0, 255, 0)),
);
ui.separator();
@@ -367,7 +367,9 @@ impl TextureManager {
let pixels = gradient.to_pixel_row();
let width = pixels.len();
let height = 1;
tex_allocator.new_texture_srgba_premultiplied((width, height), &pixels)
let id = tex_allocator.alloc();
tex_allocator.set_srgba_premultiplied(id, (width, height), &pixels);
id
})
}
}

View File

@@ -24,15 +24,41 @@ impl std::ops::IndexMut<usize> for Srgba {
}
}
// TODO: remove ?
pub const fn srgba(r: u8, g: u8, b: u8, a: u8) -> Srgba {
Srgba::new(r, g, b, a)
Srgba::from_rgba_premultiplied(r, g, b, a)
}
impl Srgba {
#[deprecated = "Use from_rgb(..), from_rgba_premultiplied(..) or from_srgba_unmultiplied(..)"]
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self([r, g, b, a])
}
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self([r, g, b, 255])
}
pub const fn from_rgb_additive(r: u8, g: u8, b: u8) -> Self {
Self([r, g, b, 0])
}
/// From `sRGBA` with premultiplied alpha.
pub const fn from_rgba_premultiplied(r: u8, g: u8, b: u8, a: u8) -> Self {
Self([r, g, b, a])
}
/// From `sRGBA` WITHOUT premultiplied alpha.
pub fn from_rgba_unmultiplied(r: u8, g: u8, b: u8, a: u8) -> Self {
if a == 255 {
Self::from_rgba_premultiplied(r, g, b, a) // common-case optimization
} else {
Rgba::from(Self::from_rgb(r, g, b))
.multiply(a as f32 / 255.0)
.into()
}
}
pub const fn gray(l: u8) -> Self {
Self([l, l, l, 255])
}
@@ -476,7 +502,7 @@ fn test_hsv_roundtrip() {
for r in 0..=255 {
for g in 0..=255 {
for b in 0..=255 {
let srgba = Srgba::new(r, g, b, 255);
let srgba = Srgba::from_rgb(r, g, b);
let hsva = Hsva::from(srgba);
assert_eq!(srgba, Srgba::from(hsva));
}