Skip to content

Make color generic over premul types #192

@sagudev

Description

@sagudev

Per my comment in #190 (comment).

Sketch:

// similar to colorspacetag
enum AlphaType {
    Alpha,
    PremultipliedAlpha,
}

struct Alpha;
struct PremultipliedAlpha;

// for const generic
// alpha trait is like colorspace trait
impl AlphaTrait for Alpha {
    const ALPHA = AlphaType::Alpha;
}

impl AlphaTrait for AlphaPremultiplied {
    const ALPHA = AlphaType::AlphaPremultiplied;
}

pub struct AlphaColor<CS, A> {
    /// The components, which may be manipulated directly.
    ///
    /// The interpretation of the first three components depends on the color
    /// space. The fourth component is separate alpha.
    pub components: [f32; 4],
    /// The color space.
    pub cs: PhantomData<CS>,
    pub alpha: PhantomData<A>,
}

pub struct DynamicColor {
    pub alpha: AlpahType,
    /// The color space.
    pub cs: ColorSpaceTag,
    /// The state of this color, tracking whether it has missing components and how it was
    /// constructed. See the documentation of [`Flags`] for more information.
    pub flags: Flags,
    /// The components.
    ///
    /// The first three components are interpreted according to the
    /// color space tag. The fourth component is alpha, interpreted
    /// as separate alpha.
    pub components: [f32; 4],
}

PremulColor would be AlphaColor<_, PremiltipliedAlpha> so we can share more impls and DynamicColor is now also dynamic over AlphaType.

Motivation:

This would be break change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions