Skip to content

Commit

Permalink
fixed some issues with integers
Browse files Browse the repository at this point in the history
also changed source_color_from_image function
  • Loading branch information
Aiving committed Jan 25, 2024
1 parent 8ad68f6 commit bb87120
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "material-colors"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "Up-to-date material-color-utilities port"
documentation = "https://docs.rs/material-colors"
Expand Down
2 changes: 1 addition & 1 deletion src/quantize/quantizer_wu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl QuantizerWu {
self.moments_b[index] += blue as u32 * count;

self.moments[index] +=
count as f64 * ((red * red) + (green * green) + (blue * blue)) as f64;
count as f64 * ((red as f64).powi(2) + (green as f64).powi(2) + (blue as f64).powi(2));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Score {
for hue in hue_population.into_iter().take(360) {
let proportion = hue as f64 / population_sum;

for i in (hue - 14)..(hue + 16) {
for i in ((hue as i32) - 14)..((hue as i32) + 16) {
let neighbor_hue = sanitize_degrees_int(i as i32);

hue_excited_proportions[neighbor_hue as usize] += proportion;
Expand Down
19 changes: 2 additions & 17 deletions src/utils/image.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::quantize::quantizer::Quantizer;
use crate::quantize::quantizer_celebi::QuantizerCelebi;
use crate::score::Score;
use crate::utils::color::argb_from_rgb;

use super::color::Argb;

Expand All @@ -10,22 +9,8 @@ use super::color::Argb;
/// @param image The image element
///
/// @return Source color - the color most suitable for creating a UI theme
pub fn source_color_from_image(image: &[u8]) -> Argb {
// Convert Image data to Pixel Array
let mut pixels = vec![];

for i in (0..=image.len()).step_by(4) {
let [r, g, b, a] = [image[i], image[i + 1], image[i + 2], image[i + 3]];

if a < 255 {
continue;
}

pixels.push(argb_from_rgb([r, g, b]));
}

// Convert Pixels to Material Colors
let result = QuantizerCelebi.quantize(&pixels, 128, None);
pub fn source_color_from_image(image: &[Argb]) -> Argb {
let result = QuantizerCelebi.quantize(image, 128, None);
let ranked = Score::score(&result.color_to_count, None, None, None);

ranked[0]
Expand Down
2 changes: 1 addition & 1 deletion src/utils/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn lerp(start: f64, stop: f64, amount: f64) -> f64 {

pub fn sanitize_degrees_int(degrees: i32) -> u32 {
match degrees {
value if value < 0 => value as u32 + 360,
value if value < 0 => (value + 36) as u32,
value => value as u32 % 360,
}
}
Expand Down

0 comments on commit bb87120

Please sign in to comment.