Skip to content

Color types should implement Debug, Display, Eq, Hash, Ord #80

@Pr0methean

Description

@Pr0methean

It would be handy if the color types implemented Debug, Display, Eq, Hash and Ord, rather than my having to create a wrapper that does, and also if the non-premultiplied ones treated all fully-transparent "colors" as equal. For Clone, Copy, Debug, Eq the derived implementations are adequate; here are my implementations of the others. The Ord, PartialEq, Display, Hash implementations should be possible to optimize noticeably for the types that are backed by a u32 rather than by four u8s.

impl PartialOrd for ComparableColor {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ComparableColor {
    fn cmp(&self, other: &Self) -> Ordering {
        if self.alpha == 0 && other.alpha == 0 {
            Ordering::Equal
        }
        let mut ordering = self.alpha.cmp(*(other.alpha));
        if ordering == Ordering::Equal {
            ordering = self.blue.cmp(*(other.blue));
            if ordering == Ordering::Equal {
                ordering = self.green.cmp(*(other.green));
                if ordering == Ordering::Equal {
                    ordering = self.red.cmp(*(other.red));
                }
            }
        }
        ordering
    }
}

impl Display for ComparableColor {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.alpha == 0 {
            write!(f, "transparent")
        } else {
            write!(f, "#{:02x}{:02x}{:02x}{:02x}", self.red, self.green, self.blue, self.alpha)
        }
    }
}

impl PartialEq<Self> for ComparableColor {
    fn eq(&self, other: &Self) -> bool {
        (self.alpha == 0 && other.alpha == 0) ||
            (self.red == other.red
                && self.green == other.green
                && self.blue == other.blue
                && self.alpha == other.alpha)
    }
}

impl Hash for ComparableColor {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.alpha.hash(state);
        if self.alpha != 0 {
            self.red.hash(state);
            self.green.hash(state);
            self.blue.hash(state);
        }
    }
}

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