How to convert RGB color to HEX for binding

does it have to be 0,50,100 as values?

No, it needs to be a linear gradient between 0 and 100. I just used those values as examples of expected outputs.

how about hsl? would be easiest if you only have one variable value
just an expression as text would do too. it has everythign inbetween too ofc

In order to do that, I would need to map my value from 0 to 100 → 0 to 120 to match the hue numbers. Is there a function to map inside of an expression?

just multiply by 1.2

Oh, haha! Yep. Well, that’s another solution, thanks!

1 Like

I had written a class for such a thing.
EDIT: Oops! Sent it before I was ready. This will also do multi-color gradients. A script transform could use this class to pick a point along the gradient.

class gradientPoint:
	''' 
		Calculate the RGB value of a point on a gradient.
		 Usage: gradient(minval, maxval, [colors])
	    	minval - minimun value to scale to
	    	maxval - maximum value to scale to
	    	colors - A list of RGB colors delineating a series of
		             adjacent linear color gradients between each pair.
		 Example: scale the values between 20 and 35 to a three color gradient.
		 	p = gradientPoint(20, 35, ['#0000FF', '#FFFF00',  '#FF0000']) 
		 	After the instance is created, call it with a value to get the scaled color
			p(20) returns '#0000FF'
			p(25) returns '#AAAA55'
	'''
	
	def __init__(self, minval, maxval, colors):
		self.minval = minval
		self.maxval = maxval
		self.colors = []
		for color in colors:
			if type(color).__name__ == 'str':
				c = self.hex_to_rgb(color)
			elif type(color).__name__ == 'list':
				c = tuple(color)
			elif type(color).__name__ == 'tuple':
				c = color
			self.colors.append(c)
			
	def __call__(self, value):
		return self.calc(value)
	
	def calc(self, value):
		import sys
		# Smallest possible difference.
		epsilon = sys.float_info.epsilon
		
		# Keep value within limits
		if value < self.minval:
			value = self.minval
		if value > self.maxval:
			value = self.maxval
		''' 
		 	Determine where the given value falls proportionality within
		 	the range from minval->maxval and scale that fractional value
		 	by the total number in the "colors" pallette.
		 '''
		i_f = float(value-self.minval) / float(self.maxval-self.minval) * (len(self.colors)-1)
		''' 
			Determine the lower index of the pair of color indices this
		 	value corresponds and its fractional distance between the lower
		 	and the upper colors.
		 '''
		# Split into integer & fractional parts. 
		i, f = int(i_f // 1), i_f % 1  
		# Does it fall exactly on one of the color points?
		if f < epsilon:
			return self.rgb_to_hex(self.colors[i])
		# Otherwise return a color within the range between them.
		else:  
			(r1, g1, b1), (r2, g2, b2) = self.colors[i], self.colors[i+1]
			#print (r1, g1, b1), (r2, g2, b2)
			#print int(r1 + f*(r2-r1)), int(g1 + f*(g2-g1)), int(b1 + f*(b2-b1))
			return self.rgb_to_hex((int(r1 + f*(r2-r1)), int(g1 + f*(g2-g1)), int(b1 + f*(b2-b1))))
	
		
	def hex_to_rgb(self, value):
		'''
			Convert a hex RGB value to a tuple of 0-255 values
		'''
		value = value.lstrip('#')
		return tuple(int(value[i:i+2], 16) for i in (0, 2, 4))
	
	def rgb_to_hex(self, rgb):
		'''
			Convert an RGB tuple to a hex string
		'''
		return '#%02x%02x%02x' % rgb