Class: CA::Gnuplot::ColorPalette

Inherits:
Object
  • Object
show all
Defined in:
lib/carray/graphics/gnuplot.rb,
lib/carray/graphics/gnuplot.rb

Overview

:nodoc:

Direct Known Subclasses

ColorPalette

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, levels, color, scale = nil, continuous = false) ⇒ ColorPalette

Returns a new instance of ColorPalette.



1870
1871
1872
1873
1874
1875
1876
# File 'lib/carray/graphics/gnuplot.rb', line 1870

def initialize (model, levels, color, scale = nil, continuous = false)
  @model  = model
  @levels = levels
  @color  = color
  @scale  = scale || CArray.int(levels).seq!
  @continuous = continuous
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



1878
1879
1880
# File 'lib/carray/graphics/gnuplot.rb', line 1878

def colors
  @colors
end

#scaleObject (readonly)

Returns the value of attribute scale.



1878
1879
1880
# File 'lib/carray/graphics/gnuplot.rb', line 1878

def scale
  @scale
end

Class Method Details

.CPT(file, opt = nil) ⇒ Object



1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
# File 'lib/carray/graphics/gnuplot.rb', line 1918

def self.CPT (file, opt = nil)
  options = {:continuous => false}.update(opt)
  continuous = options[:continuous]
  if File.exist?(file)
    text = File.read(file)
  else
    if continuous
      text = IO.popen("makecpt -C#{file} -Z", "r") { |io| io.read }
    else
      text = IO.popen("makecpt -C#{file}", "r") { |io| io.read }        
    end 
    if text =~ /\A\s*\Z/
      raise "failed to makecpt #{file}"
    end
  end

  lines = text.split("\n").map{|line| line.strip }
  
  model   = "HSV"
  entries = []
  lines.each do |line|
    case line
    when /\A\#\s*COLOR_MODEL\s*=\s*(.+)\s*\Z/
      model = $1.upcase
    when /\A\Z/, /\A#/, /\A([FBN])\s*(.*)\Z/
      next
    else
      list = line.split(/\s+/)
      list[1] = list[1].split(/\-/)
      list[3] = list[3].split(/\-/)
      list = list.flatten.map{|x| x.to_f}
      entries.push(list)
    end
  end

  levels = entries.size * 2
  color  = CArray.float(levels, 3)
  scale  = CArray.float(levels)
  level = 0
  entries.each do |entry|
    scale[level] = entry[0]
    color[level, nil] = entry[1, 3]
    level += 1
    scale[level] = entry[4]
    color[level, nil] = entry[5, 3]
    level += 1
  end
  if model =~ /HSV/
    color[nil, 0] /= 360
  else
    color[] /= 255
  end

  return ColorPalette.new(model, levels, color, scale, options[:contiuous])
end

.GMT(file, continuous = true) ⇒ Object



1974
1975
1976
# File 'lib/carray/graphics/gnuplot.rb', line 1974

def self.GMT (file, continuous = true)    
  return CPT(file, :continuous=>continuous)
end

Instance Method Details

#maxObject



1884
1885
1886
# File 'lib/carray/graphics/gnuplot.rb', line 1884

def max
  return @scale.max
end

#minObject



1880
1881
1882
# File 'lib/carray/graphics/gnuplot.rb', line 1880

def min
  return @scale.min
end

#rangeObject



1888
1889
1890
# File 'lib/carray/graphics/gnuplot.rb', line 1888

def range
  return @scale.min..@scale.max
end

#to_gnuplotObject



1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
# File 'lib/carray/graphics/gnuplot.rb', line 1892

def to_gnuplot
  out = []
  maxcolors = 1024
  if @continuous
    (@levels).times do |i|
      level = @scale[i]
      out.push([level, *@color[i,nil].to_a])
    end
  else
    (@levels-1).times do |i|
      level  = @scale[i]
      level1 = @scale[i+1]
      out.push([level, *@color[i,nil].to_a])
      out.push([level1, *@color[i,nil].to_a])
    end
    maxcolors = @levels-1
  end
  return ["set palette model #{@model} maxcolors #{maxcolors} defined (\\",
    out.map{|list| "  " + list.join(" ")}.join(",\\\n") + "\\",
    ")"].join("\n")
end