interpolate hex color vectors in one line of ruby

June 27, 2007

which color lies halfway inbetween #ff00ff and #00ff00 you ask?

One line of ruby code can interolate two color vectors like they are usually found inside css files, like: body{background: #ff00ff; color: #00ff00}.

%w{ff00ff 00ff00}.map{|a|a.scan(/../).map{|b|b.hex}}.inject{|c,d|(0...c.size).each{|i|c[i]+=d[i]};c}.map{|e|"%02x"%(e>>1)}.join


#121 character solution by schmidt(
%w{ff00ff 00ff00}.map{|a|a.scan(/../).map{|b|b.hex}}.inject{|c,d|c.size.times{|i|c[i]+=d[i]};c}.map{|e|"%0x"%(e>>1)}.join

Answer -> 0x7f7f7f. sure, there must be a shorter version. Can you show it to me?

next step: color animations and gradients and HSR colorspace conversions mayby.

have fun

Technorati Tags: , , , , , ,

share this: