<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: tagging code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 07 Oct 2008 03:53:09 GMT</pubDate>
    <description>DZone Snippets: tagging code</description>
    <item>
      <title>Tag Cloud in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2251</link>
      <description>The options.delete with the default looks mildly interesting. Just wanted to remember that.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def font_size_for_tag_cloud( total, lowest, highest, options={} )&lt;br /&gt; return nil if total.nil? or highest.nil? or lowest.nil?&lt;br /&gt; #&lt;br /&gt; # options&lt;br /&gt; maxf = options.delete( :max_font_size ) || 14&lt;br /&gt; minf = options.delete( :min_font_size ) || 11&lt;br /&gt; maxc = options.delete( :max_color ) || [ 0, 0, 0 ]&lt;br /&gt; minc = options.delete( :min_color ) || [ 156, 156, 156 ]&lt;br /&gt; hide_sizes = options.delete( :hide_sizes )&lt;br /&gt; hide_colours = options.delete( :hide_colours )&lt;br /&gt; #&lt;br /&gt; # function to work out rgb values&lt;br /&gt; def rgb_color( a, b, i, x)&lt;br /&gt;  return nil if i &lt;= 1 or x &lt;= 1&lt;br /&gt;  if a &gt; b&lt;br /&gt;   a-(Math.log(i)*(a-b)/Math.log(x)).floor&lt;br /&gt;  else&lt;br /&gt;   (Math.log(i)*(b-a)/Math.log(x)+a).floor&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt; #&lt;br /&gt; # work out colours&lt;br /&gt; c = []&lt;br /&gt; (0..2).each { |i| c &lt;&lt; rgb_color( minc[i], maxc[i], total, highest ) || nil }&lt;br /&gt; colors = c.compact.empty? ? minc.join(',') : c.join(',')&lt;br /&gt; #&lt;br /&gt; # work out the font size&lt;br /&gt; spread = highest.to_f - lowest.to_f&lt;br /&gt; spread = 1.to_f if spread &lt;= 0&lt;br /&gt; fontspread = maxf.to_f - minf.to_f&lt;br /&gt; fontstep = spread / fontspread&lt;br /&gt; size = ( minf + ( total.to_f / fontstep ) ).to_i&lt;br /&gt; size = maxf if size &gt; maxf&lt;br /&gt; #&lt;br /&gt; # display the results&lt;br /&gt; size_txt = "font-size:#{ size.to_s }px;" unless hide_sizes&lt;br /&gt; color_txt = "color:rgb(#{ colors });" unless hide_colours&lt;br /&gt; return [ size_txt, color_txt ].join&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 06 Jul 2006 17:01:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2251</guid>
      <author>jnunemaker ()</author>
    </item>
  </channel>
</rss>
