Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pr0methean opened this issue Apr 29, 2023 · 0 comments
Open

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

Pr0methean opened this issue Apr 29, 2023 · 0 comments

Comments

@Pr0methean
Copy link

Pr0methean commented Apr 29, 2023

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);
        }
    }
}
@Pr0methean Pr0methean changed the title Color types should implement Clone, Debug, Display, Eq, Hash, Ord Color types should implement Debug, Display, Eq, Hash, Ord Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant