From: Max Williams Date: 2009-04-15T01:43:41+09:00 Subject: Re: A challenge: generate web colors for tree nodes. 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? -- Posted via http://www.ruby-forum.com/.