Class: Colors::Colormap
- Inherits:
-
Object
show all
- Defined in:
- lib/colors/colormap.rb
Constant Summary
collapse
- PNG_WIDTH =
512
- PNG_HEIGHT =
64
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, n_colors) ⇒ Colormap
Returns a new instance of Colormap.
3
4
5
6
7
8
9
10
11
12
13
|
# File 'lib/colors/colormap.rb', line 3
def initialize(name, n_colors)
@name = name
@n_colors = n_colors.to_int
@bad_color = RGBA.new(0r, 0r, 0r, 0r)
@under_color = nil
@over_color = nil
@under_index = self.n_colors
@over_index = self.n_colors + 1
@bad_index = self.n_colors + 2
@initialized = false
end
|
Instance Attribute Details
#n_colors ⇒ Object
Returns the value of attribute n_colors.
15
16
17
|
# File 'lib/colors/colormap.rb', line 15
def n_colors
@n_colors
end
|
#name ⇒ Object
Returns the value of attribute name.
15
16
17
|
# File 'lib/colors/colormap.rb', line 15
def name
@name
end
|
Instance Method Details
#[](x) ⇒ Object
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.rb', line 17
def [](x)
init_colormap unless @initialized
xs = Array(x)
scalar_p = (xs.length == 1) && xs[0] == x
if all_ratio?(xs)
xs.map! do |v|
v *= self.n_colors
v = -1 if v < 0
v = self.n_colors - 1 if v == self.n_colors
v.clamp(-1, self.n_colors).to_i
end
end
ys = xs.map do |v|
v = case
when v >= self.n_colors
@over_index
when v < 0
@under_index
else
v
end
@lookup_table[v]
end
if scalar_p
ys[0]
else
ys
end
end
|
#over_color ⇒ Object
50
51
52
53
|
# File 'lib/colors/colormap.rb', line 50
def over_color
init_colormap unless @initialized
@lookup_table[@over_index]
end
|
#over_color=(color) ⇒ Object
55
56
57
58
|
# File 'lib/colors/colormap.rb', line 55
def over_color=(color)
@over_color = color
update_extreme_colors if @initialized
end
|
#reversed ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/colors/colormap.rb', line 70
def reversed
rev_name = "#{self.name}_r" unless self.name.nil?
rev = make_reverse_colormap(rev_name)
rev.over_color = self.over_color
rev.under_color = self.under_color
return rev
end
|
#to_html ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/colors/colormap.rb', line 97
def to_html
require "base64"
png_blob = to_png
png_base64 = Base64.encode64(png_blob)
html = %Q[<div style="vertical-align: middle;">] +
%Q[<strong>#{self.name}</strong> ] +
%Q[</div>] +
%Q[<div class="cmap"><img alt="#{self.name} colormap" ] +
%Q[title="#{self.name}" style="border: 1px solid #555;" ] +
%Q[src="data:image/png;base64,#{png_base64}"></div>] +
%Q[<div style="vertical-align: middle; ] +
%Q[max-width: #{PNG_WIDTH + 2}px; ] +
%Q[display: flex; justify-content: space-between;">] +
%Q[<div style="float: left;">] +
%Q[#{html_color_block(under_color)} under</div>] +
%Q[<div style="float: right;">] +
%Q[over #{html_color_block(over_color)}</div>]
["text/html", html]
end
|
#to_png ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/colors/colormap.rb', line 85
def to_png
require "chunky_png"
png = ChunkyPNG::Image.new(PNG_WIDTH, PNG_HEIGHT, ChunkyPNG::Color::TRANSPARENT)
x = Utils.linspace(0, 1, PNG_WIDTH)
(0 ... PNG_WIDTH).each do |i|
color = self[x[i].to_f]
png_color = ChunkyPNG::Color.rgba(*color.components.map{|v| (v*255).to_i })
png.line(i, 0, i, PNG_HEIGHT-1, png_color)
end
png.to_blob
end
|
#under_color ⇒ Object
60
61
62
63
|
# File 'lib/colors/colormap.rb', line 60
def under_color
init_colormap unless @initialized
@lookup_table[@under_index]
end
|
#under_color=(color) ⇒ Object
65
66
67
68
|
# File 'lib/colors/colormap.rb', line 65
def under_color=(color)
@under_color = color
update_extreme_colors if @initialized
end
|