# A challenge: generate web colors for tree nodes

**URL:** <https://www.ruby-forum.com/t/a-challenge-generate-web-colors-for-tree-nodes/165476>\
**Category:** Ruby\
**Created:** [April 14, 2009, 11:26am UTC](https://www.ruby-forum.com/t/a-challenge-generate-web-colors-for-tree-nodes/165476 "2009-04-14T11:26:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 14, 2009, 11:26am UTC](https://www.ruby-forum.com/t/a-challenge-generate-web-colors-for-tree-nodes/165476/1 "2009-04-14T11:26:33Z")

</div>

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 🙂 Hopefully i’ll post my own idea soon.

cheers  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 14, 2009, 6:43pm UTC](https://www.ruby-forum.com/t/a-challenge-generate-web-colors-for-tree-nodes/165476/2 "2009-04-14T18:43:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 14, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/a-challenge-generate-web-colors-for-tree-nodes/165476/3 "2009-04-14T21:36:19Z")

</div>

On Apr 14, 12:43 pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?
