kornelski / rust-rgb

struct RGB for sharing pixels between crates
https://lib.rs/rgb
MIT License
98 stars 19 forks source link

map_colors is not a full replacement for map_rgb #137

Open kornelski opened 1 month ago

kornelski commented 1 month ago

map_rgb on RGBA<T, A> uses a From for the A type to make it match T, allowing code like px.map_colors(|c| c as f32 * px.a as f32) to just work and update alpha.

map_colors is just for the colors, and results in an annoying RGBA<f32, u8>.

https://docs.rs/rgb/0.8.90-alpha.2/rgb/trait.HetPixel.html#tymethod.map_colors

Should it also convert alpha? Should there be another method?

ripytide commented 4 weeks ago

I'm a bit confused by the use-case, can you provide an example using concrete types since your example uses generic types but then also does as casts so I'm assuming it's not generic?

How about this?

use rgb::{HetPixel, Pixel, Rgba};

fn main() {
    let px: Rgba<u8> = Rgba {
        r: 0,
        g: 10,
        b: 10,
        a: 255,
    };

    let px = px.map(f32::from);
    let px = px.map_colors(|c| c * px.a);
    print!("{px}");
}