module Colors::ColormapRegistry

Constants

BUILTIN_COLORMAPS
LUT_SIZE

Public Class Methods

[](name) click to toggle source
# File lib/colors/colormap_registry.rb, line 5
def self.[](name)
  return name if name.is_a?(Colormap)

  name = String.try_convert(name)
  if @registry.key?(name)
    return @registry[name]
  else
    raise ArgumentError, "Unknown colormap name: %p" % name
  end
end
load_colormap_data(name) click to toggle source
# File lib/colors/colormap_data.rb, line 12
def self.load_colormap_data(name)
  path = @colormaps_dir.join("#{name}.json")
  json = File.read(path)
  JSON.load(json, nil, symbolize_names: true, create_additions: false)
end
register(cmap, name: nil, override_builtin: false) click to toggle source
# File lib/colors/colormap_registry.rb, line 16
def self.register(cmap, name: nil, override_builtin: false)
  case name
  when String, Symbol
    name = name.to_s
  when nil
    name = cmap.name
    if name.nil?
      raise ArgumentError, "`name` cannot be omitted for unnamed colormaps"
    end
  else
    name = String.try_convert(name)
    if name.nil?
      raise ArgumentError, "`name` must be convertible to a String by to_str"
    end
  end

  if @registry.key?(name)
    if BUILTIN_COLORMAPS.key?(name)
      unless override_builtin
        raise ArgumentError,
              "Trying to re-register a builtin colormap: %p" % name
      end
    end
    warn "Trying to re-register the colormap %p which already exists" % name
  end

  unless cmap.is_a?(Colormap)
    raise ArgumentError,
          "Invalid value for registering a colormap (%p for a Colormap)" % cmap
  end

  @registry[name] = cmap
end
register_listed_colormap(name, data=nil) click to toggle source
# File lib/colors/colormap_data.rb, line 18
def self.register_listed_colormap(name, data=nil)
  data = load_colormap_data(name) if data.nil?
  colors = data.map {|r, g, b| Colors::RGB.new(r, g, b) }
  BUILTIN_COLORMAPS[name] = ListedColormap.new(colors, name: name)
end
unregister(name) click to toggle source
# File lib/colors/colormap_registry.rb, line 50
def self.unregister(name)
  if @registry.key?(name)
    if BUILTIN_COLORMAPS.key?(name)
      raise ArgumentError,
            "Unable to unregister the colormap %p which is a builtin colormap" % name
    end
  else
    @registry.delete(name)
  end
end