Skip to content

How can I get modal average colors? #58

Answered by okaneco
Tameflame asked this question in Q&A
Discussion options

You must be logged in to vote

I'm not too familiar with SVG but that problem does sound like this library might not be the right fit.

If the images are flat or you'll have large areas with a solid color, I'd try using a hashmap for counting the most common colors. After rasterizing, you can populate a hashmap with the color as the key and the count as the value. It could look like the following. Then you can create a Vec of the (keys, values) and sort by the value to find the most common colors.

use std::collections::HashMap;

// If your colors are floats, you could use `{float}::to_bits` to put them in a hashmap since floats don't impl Hash
fn count_colors(
    pixels: &[[u8; 3]],
    map: &mut HashMap<[u8; 3], u64>,

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Tameflame
Comment options

@okaneco
Comment options

Answer selected by Tameflame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants