Page 1 of 2

Fast RGB to HSV (And vice versa) calculations?

Posted: Sun Mar 17, 2019 12:55 pm
by hdsjulian
I'm looking for a way to calculate hsv and rgb values respectively. So far I just have used straight forward python implementation that does it, but it is _very_ slow (1.7ms on an esp8266).

Code: Select all

   
    h = float(h)
    s = float(s)
    v = float(v)
    h60 = h / 60.0
    h60f = math.floor(h60)
    hi = int(h60f) % 6
    f = h60 - h60f
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)
    r, g, b = 0, 0, 0
    if hi == 0: r, g, b = v, t, p
    elif hi == 1: r, g, b = q, v, p
    elif hi == 2: r, g, b = p, v, t
    elif hi == 3: r, g, b = p, q, v
    elif hi == 4: r, g, b = t, p, v
    elif hi == 5: r, g, b = v, p, q
    r, g, b = int(r * 255), int(g * 255), int(b * 255)
    return r, g, b
    
Anyone here who has had similar problems and found a solution?

Re: Fast RGB to HSV (And vice versa) calculations?

Posted: Tue Mar 19, 2019 1:46 am
by mattyt
I suggest first taking a look at Maximising MicroPython Speed to determine if you can apply any of the techniques there to improve the performance in MicroPython. Damien's PyCon AU talk on Writing fast and efficient MicroPython is full of useful tips too. Some of those may help get performance to an acceptable level for your implementation.

That said, numerical conversions - particularly if they need to be repeated frequently - are often good candidates to write as a C module. The Loboris port has completed half the work; an rgb_to_hsb conversion function in C and the NeoPixel module that allows it to be accessed from MicroPython. It's just that the implementation is bound to the NeoPixel module - you'd need to extract those parts and create a separate module. You might find that Mike Teachman's argument_examples may be useful to help define the interface to MicroPython from C.

Even better, it would be fantastic if you were to implement this as the start of a standard implementation of the colorsys module which MicroPython currently lacks. I for one would really appreciate it! :)

Re: Fast RGB to HSV (And vice versa) calculations?

Posted: Tue Mar 19, 2019 6:19 pm
by hdsjulian
unfortunately the whole reason i started using micropython on my esp8266 was that i just don't do well with C and i'd rather focus on getting my actual projects done (otherwise i could also just use arduino) :) so i doubt any c-stuff will come from me :/

Re: Fast RGB to HSV (And vice versa) calculations?

Posted: Wed Mar 20, 2019 12:55 am
by mattyt
Completely understand; so perhaps start with trying to apply the MicroPython performance suggestions.

I'd like to tackle implementing it as C module but spare time is currently in desperately short supply! I'll bump it up the priority list and try to get to it... In the meantime, please keep us posted as to how you progress using MicroPython!

Fast RGB to HSV (And vice versa) calculations?

Posted: Wed Mar 20, 2019 1:43 am
by jickster
Seems a perfect application for viper.

https://www.kickstarter.com/projects/21 ... ontrollers

Sent from my iPhone using Tapatalk Pro

Re: Fast RGB to HSV (And vice versa) calculations?

Posted: Wed Mar 20, 2019 1:47 am
by mattyt
Viper is great - unless you want it to work on multiple ports. Most of my development is on the ESP32 right now where there is no Viper implementation...

Re: Fast RGB to HSV (And vice versa) calculations?

Posted: Wed Mar 20, 2019 1:48 am
by jickster
mattyt wrote:Viper is great - unless you want it to work on multiple ports. Most of my development is on the ESP32 right now where there is no Viper implementation...
What about “@native”?


Sent from my iPhone using Tapatalk Pro

Re: Fast RGB to HSV (And vice versa) calculations?

Posted: Wed Mar 20, 2019 4:05 am
by mattyt
According to the forum post native and viper decorators on the ESP32 neither decorators are available for the ESP32.

It's possible that it has been added to the ESP32 port since that post but I couldn't find anything obvious in the codebase that made it clear...

Re: Fast RGB to HSV (And vice versa) calculations?

Posted: Wed Mar 20, 2019 4:46 pm
by oclyke
When I see HSV->RGB I think of this great write-up and portable C library: http://www.vagrearg.org/content/hsvrgb

Of course, not too helpful in the near-term but since mattyt is talking about a colorsys module it might be helpful. Speaking of which, I'd like to help out with something like that. Let's coordinate!

And even if you don't want to write C the article at that link is still a good read if you like math and optimizations :D

Re: Fast RGB to HSV (And vice versa) calculations?

Posted: Wed Mar 20, 2019 7:23 pm
by marfis
mattyt wrote:
Wed Mar 20, 2019 4:05 am

It's possible that it has been added to the ESP32 port since that post but I couldn't find anything obvious in the codebase that made it clear...
according to this
https://github.com/micropython/micropython/issues/4607

it doesn‘t have priority for Damien.