# How to optimize HSB color comparisons?

**URL:** https://discourse.processing.org/t/how-to-optimize-hsb-color-comparisons/38551
**Category:** Coding Questions
**Created:** [August 29, 2022, 10:22pm UTC](https://discourse.processing.org/t/how-to-optimize-hsb-color-comparisons/38551 "2022-08-29T22:22:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![i-make-robots](https://avatars.discourse-cdn.com/v4/letter/i/73ab20/32.png) [@i-make-robots](https://discourse.processing.org/u/i-make-robots)
#### Post date: [August 29, 2022, 10:22pm UTC](https://discourse.processing.org/t/how-to-optimize-hsb-color-comparisons/38551/1 "2022-08-29T22:22:13Z")

</div>

> **[GitHub - MarginallyClever/weaving\_algorithm: weaving thread between nails in...](https://github.com/MarginallyClever/weaving_algorithm)**
>
> weaving thread between nails in a circle to produce an image. - GitHub - MarginallyClever/weaving\_algorithm: weaving thread between nails in a circle to produce an image.

This sketch takes a picture and interprets it as though it was woven from colored string wrapped around a circle of nails. It was originally written in RGB space where I am most comfortable. I tried swapping out the RGB comparisons for HSI like so:

```auto
// the square of the linear distance between two colors.
float scoreColors(color c0,color c1) {
  float h = hue(c0)-hue(c1);
  float s = saturation(c0)-saturation(c1);
  float b = brightness(c0)-brightness(c1);
  return (h*h +s*s +b*b);
}

```

and it’s great! but also slow AF. I’m looking for ideas how to reduce the total number of repetitive HSB conversions. One I had was to somehow convert the entire source image via some HSBImage class and then work entirely in HSB - except when writing out to the visible buffers. Already it sounds like a mess.  
I tried using `colorMode(HSB)` but that’s not helpful, colors were wildly wrong and it didn’t seem to run any faster.

So if you have some ideas I’d love to hear them. Low priority, just a hobby for me.

Thanks!

---

<div class="post-metadata">

### Author: ![i-make-robots](https://avatars.discourse-cdn.com/v4/letter/i/73ab20/32.png) [@i-make-robots](https://discourse.processing.org/u/i-make-robots)
#### Post date: [August 29, 2022, 10:23pm UTC](https://discourse.processing.org/t/how-to-optimize-hsb-color-comparisons/38551/2 "2022-08-29T22:23:03Z")

</div>

oh and here’s some examples. with RGB:

[![](https://cdn.discordapp.com/attachments/812188915821445121/1013891302460489799/unknown.png) ](https://cdn.discordapp.com/attachments/812188915821445121/1013891302460489799/unknown.png)

with HSB:

[![](https://cdn.discordapp.com/attachments/812188915821445121/1013931408806396004/unknown.png) ](https://cdn.discordapp.com/attachments/812188915821445121/1013931408806396004/unknown.png)
