linebender / linebender/tiny-skia

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

Open
#80 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.6k
Forks
99
Avg merge
2h 52m
Merged PRs (30d)
1

Description

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 `u8`s.
```
impl PartialOrd for ComparableColor {
fn partial_cmp(&self, other: &Self) -> Option {
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 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(&self, state: &mut H) {
self.alpha.hash(state);
if self.alpha != 0 {
self.red.hash(state);
self.green.hash(state);
self.blue.hash(state);
}
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

No files, tests, or entry points are named. Start by locating the color types and checking which are backed by a u32 versus four u8s, then review their existing trait implementations. Done means the requested traits are implemented for the color types, fully transparent non-premultiplied colors compare equal, and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
computer-graphics
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.