Skip to content

Commit

Permalink
Use leftover bits in HighestBits selector
Browse files Browse the repository at this point in the history
  • Loading branch information
gennyble committed Jun 10, 2024
1 parent a57cb34 commit 36ccf4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
12 changes: 6 additions & 6 deletions squash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ license = "ISC"
description = "CLI tool for quantizing colours"
repository = "https://github.com/gennyble/colorsquash/tree/main/squash"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# the meat 'o the thing! the meaning behind it all
colorsquash = { path = "..", version = "0.2.0", default-features = false, features = [
"gifed",
] }
[dependencies.colorsquash]
path = ".."
version = "0.2.0"
default-features = false # `kmeans` crate currently very broken
features = ["gifed"]

[dependencies]
# just useful tools for writing binaries
anyhow = "1.0.75"
camino = "1.1.6"
Expand Down
21 changes: 16 additions & 5 deletions src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,28 @@ pub struct HighestBits {}

impl Selector for HighestBits {
fn select(&mut self, max_colors: usize, image: ImageData) -> Vec<RGB8> {
let max_bits = max_colors.next_power_of_two().ilog2() / 3;
let shift = 8 - max_bits;
let bits = max_colors.next_power_of_two().ilog2();
let leftover = bits % 3;
let shift = 8 - (bits / 3);

//TODO: gen- we're taking red/green here because, as i remember, they
// are the colours to which we are most sensetive? but it would be cool
// if this was selectable
let (rshift, gshift, bshift) = match leftover {
0 => (shift, shift, shift),
1 => (shift, shift - 1, shift),
2 => (shift - 1, shift - 1, shift),
_ => unreachable!(),
};

image
.0
.iter()
.map(|color| {
RGB8::new(
color.r >> shift << shift,
color.g >> shift << shift,
color.b >> shift << shift,
color.r >> rshift << rshift,
color.g >> gshift << gshift,
color.b >> bshift << bshift,
)
})
.collect::<HashSet<_>>()
Expand Down

0 comments on commit 36ccf4d

Please sign in to comment.