## Fast RGB to HSV (And vice versa) calculations?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
hdsjulian
Posts: 31
Joined: Mon Dec 03, 2018 8:29 pm

### Fast RGB to HSV (And vice versa) calculations?

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?

mattyt
Posts: 297
Joined: Mon Jan 23, 2017 6:39 am

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

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!

hdsjulian
Posts: 31
Joined: Mon Dec 03, 2018 8:29 pm

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

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 :/

mattyt
Posts: 297
Joined: Mon Jan 23, 2017 6:39 am

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

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!

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

### Fast RGB to HSV (And vice versa) calculations?

Seems a perfect application for viper.

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

Sent from my iPhone using Tapatalk Pro

mattyt
Posts: 297
Joined: Mon Jan 23, 2017 6:39 am

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

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...

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

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

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...

Sent from my iPhone using Tapatalk Pro

mattyt
Posts: 297
Joined: Mon Jan 23, 2017 6:39 am

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

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...

oclyke
Posts: 11
Joined: Tue Mar 19, 2019 4:55 am

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

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

marfis
Posts: 197
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

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

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.