# RGB to HSV conversion function destroying my Pixelblaze?

**URL:** <https://forum.electromage.com/t/rgb-to-hsv-conversion-function-destroying-my-pixelblaze/3650>\
**Category:** Patterns and Code\
**Created:** [July 11, 2024, 5:41pm UTC](https://forum.electromage.com/t/rgb-to-hsv-conversion-function-destroying-my-pixelblaze/3650 "2024-07-11T17:41:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hololit](https://forum.electromage.com/letter_avatar_proxy/v4/letter/h/5daacb/32.png) [@hololit](https://forum.electromage.com/u/hololit)\
**Post date:** [July 11, 2024, 5:41pm UTC](https://forum.electromage.com/t/rgb-to-hsv-conversion-function-destroying-my-pixelblaze/3650/1 "2024-07-11T17:41:56Z")

</div>

Hello all,

As I’m starting patterns that get DMX channels for color information, I get on my Pixelblaze r,g,b information when using a color picker from most DMX consoles (using Resolume and QLC+ so far). I intend to create it as a framework that would allow any pattern to be controlled through DMX/Artnet/Flamecaster

But I would like to transform this data into h,s,v values to be used in my existing patterns, since they almost all use HSV for practical reasons.

I have asked my friend chatGPT to cook me a nice function using the code reference, and it looks correct (as in “no bug detected”), but executing it absolutely crashes my Pixelblaze and restarts it with a “No LED” configuration and very hot temperature.

What did we mess up ? I don’t see anything that could lead to a crash, and even though I’m not sure exactly about the math between, it looks correct.

Thank you !

```auto

export var rgb = [0, 1, 1] // Some RGB values
export var hsvValues = array(3)

function rgb2hsv(r, g, b) {
  
  RGBmax = max(r, g)
  RGBmax = max(RGBmax, b)
  
  RGBmin = min(r, g)
  RGBmin = max(RGBmin, b)
  
  deltaRGB = RGBmax - RGBmin
  h = 0
  s = (RGBmax == 0 ? 0 : deltaRGB / RGBmax)
  v = RGBmax

  if (RGBmax != RGBmin) {
    if (RGBmax == r) {
      h = (g - b) / deltaRGB + (g < b ? 6 : 0)
    } else if (RGBmax == g) {
      h = (b - r) / deltaRGB + 2
    } else if (RGBmax == b) {
      h = (r - g) / deltaRGB + 4
    }
    h /= 6
  }
  
  return [h, s, v]
}

export function beforeRender(delta) {
  //t1 = time(.1)
  
  hsvValues = rgb2hsv(rgb[0], rgb[1], rgb[2])
  
}

export function render2D(index,x,y) {
  
  hsv(hsvValues[0], hsvValues[1], hsvValues[2])
  
  //h = t1 + index/pixelCount
  //s = 1
  //v = 1
  //hsv(h, s, v)
}

```

---

<div class="post-metadata">

**Author:** ![wizard](https://forum.electromage.com/user_avatar/forum.electromage.com/wizard/32/4_2.png) [@wizard](https://forum.electromage.com/u/wizard)\
**Post date:** [July 11, 2024, 7:30pm UTC](https://forum.electromage.com/t/rgb-to-hsv-conversion-function-destroying-my-pixelblaze/3650/2 "2024-07-11T19:30:37Z")

</div>

I would check the forums first before ChatGPT. That implementation is creating an array for every call.

> [@Color blending code](https://forum.electromage.com/t/color-blending-code/1429/2):
>
> I have a few handy, seen in folk’s patterns or ported from plain JS: //linear interpolation function lerp(a, b, t) { return a \* (1-t) + b \* t } //cross fade RGB between 2 colors using linear interpolation //output sets r,g,b globals function crossFadeColors(r1, g1, b1, r2, g2, b2, t) { r = lerp(r1, r2, t) g = lerp(g1, g2, t) b = lerp(b1, b2, t) } //convert HSV to RGB //output sets r,g,b globals function hsv2rgb(hh, ss, vv) { var h = mod(hh, 1) var s = clamp(ss, 0, 1) var v = cl…

---

<div class="post-metadata">

**Author:** ![hololit](https://forum.electromage.com/letter_avatar_proxy/v4/letter/h/5daacb/32.png) [@hololit](https://forum.electromage.com/u/hololit)\
**Post date:** [July 12, 2024, 11:11am UTC](https://forum.electromage.com/t/rgb-to-hsv-conversion-function-destroying-my-pixelblaze/3650/3 "2024-07-12T11:11:24Z")

</div>

> [@Color blending code](https://forum.electromage.com/t/color-blending-code/1429/2):
>
> ndy, seen in folk’s patterns or ported from plain JS: //linear interpolation function lerp(a, b, t) { return a \* (1-t) + b \* t } //cross fade RGB between 2

Ah apologies, I actually did a search on the forum but failed to find this topic. Sorry.  
On a more general topic, I understand it creates an array for every call because we return an array every time ? It which means it completely saturates the RAM of the ESP32 and leads to a crash/heating ?

Is there a way to purge the variable once used, or force the code to use always the same array ? I can see returning an array from a function could be useful. Sorry if it’s a basic question.

Thank you

---

<div class="post-metadata">

**Author:** ![wizard](https://forum.electromage.com/user_avatar/forum.electromage.com/wizard/32/4_2.png) [@wizard](https://forum.electromage.com/u/wizard)\
**Post date:** [July 12, 2024, 2:57pm UTC](https://forum.electromage.com/t/rgb-to-hsv-conversion-function-destroying-my-pixelblaze/3650/4 "2024-07-12T14:57:05Z")

</div>

Nothing in software should be changing the heat profile in a noticeable way. The difference between an idle cpu and a busy one is a handful of milliamps and PB pretty much always uses the full cpu for fps.

But it might cause a crash. I have check in place to try to avoid memory overrunning and causing problems, but still possible. Usually you get an error back in the editor.

PB doesn’t currently have garbage collection, so it can’t free up any unused arrays. You can return an array by reference. So eg make a global and keep using that. The `array.replace()` function makes it efficient to reuse them and easily set all of their elements at once.

---

<div class="post-metadata">

**Author:** ![hololit](https://forum.electromage.com/letter_avatar_proxy/v4/letter/h/5daacb/32.png) [@hololit](https://forum.electromage.com/u/hololit)\
**Post date:** [July 14, 2024, 2:28pm UTC](https://forum.electromage.com/t/rgb-to-hsv-conversion-function-destroying-my-pixelblaze/3650/5 "2024-07-14T14:28:18Z")

</div>

It works great ! Thank you.

I’ll be back soon with some patterns adapted to be controllable from a DMX console, and a framework to handle this in a standardized way !
