# Code review: Modifying RGB by Manipulating HSV colors

**URL:** <https://forum.inductiveautomation.com/t/code-review-modifying-rgb-by-manipulating-hsv-colors/58295>\
**Category:** General Discussion\
**Tags:** ignition80\
**Created:** [April 11, 2022, 2:13pm UTC](https://forum.inductiveautomation.com/t/code-review-modifying-rgb-by-manipulating-hsv-colors/58295 "2022-04-11T14:13:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [April 11, 2022, 2:13pm UTC](https://forum.inductiveautomation.com/t/code-review-modifying-rgb-by-manipulating-hsv-colors/58295/1 "2022-04-11T14:13:46Z")

</div>

In my ongoing question for a programmatically controlled gradient function ([here](https://forum.inductiveautomation.com/t/can-you-parameterize-a-style/57899) and [here](https://forum.inductiveautomation.com/t/how-do-you-use-the-expression-functions-brighter-and-darker/58182)) I stumbled upon a built in python library that does conversions between RGB and HSV. This allows be manipulate the RGB colors in the manner I desire. So I wrote a function to do so. However not being the most skilled in python, I have no idea if it can be done in a better manner.

This function:

1. Takes an RGB value in the form of #FFAABB etc
2. Converts it to HSV
3. Multiples the V component by a user supplied value
4. Converts it back to RGB
5. Reformats it back to #AABBCC format etc

Note that rgb\_to\_hsv returns a tuple, which of course is immutable in python.

```auto
def mulColorV(self, rgbHex, factV):
	"""
	Custom method that can be invoked directly on this component.

	Arguments:
		self: A reference to the component that is invoking this function.
		rgbHex: 
		factV: 
	"""
	try:
		import colorsys
		rgbDec=list(int(rgbHex[i:i+2], 16)/255.0 for i in (1, 3, 5))
		hsvDec=list(colorsys.rgb_to_hsv(rgbDec[0],rgbDec[1],rgbDec[2]))
		hsvDec[2]*=factV
		if hsvDec[2]>1.0:
			hsvDec[2]=1.0
		if hsvDec[2]<0.0:
			hsvDec[2]=0.0
		newRgb=colorsys.hsv_to_rgb(hsvDec[0],hsvDec[1],hsvDec[2])
		newHex='#{0:02X}{1:02X}{2:02X}'.format(int(newRgb[0]*255),int(newRgb[1]*255),int(newRgb[2]*255))
		return newHex
	except:
		return '#000000'

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 11, 2022, 4:44pm UTC](https://forum.inductiveautomation.com/t/code-review-modifying-rgb-by-manipulating-hsv-colors/58295/2 "2022-04-11T16:44:51Z")

</div>

You may be interested in Java’s `Color` class, which has various static methods to convert between HSV and RGB representations:  
[https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Color.html#getHSBColor(float,float,float)](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Color.html#getHSBColor(float,float,float))

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [April 12, 2022, 8:33pm UTC](https://forum.inductiveautomation.com/t/code-review-modifying-rgb-by-manipulating-hsv-colors/58295/3 "2022-04-12T20:33:09Z")

</div>

I just took a look at the java classes, and there seemed to be so much jumping through hoops that I’d end up with about the same code complexity that I already have. So unless there is a performance reason to take care of I’m currently not intending a re-write

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 12, 2022, 9:10pm UTC](https://forum.inductiveautomation.com/t/code-review-modifying-rgb-by-manipulating-hsv-colors/58295/4 "2022-04-12T21:10:18Z")

</div>

That’s fair. I would expect better performance out of a more Java-based solution, but it’s very likely not going to be enough to actually matter.

For posterity, a reimplementation of your method:

```auto
def mulColorV(rgbHex, factV):
	from com.inductiveautomation.ignition.common import TypeUtilities
	from java.awt import Color
	javaColor = TypeUtilities.getColorFromString(rgbHex)
	hsl = Color.RGBtoHSB(javaColor.red, javaColor.green, javaColor.blue, None)
	hsl[2] = max(0.0, min(hsl[2] * factV, 1.0))
	hslColor = Color.getHSBColor(*hsl)
	return "#" + TypeUtilities.colorToHex(hslColor)
	
mulColorV("#100000", 0.2)
mulColorV("#100000", 2.0) 

```

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [April 12, 2022, 10:10pm UTC](https://forum.inductiveautomation.com/t/code-review-modifying-rgb-by-manipulating-hsv-colors/58295/5 "2022-04-12T22:10:38Z")

</div>

OTOH I might steal that code 😉
