Coder Social home page Coder Social logo

Upgrade to palette 0.7 about kmeans-colors HOT 5 CLOSED

djc avatar djc commented on July 17, 2024 2
Upgrade to palette 0.7

from kmeans-colors.

Comments (5)

okaneco avatar okaneco commented on July 17, 2024 2

Thanks for looking into this. I started working on this before I last commented but kept running into the cascading errors.

Real seems to be the replacement for Component and FloatComponent in some ways with their shared behavior extracted to the traits in palette::num. I'll have to review the PRs and related issues for the reasoning behind the changes. I was probably going to open discussion topics on the palette repo for some aspects of the migration. There might be gaps in the current traits that can be augmented for future palette versions (such as no PartialOrd to sort Real by, or a ToPrimitive like in num-traits to convert from Real to f32/f64).

I believe the T bounds will look something this in order to do arithmetic with the values and comparisons.

impl<Wp, T> Calculate for Lab<Wp, T>
where
    Wp: palette::white_point::WhitePoint<T>,
    T: palette::num::Real + palette::num::Arithmetics + palette::num::PartialCmp,

Anywhere with raw floats needs to use T::from_f64.

-            let mut l = 0.0;
+            let mut l = T::from_f64(0.0);

But we still run into roadblocks because of how kmeans_colors::Calculate is declared. The traits in this crate might need to be parameterized for the output of functions that currently return f32 or more thoroughly redesigned.

pub trait Calculate: Sized {
    fn get_closest_centroid(buffer: &[Self], centroids: &[Self], indices: &mut Vec<u8>);
    fn recalculate_centroids(rng: &mut impl Rng, buf: &[Self], centroids: &mut [Self], indices: &[u8]);
    fn check_loop(centroids: &[Self], old_centroids: &[Self]) -> f32;
    fn create_random(rng: &mut impl Rng) -> Self;
    fn difference(c1: &Self, c2: &Self) -> f32;
}

from kmeans-colors.

okaneco avatar okaneco commented on July 17, 2024 1

I updated palette in #52 and published 0.6 of this crate to crates.io.

from kmeans-colors.

djc avatar djc commented on July 17, 2024 1

Thanks!

from kmeans-colors.

okaneco avatar okaneco commented on July 17, 2024

I'll try to work on that soon. Quite a bit changed from palette 0.6 to palette 0.7 so it's not quite as simple as bumping the version number.

from kmeans-colors.

djc avatar djc commented on July 17, 2024

Yup. I started on a PR but can't quite figure out how to replace Component.

diff --git a/src/colors/kmeans.rs b/src/colors/kmeans.rs
index e857d4f..a34b425 100644
--- a/src/colors/kmeans.rs
+++ b/src/colors/kmeans.rs
@@ -1,12 +1,14 @@
 #[cfg(feature = "palette_color")]
 use palette::{Lab, Srgb};
+#[cfg(feature = "palette_color")]
+use palette::num::Real;
 
 use rand::Rng;
 
 use crate::kmeans::{Calculate, Hamerly, HamerlyCentroids, HamerlyPoint};
 
 #[cfg(feature = "palette_color")]
-impl<Wp: palette::white_point::WhitePoint> Calculate for Lab<Wp> {
+impl<T: Real, Wp: palette::white_point::WhitePoint<T>> Calculate for Lab<Wp> {
     fn get_closest_centroid(lab: &[Lab<Wp>], centroids: &[Lab<Wp>], indices: &mut Vec<u8>) {
         for color in lab.iter() {
             let mut index = 0;
@@ -162,7 +164,7 @@ impl Calculate for Srgb {
 }
 
 #[cfg(feature = "palette_color")]
-impl<Wp: palette::white_point::WhitePoint> Hamerly for Lab<Wp> {
+impl<T: Real, Wp: palette::white_point::WhitePoint<T>> Hamerly for Lab<Wp> {
     fn compute_half_distances(centers: &mut HamerlyCentroids<Self>) {
         // Find each center's closest center
         for ((i, ci), half_dist) in centers
@@ -441,9 +443,10 @@ pub trait MapColor: Sized {
 }
 
 #[cfg(feature = "palette_color")]
-impl<Wp> MapColor for Lab<Wp>
+impl<T, Wp> MapColor for Lab<Wp>
 where
-    Wp: palette::white_point::WhitePoint,
+    T: Real,
+    Wp: palette::white_point::WhitePoint<T>,
 {
     #[inline]
     fn map_indices_to_centroids(centroids: &[Self], indices: &[u8]) -> Vec<Self> {
@@ -459,9 +462,10 @@ where
 }
 
 #[cfg(feature = "palette_color")]
-impl<Wp> MapColor for palette::Laba<Wp>
+impl<T, Wp> MapColor for palette::Laba<Wp>
 where
-    Wp: palette::white_point::WhitePoint,
+    T: Real,
+    Wp: palette::white_point::WhitePoint<T>,
 {
     #[inline]
     fn map_indices_to_centroids(centroids: &[Self], indices: &[u8]) -> Vec<Self> {
@@ -477,7 +481,7 @@ where
 }
 
 #[cfg(feature = "palette_color")]
-impl<C> MapColor for Srgb<C>
+impl<T, C> MapColor for Srgb<C>
 where
     C: palette::Component,
 {

(For reference, there's a kind of changelog.)

from kmeans-colors.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.