Faster Color Library suggestion for RGB to HSV

@bh
I'm processing images that have a bounding box around identified objects and using saturation as the most reliable way of finding the spatial limits of the bounding boxes

I needed a pixel -> HSV fast conversion and found that there are primitives in Snap! 7 to do this so I've produced a modified reporter to take use them

I used this formula in Wikipedia for the HSV -> HSL conversion

hopefully got it right :slight_smile:

<blocks app="Snap! 7dev, https://snap.berkeley.edu" version="2"><block-definition s="using primitives ✐ HSL/HSV (hsl? %&apos;HSL?&apos; ) from RGB %&apos;color&apos;" type="reporter" category="pen"><header></header><code></code><translations></translations><inputs><input type="%b">false</input><input type="%s"></input></inputs><script><block s="doIf"><block s="reportNot"><block var="HSL?"/></block><script><block s="doReport"><block s="reportProduct"><l>100</l><block s="reportApplyExtension"><l>clr_hsv(clr)</l><list><block s="reportApplyExtension"><l>clr_rgba(r, g, b, a)</l><list><block s="reportListItem"><l>1</l><block var="color"/></block><block s="reportListItem"><l>2</l><block var="color"/></block><block s="reportListItem"><l>3</l><block var="color"/></block></list></block></list></block></block></block></script></block><block s="doDeclareVariables"><list><l>HSV</l><l>lightness</l><l>saturation</l></list></block><block s="doSetVar"><l>HSV</l><block s="reportApplyExtension"><l>clr_hsv(clr)</l><list><block s="reportApplyExtension"><l>clr_rgba(r, g, b, a)</l><list><block s="reportListItem"><l>1</l><block var="color"/></block><block s="reportListItem"><l>2</l><block var="color"/></block><block s="reportListItem"><l>3</l><block var="color"/></block></list></block></list></block></block><block s="doSetVar"><l>lightness</l><block s="reportProduct"><block s="reportListItem"><l>3</l><block var="HSV"/></block><block s="reportDifference"><l>1</l><block s="reportQuotient"><block s="reportListItem"><l>2</l><block var="HSV"/></block><l>2</l></block></block></block></block><block s="doIfElse"><block s="reportOr"><block s="reportEquals"><block var="lightness"/><l>0</l></block><block s="reportEquals"><block var="lightness"/><l>1</l></block></block><script><block s="doSetVar"><l>saturation</l><l>0</l></block></script><script><block s="doSetVar"><l>saturation</l><block s="reportQuotient"><block s="reportDifference"><block s="reportListItem"><l>3</l><block var="HSV"/></block><block var="lightness"/></block><block s="reportMin"><block var="lightness"/><block s="reportDifference"><l>1</l><block var="lightness"/></block></block></block></block></script></block><block s="doReport"><block s="reportNewList"><list><block s="reportProduct"><l>100</l><block s="reportListItem"><l>1</l><block var="HSV"/></block></block><block s="reportProduct"><l>100</l><block var="saturation"/></block><block s="reportProduct"><l>100</l><block var="lightness"/></block></list></block></block></script></block-definition></blocks>

For my own purposes, I'm using the primitives directly as I don't need the 0-100 range conversion
I'm not using native JS as the speed-up isn't worth the hassle of enabling it

untitled script pic (17)
untitled script pic (18)

I used the same Wikipedia article you did. :~)

Actually I'm not sure exactly what you're asking for. You have the primitive; do you need more than that?

Sorry - I was suggesting it as an enhancement for your library

For Chrome@Win10 it's not that obvious as "map" is the main bottleneck.
untitled script pic (98)

Compiled "map" is much better, but still 3 times slower
untitled script pic (100)

Interesting. I'm going to be really upset if I can't use functional programming in the library! :~( :~(

:slight_smile:
I haven't used a compiled function in some time as I ran into problems once and spent days trying to find out why something didn't work the way I thought it should

It turned out to be use of a compiled reporter inside another reporter

So since then I've not used the facility

But in this case - it give's a nice free speed up :slight_smile:

I'm only getting images every 10 seconds or so, so a few seconds processing each one is fast enough for me

Yeah, it's marked "experimental" for a reason (in the hovertext), but it works great on really simple functions such as ITEM 1 OF. The thing is, its compiler isn't very smart. Someday, if we all wish hard for it, maybe all of Snap! will be a JIT compiler. :~) The thing is it can't happen if the JITness isn't good enough to let you edit a script while it's running; Jens would veto that.

Also, this is my first forray into using Snap! 7 primitives and I realise that to keep to the Snap! way, if we are going to use them (for reasons like this for big speedups) we should wrap them up to make projects look Snap!ish rather than primitiveish :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.