Module: Colors::ColormapRegistry

Defined in:
lib/colors/colormap_data.rb,
lib/colors/colormap_registry.rb,
lib/colors/colormap_data/seaborn_builtin.rb,
lib/colors/colormap_data/matplotlib_builtin.rb

Constant Summary collapse

BUILTIN_COLORMAPS =
{}
LUT_SIZE =
512

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# 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) ⇒ Object



12
13
14
15
16
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



18
19
20
21
22
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# 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