okaneco / kmeans-colors

k-means clustering library and binary to find dominant colors in images
Apache License 2.0
133 stars 9 forks source link

Trait not implemented while using palette 0.6 #48

Closed davisschenk closed 2 years ago

davisschenk commented 2 years ago

Hello, I've noticed an interesting bug when attempting to use this library with the palette feature enabled. Everything appears to work great when using palette 0.6 but when I attempt to use palette 0.5 I get trait not implemented for Calculate and Hamerly on Lab.

This is main.rs

use kmeans_colors::get_kmeans_hamerly;
use palette::{Lab, Pixel, Srgb};

fn main() {
    let bytes = [0u8, 0, 255, 0, 255, 128];

    let lab: Vec<Lab> = Srgb::from_raw_slice(&bytes)
        .iter()
        .map(|x| x.into_format().into())
        .collect();

    let run_result = get_kmeans_hamerly(8, 20, 5.0, true, &lab, 0);
    println!("{:?}", run_result)
}

and Cargo.toml

[package]
name = "test-kmeans"
version = "0.1.0"
edition = "2021"

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

[dependencies.kmeans_colors]
version = "0.4.0"
features = ["palette_color"]
default-features = false

[dependencies.palette]
version = "0.5"
default-features = false
features = ["std"]

This works well, but once you change the palette version to 0.6 and update the code for generating the vector of colors it no longer functions as expected.

okaneco commented 2 years ago

Thanks for the issue and sorry for the confusion. I never got around to publishing a version of the crate with palette 0.6 support even though it's been sitting in the repo. I've pushed that out after #49 and the following runs now with kmeans_colors = 0.5.0.

main.rs and Cargo.toml ```rust // main.rs use kmeans_colors::get_kmeans_hamerly; use palette::{IntoColor, Lab, Pixel, Srgb}; fn main() { let bytes = [0u8, 0, 255, 0, 255, 128]; let lab: Vec = Srgb::from_raw_slice(&bytes) .iter() .map(|x| x.into_format().into_color()) .collect(); let run_result = get_kmeans_hamerly(8, 20, 5.0, true, &lab, 0); println!("{:?}", run_result) } ``` ```toml # Cargo.toml [dependencies.kmeans_colors] version = "0.5.0" features = ["palette_color"] default-features = false [dependencies.palette] version = "0.6" default-features = false features = ["std"] ```

Usually when you get that kind of error about a trait not being implemented that you know exists, there's a good chance it's an issue like this with conflicting crate versions. You can check the Cargo.lock file of your project and see what version a crate is dependent on. Since the third party crate is part of the public API of this one and you can't mix multiple versions of a crate (palette 0.5 and palette 0.6) in a project, it's good that you filed this issue so I could update. As you noticed, it worked with 0.5 but not 0.6 which confused me as well for a few minutes until I realized what happened.

davisschenk commented 2 years ago

Thanks! Got everything working now! Love the project and appreciate the explanation on why it wasn't working and the tip for looking at Cargo.lock!