Skip to content

Consider traits that apply to all pixel variants (where appropriate) #36

@antonmarsden

Description

@antonmarsden

I'd like to use this library to define algorithms that work for any RGB[A] pixel arrangement. However, there doesn't appear to be a generic way to achieve this without using macros. Traits could be used to provide such capability. For example:

pub trait RgbPixel<T> {
    fn red(&self) -> T;
    fn green(&self) -> T;
    fn blue(&self) -> T;
    fn set_red(&mut self, r: T);
    fn set_green(&mut self, g: T);
    fn set_blue(&mut self, b: T);

    fn rgb(&self) -> (T, T, T);
    fn set_rgb(&mut self, rgb: (T, T, T));
}

You might also have an AlphaPixel trait.

Example impl for RGBA:

/// Example impl for RGBA
impl<T : Copy> RgbPixel<T> for RGBA<T> {
    fn red(&self) -> T { self.r }
    fn green(&self) -> T { self.g }
    fn blue(&self) -> T { self.b }
    fn set_red(&mut self, r: T) { self.r = r; }
    fn set_green(&mut self, g: T) { self.g = g; }
    fn set_blue(&mut self, b: T) { self.b = b; }
    fn rgb(&self) -> (T, T, T) { (self.r, self.g, self.b) }
    fn set_rgb(&mut self, rgb: (T, T, T)) {
        self.r = rgb.0;
        self.g = rgb.1;
        self.b = rgb.2;
    }
}

I can implement this outside of the rgb crate, but was wondering whether you'd consider making it part of the core library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions