Source

community_codeswarm / color_range.py

The default branch has multiple heads

Full commit

#!/usr/bin/env python

"""A simple module to automatically create code_swarm color definitions for any number of given items.

The ranges get output in the Java format needed for code_swarm: 
255,255,255
a,b,c

All should have the same brightness: 
a+b+c = 450

"""

def get_color_string(item_names): 
	"""Get the color range for the number of items.

	To get that, slowly walk the rainbow: decrement red for green, then decrement 
green for blue, then decrement blue for red again. 

	Than means we have a range of points to move equal to 
	red 250 -> 100 = 150
	green 250 -> 100 = 150
	blue 250 -> 100 = 150
	= 450 points. We just need to give each item a number in the range 0 to 450. 
	>>> item_names = ["arne", "veronica"]
	>>> get_color_string(item_names)
	'ColorAssign0="arne","arne.*", 100,250,100, 100,250,100\\r\\nColorAssign1="veronica","veronica.*", 325,100,25, 325,100,25\\r\\n'

"""
	# Starting conditions
	result_string = ""
	number_of_items = len(item_names)
	for i in range(number_of_items): 
		#: The number of points we have to shift the color
		points_to_shift = int(float(i) / number_of_items * 450.0)
		if points_to_shift <= 150: 
			red = 250 - points_to_shift
			green = 100 + points_to_shift
			blue = 100
		elif points_to_shift <= 300: 
			red = 100
			green = 250 - points_to_shift + 150
			blue = 100 + points_to_shift - 150
		else: 
			red = 100 + points_to_shift - 300
			green = 100
			blue = 250 - points_to_shift + 300

		# create the result string
		# ColorAssign4="hg group","group.*", 200,50,10, 200,50,10
		result_string += "ColorAssign" + str(i+1) + '="' + item_names[i] + '","' + item_names[i] + '.*", ' + str(red) + "," + str(green) + "," + str(blue) + ", " + str(red) + "," + str(green) + "," + str(blue) + "\r\n"

	return result_string


#### Self-Test ####

def _test(): 
	from doctest import testmod
	testmod()

if __name__ == "__main__": 
	_test()
	print get_color_string(["arne", "veronica"])