-
My use case is changing the colors of elements on .svg files. I want to rasterize an .svg file, pick out the most pixel-common colors, and then doing a basic text replacement on the fill attributes of the .svg file. How can I go about this with this library? Since it's a mean-search function are my options to use a different library or apply some soft of color distance function? I'm thinking the image library might be appropriate but I'll need to figure out the exact pixel count of each color present and I have no idea how I'd go about doing that Could I have advice on how I might go about either route? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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>,
vec: &mut Vec<([u8; 3], u64)>,
) {
// Count the colors in an image
for &color in pixels {
map.entry(color).and_modify(|c| *c += 1).or_insert(1);
}
// Fill a vector with the keys and values from the map
vec.extend(map.iter().map(|(&k, &v)| (k, v)));
// Sort the vec by the counts, the most common pixels will be at the end
// (use `std::cmp::Reverse(a.1)` if you want the most common at the front)
vec.sort_unstable_by_key(|a| a.1);
} (You may want to use From here, you can try using a color distance metric to see if any of the highest count values are close enough to the colors you have in the SVG text to know that you have a match. If that doesn't work, then Kmeans or some other clustering/segmenting algorithm might be helpful for finding common color pixel groupings. You'd segment the image then count the colors inside each cluster to find the mode color. Different clustering algorithms have pros and cons: some, like Kmeans, don't take locality into account which makes it faster but not always the best results. Some algorithms use locality as part of the distance metric for determining clusters. Hope this was helpful. |
Beta Was this translation helpful? Give feedback.
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.