kornelski / rust-rgb

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

Should 1-channel Gray still use to/from [v; 1]? #115

Closed kornelski closed 4 months ago

kornelski commented 4 months ago

It seems much more useful to have From/Into that wrap/unwrap Gray newtype than to work with 1-channel arrays.

I don't think these arrays are usable in a generic context, so consistency here may not be necessary?

ripytide commented 4 months ago

I'm a bit confused by what you mean but if you mean impl From<Gray<u8>> for u8 vs impl From<Gray<u8>> for [u8; 1], then my question would be why not offer both?

Related to this is the question of whether we implement the pixel traits for the primitive types? Such as: impl Pixel for u8

kornelski commented 4 months ago

Can't do both as generic due to overlapping implementations.

kornelski commented 4 months ago

Pixel for u8 would be nice for working with grayscale, but I'm afraid it would more likely cause confusion in code that returns all image data as blobs of unsigned char.

ripytide commented 4 months ago

Ah ok, yeah the overlapping implementations is really annoying. The pros of [T; 1] are consistency with the other pixel types which might be usable in a generic context:

use rgb::Pixel;

fn test<const N: usize, P>(array: [P::Component; N])
where
    P: Pixel,
    P: From<[P::Component; N]>,
{
    //no panic
    let x = P::from(array);
    //potential panic
    let x = P::try_from_components(array).unwrap();
}
kornelski commented 4 months ago

ok, let's keep arrays.