Module: CA::Gnuplot::Palette

Defined in:
lib/carray/graphics/gnuplot.rb

Class Method Summary collapse

Class Method Details

.quote(text) ⇒ Object



1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
# File 'lib/carray/graphics/gnuplot.rb', line 1682

def self.quote (text)
  case text
  when Symbol
    text.to_s
  when /^\{\{(.*)\}\}$/
    $1
  else
    text = text.clone
    if text[0, 1] == "\\"
      text[1..-1]
    else
      text.gsub!(/\n/, '\\n')
      text.gsub!(/\t/, '\\t')
      text.gsub!(/"/, '\\\\"')
      '"' + text + '"'
    end
  end
end

.set(gp, kind, *argv) ⇒ Object



1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
# File 'lib/carray/graphics/gnuplot.rb', line 1701

def self.set (gp, kind, *argv)
  if argv.last.is_a?(Hash)
    opt = argv.pop
  else
    opt = {}
  end
  if opt[:model]
    gp.set %{ palette model #{opt[:model]} }
  end
  if opt[:maxcolors]
    gp.set %{ palette maxcolors #{opt[:maxcolors]} }
  end
  case kind
  when :defined
    self.set_palette_array(gp, argv)
  when :random
    n = argv.first || 10
    list = []
    n.times do |i|
      value = rand(256)*0x10000 + rand(256)*0x100 + rand(256)
      list << [i, "#%6x" % value]
    end
    self.set_palette_array(gp, list)
  when :rgbformulae
    set_palette_formula(gp, *argv[0,3])        
  when :file
    set_palette_file(gp, argv.first)                
  when :gmt
    set_palette_gmt(gp, argv.first, opt)
  when :cpt_city
    set_palette_cpt_city(gp, argv.first, opt)
  when Symbol
    set_palette_predefined(gp, kind)        
  when String
    set_palette_string(gp, kind)
  when CArray
    set_palette_carray(gp, kind)
  when Array
    self.set_palette_array(gp, kind)        
  when ColorPalette
    set_palette_color_palette(gp, kind)
  end
end

.set_palette_array(gp, array) ⇒ Object



1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
# File 'lib/carray/graphics/gnuplot.rb', line 1757

def self.set_palette_array (gp, array)
  gp.set "palette " + 
    'defined (' + array.map { |x|
      case x[1]
      when String
        [x[0], quote(x[1])].join(' ')
      else
        x.join(' ')
      end
    }.join(', ') + ')'
end

.set_palette_carray(gp, ca) ⇒ Object



1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
# File 'lib/carray/graphics/gnuplot.rb', line 1769

def self.set_palette_carray (gp, ca)
  gp.instance_exec {
    with_tempfile(1) { |tmpfile|
      open(tmpfile, "w") {|io| ca.dump_binary(io)}
      if ca.rank == 1 or ( ca.rank == 2 and ca.dim1 == 1)
        set("palette file '#{tmpfile}' binary record=#{ca.dim0} using 1:1:1 model #{model}")
      elsif ca.rank == 2 and ca.dim1 == 3
        set("palette file '#{tmpfile}' binary record=#{ca.dim0} using 1:2:3 model #{model}")
      else
        raise "invalid palette specification"
      end
    }
  }
end

.set_palette_color_palette(gp, pal) ⇒ Object



1784
1785
1786
# File 'lib/carray/graphics/gnuplot.rb', line 1784

def self.set_palette_color_palette (gp, pal)
  gp.put(pal.to_gnuplot)
end

.set_palette_cpt_city(gp, name, opt) ⇒ Object



1797
1798
1799
1800
1801
1802
1803
1804
1805
# File 'lib/carray/graphics/gnuplot.rb', line 1797

def self.set_palette_cpt_city (gp, name, opt)
  file = File.expand_path(File.join(ENV["CPTCITY"],name) + ".cpt")
  cpt = ColorPalette::CPT(file, :continuous=>opt[:continuous])
  if opt[:use_scale]
    min, max = cpt.min, cpt.max
    gp.set %{ cbrange [#{min}:#{max}] }
  end
  gp.put(cpt.to_gnuplot)
end

.set_palette_file(gp, *argv) ⇒ Object



1749
1750
1751
# File 'lib/carray/graphics/gnuplot.rb', line 1749

def self.set_palette_file (gp, *argv)
  gp.set 'palette file ' + quote(argv[0]) + " " + argv[1].to_s       
end

.set_palette_formula(gp, *argv) ⇒ Object



1745
1746
1747
# File 'lib/carray/graphics/gnuplot.rb', line 1745

def self.set_palette_formula (gp, *argv)
  gp.set 'palette rgbformulae ' + argv.join(', ')
end

.set_palette_gmt(gp, name, opt) ⇒ Object



1788
1789
1790
1791
1792
1793
1794
1795
# File 'lib/carray/graphics/gnuplot.rb', line 1788

def self.set_palette_gmt (gp, name, opt)
  cpt = ColorPalette::CPT(name, :continuous=>opt[:continuous])
  if opt[:use_scale]
    min, max = cpt.min, cpt.max
    gp.set %{ cbrange [#{min}:#{max}] }
  end
  gp.put(cpt.to_gnuplot)
end

.set_palette_predefined(gp, name) ⇒ Object



1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
# File 'lib/carray/graphics/gnuplot.rb', line 1807

def self.set_palette_predefined (gp, name)
  case name.to_s
  when "gray"
    gp.set %{ palette defined (0     "#FFFFFF", \
                               1     "#000000") }
  when "gray_inv"
    gp.set %{ palette defined (0     "#000000", \
                               1     "#FFFFFF") }
  when "rainbow"
    gp.set %{ palette rgbformulae 22,13,-31 }
  when "polar"
    gp.set %{ palette defined (-1 "blue",  \
                                0 "white", \
                                1 "red") }
  when "jet"
    gp.set %{ palette defined (0     "#00007F", \
                               0.125 "#0000FF", \
                               0.375 "#FFFFFF", \
                               0.625 "#FFFF00", \
                               0.875 "#FF0000", \
                               1     "#7F0000") }
  when "split"
    gp.set %{ palette defined (-1.0 "#7F7FFF", \
                               -0.5 "#000080", \
                                0.0 "#000000", \
                                0.5 "#800000", \
                                1.0 "#FF7F7F" ) }
  when "green_metal"
    gp.set %{ palette defined ( 0.0    "#000000", \
                                0.5263 "#8D8D6C", \
                                0.6842 "#92BDBE", \
                                1.0    "#FFFFFF" )}
  when "matlab"
    gp.set %{ palette defined ( 0 '#000090', \
                                1 '#000fff', \
                                2 '#0090ff', \
                                3 '#0fffee', \
                                4 '#90ff70', \
                                5 '#ffee00', \
                                6 '#ff7000', \
                                7 '#ee0000', \
                                8 '#7f0000')}
  when "matlabw"
    gp.set %{ palette defined ( 0 'white',
                                0.01 '#000090', \
                                1 '#000fff', \
                                2 '#0090ff', \
                                3 '#0fffee', \
                                4 '#90ff70', \
                                5 '#ffee00', \
                                6 '#ff7000', \
                                7 '#ee0000', \
                                8 '#7f0000')}
  else
    raise "Unknown palette name"
  end
end

.set_palette_string(gp, string) ⇒ Object



1753
1754
1755
# File 'lib/carray/graphics/gnuplot.rb', line 1753

def self.set_palette_string (gp, string)
  gp.set "palette " + string
end