A challenge: generate web colors for tree nodes

hey all

here’s a nice little ruby challenge to welcome everyone back after
easter:

I have a long (about 1100) list of elements (class name Property),
arranged into a tree structure. I want each node to have its own unique
color, as a web color format string (eg “#112233”). Each has an id, a
parent_id and a name - the color can be generated using any or all of
these, or by some other (consistent) method.

Further requirements:
The color can’t be brighter than #aaaaaa.
Properties with the same parent_id should have distinctly different
colors.
The color can be essentially random, but should be consistent - ie if
it’s generated using the name then it should only ever change if the
name changes.

I’ve got a few vague ideas involving SHA1 but i thought i’d throw this
out as a fun ruby task :slight_smile: Hopefully i’ll post my own idea soon.

cheers
max

OK, here’s a stab which doesn’t satisfy all the requirements.

It makes all unique colors for all 1123 properties, probably by a fluke.
It does set a brightness limit but doesn’t make all children of a
given node have not-similar colors:

#generate a unique color for this property
def web_color
hashable = “#{self.id}#{self.parent_id}#{self.name}”
“#” + Digest::SHA1.hexdigest(hashable)[0…5]
end

#normalise color so that it’s not above an overall ‘brightness’
def normalised_web_color(max_brightness=200)
color = self.web_color
arr = [color[1…2], color[3…4], color[5…6]].collect(&:hex)
#we only want to normalise if all values are over max_brightness
if arr.all?{|x| x > max_brightness}
arr = arr.collect { |x| x = ((x.to_f/255)*max_brightness).to_i }
end
“##{arr.collect{|x| sprintf(”%02x", x)}.join}"
end

Feels kind of hacky and dirty to me…any thoughts, anyone?

On Apr 14, 12:43 pm, Max W. [email protected] wrote:

OK, here’s a stab which doesn’t satisfy all the requirements.

It makes all unique colors for all 1123 properties, probably by a fluke.
It does set a brightness limit but doesn’t make all children of a
given node have not-similar colors:

That’s the hard part. What defines color similarity? This involves
color perception theory. We lose our ability to differentiate colors
as the brightness goes down. Can you tell #000002 from #020000?
Perhaps you should have a minimum brightness. Also, how many colors
can we reasonably distinguish? What if a node has 20 children? 50? 250?