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

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

Post by hdsjulian » Thu Mar 21, 2019 5:16 pm

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

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

Sent from my iPhone using Tapatalk Pro
unfortunately viper doesn't seem to support divisions? as there are quite a few going on (and necessary for HSV to RGB calculation) this isn't possible then, unless i'm doing something wrong.
i get "ViperTypeError: binary op __truediv__ not implemented"

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

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

Post by jickster » Thu Mar 21, 2019 6:26 pm

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

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

Sent from my iPhone using Tapatalk Pro
unfortunately viper doesn't seem to support divisions? as there are quite a few going on (and necessary for HSV to RGB calculation) this isn't possible then, unless i'm doing something wrong.
i get "ViperTypeError: binary op __truediv__ not implemented"
You’ll probably get the same error but try @micropython.native


Sent from my iPhone using Tapatalk Pro

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

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

Post by jickster » Thu Mar 21, 2019 7:28 pm

FYI what’s probably happening is the esp doesn’t have hardware divide.




Sent from my iPhone using Tapatalk Pro

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

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

Post by hdsjulian » Thu Mar 21, 2019 11:57 pm

jickster wrote:
Thu Mar 21, 2019 6:26 pm
hdsjulian wrote:
jickster wrote:
Wed Mar 20, 2019 1:43 am
Seems a perfect application for viper.

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

Sent from my iPhone using Tapatalk Pro
unfortunately viper doesn't seem to support divisions? as there are quite a few going on (and necessary for HSV to RGB calculation) this isn't possible then, unless i'm doing something wrong.
i get "ViperTypeError: binary op __truediv__ not implemented"
You’ll probably get the same error but try @micropython.native


Sent from my iPhone using Tapatalk Pro
Native actually works and makes it faster but i'm now still at .5ms for a conversion...

here's my code

Code: Select all

@micropython.viper
def hsv2rgbint(h: int, s: int, v:int):
	h60 = h / 60
	#basically do what rounding would do with floats
	h60t = int(h60/1000)
	h60f = (h60t*1000)
	hi = int(h60f/1000) % 6
	f = (h60 - h60f)
	p = v * (1000 - s)
	q = v * (1000 - (f * s)/1000)
	t = v * (1000 - (1000 - f) * s/1000)
	p = p/1000
	q = q/1000
	t = t/1000
	r1 = 0
	g1 = 0
	b1 = 0
	if hi == 0: 
		r1 = v
		g1 = t
		b1 = p
	elif hi == 1: 
		r1 = q
		g1 = v
		b1 = p
	elif hi == 2: 
		r1 = p
		g1 = v
		b1 = t
	elif hi == 3:
		r1 = p
		g1 = q
		b1 = v
	elif hi == 4: 
		r1 = t
		g1 = p
		b1 = v
	elif hi == 5: 
		r1 = v
		g1 = p
		b1 = q
	r = r1*255
	r = r1/1000
	g = g1*255
	g = g1/1000
	b = b1*255
	b = b1/1000
	return r, g, b
	

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

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

Post by jickster » Fri Mar 22, 2019 12:20 am

I find it suspicious that viper divide doesn’t exist but native divide does.




Sent from my iPhone using Tapatalk Pro

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

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

Post by jickster » Fri Mar 22, 2019 12:35 am

There’s one way to determine if the multiplies and divides are costing so much time.

Where you divide by 1000, replace it with right shift by 10.

a / 1000

Becomes

(a >> 10)

Multiplies turn into left shift by 10

a * 1000

Becomes

(a << 10)

This is “almost” correct because left/right shift by 10 is multiply/divide by 1024.

I’m not saying this is a satisfactory solution. I’m just trying to zero in on your function’s bottleneck.



Sent from my iPhone using Tapatalk Pro

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

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

Post by jickster » Sun Mar 24, 2019 5:48 pm

If I wrote the C code and showed you how to merge it into the firmware, would you do it?


Sent from my iPhone using Tapatalk Pro

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

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

Post by hdsjulian » Mon Mar 25, 2019 9:29 pm

jickster wrote:
Sun Mar 24, 2019 5:48 pm
If I wrote the C code and showed you how to merge it into the firmware, would you do it?


Sent from my iPhone using Tapatalk Pro
i found this btw, a basic implementation of FastLED, for an older version of micropython, but it was discontinued. i'd guess you can just pull this out. https://github.com/aykevl/micropython/tree/modpixel
the bigger question then is, would this ever have the chance to be added to the main branch of micropython or would i forever have to build my own version :)

(answering your actual question, if you mean me: i'm super interested but right now my ability to work on this stuff is like "oh i've got an hour in between, lets play around with some LEDs" and I fear in order to learn the merging stuff i'd have to commit to more time and don't really see when i'd have it :(

User avatar
mattyt
Posts: 275
Joined: Mon Jan 23, 2017 6:39 am

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

Post by mattyt » Mon Mar 25, 2019 11:22 pm

hdsjulian wrote:
Mon Mar 25, 2019 9:29 pm
the bigger question then is, would this ever have the chance to be added to the main branch of micropython or would i forever have to build my own version :)
My guess is that this wouldn't end up in the main branch; at least not unless it was portable across most/all of the ports. FastLED is not particularly small!

However, there's been a lot of work recently to allow native modules to be loaded at runtime. It's not quite ready for prime time but the idea is that it would allow you to build a FastLED module (which would be a C module wrapping the FastLED code, presenting a MicroPython API) that could be loaded with 'import fastled'. It would have to be built for each port but that's manageable.

Damien gave a talk on Native Modules in MicroPython at our February meetup.

I think it'll be an important feature to allow extending MicroPython cleanly.
marfis wrote:
Wed Mar 20, 2019 7:23 pm
according to this
https://github.com/micropython/micropython/issues/4607
it doesn‘t have priority for Damien.
Sure, Damien has plenty on his plate and I wouldn't expect colorsys to be high on his list. That doesn't mean it won't be welcomed! I'm certain he'd accept a good-quality PR for colorsys. :)

Post Reply