Class: CTioga2::Graphics::Styles::ColorMap
- Inherits:
-
Object
- Object
- CTioga2::Graphics::Styles::ColorMap
- Defined in:
- lib/ctioga2/graphics/styles/colormap.rb
Overview
For now, ColorMap relies on the intrisic tioga color
A mapping Z values -> color.
It can be a simple two-point gradient, but it can also be much more complex.
Basically, a ColorMap is a series of colors with an optional Z value (taken as the average of the ones around if missing) + a color for above and a color for below.
map, but it would be interesting to implement that “by hand” for the case when a byte of resolution isn’t enough (which are going to be rare, I think)
Instance Attribute Summary collapse
-
#above ⇒ Object
Colors for points of Z value below and above the limit; nil for no specific value, :mask for masking them out.
-
#below ⇒ Object
Colors for points of Z value below and above the limit; nil for no specific value, :mask for masking them out.
-
#colors ⇒ Object
Corresponding colors.
-
#rgb ⇒ Object
Whether the map follows RGB (true) or HLS (false).
-
#values ⇒ Object
Z values.
Class Method Summary collapse
-
.from_text(str) ⇒ Object
Creates a ColorMap from a text specification of the kind:.
Instance Method Summary collapse
-
#initialize(values = [], colors = []) ⇒ ColorMap
constructor
A new instance of ColorMap.
-
#prepare_data_display(t, data, zmin, zmax) ⇒ Object
Prepares the ‘data’, ‘colormap’ and ‘value_mask’ arguments to t.create_image based on the given data, and the min and max Z levels.
-
#to_colormap(t, zmin, zmax) ⇒ Object
Converts to a Tioga color_map.
-
#z_color(z, zmin, zmax) ⇒ Object
Returns a color triplet corresponding to the given z value.
Constructor Details
#initialize(values = [], colors = []) ⇒ ColorMap
Returns a new instance of ColorMap.
59 60 61 62 63 64 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 59 def initialize(values = [], colors = []) @values = values.dup @colors = colors.dup @rgb = true end |
Instance Attribute Details
#above ⇒ Object
These are currently not implemented.
Colors for points of Z value below and above the limit; nil for no specific value, :mask for masking them out
50 51 52 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 50 def above @above end |
#below ⇒ Object
These are currently not implemented.
Colors for points of Z value below and above the limit; nil for no specific value, :mask for masking them out
50 51 52 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 50 def below @below end |
#colors ⇒ Object
Corresponding colors
44 45 46 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 44 def colors @colors end |
#rgb ⇒ Object
Whether the map follows RGB (true) or HLS (false). On by default.
It does not change anything with respect to how the colors are interpreted: whatever happens, the values are RGB.
57 58 59 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 57 def rgb @rgb end |
#values ⇒ Object
Z values
41 42 43 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 41 def values @values end |
Class Method Details
.from_text(str) ⇒ Object
Creates a ColorMap from a text specification of the kind:
Red--Blue(1.0)--Green
- The specification can optionally be surrounded by colors with
-
Green::Red–Blue::Orange
Means that Green are for colors below, Orange for above. These colors can also be “cut” or “mask”, meaning that the corresponding side isn’t displayed.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 77 def self.from_text(str) str = str.dup hls = false re = /natural:?/i # Not too bad ? if str =~ re str.sub!(re,'') hls = true end l = str.split(/::/) if l.size == 2 # This is the complex case if l[1] =~ /--/ l.push('') else l.unshift('') end elsif l.size == 1 l.push('') l.unshift('') end ## @todo More and more I find that this metabuilder thing is ## a little cumbersome, especially since I have an ## additional type system on top of this one. colortype = Commands::CommandType.get_type('color') # Now, we have three elements if l[0].size > 0 if l[0] =~ /mask|cut/i below = :mask else below = colortype.string_to_type(l[0]) end else below = nil end if l[2].size > 0 if l[2] =~ /mask|cut/i above = :mask else above = colortype.string_to_type(l[2]) end else above = nil end specs = l[1].split(/--/) values = [] colors = [] for s in specs if s =~ /([^(]+)\((.*)\)/ values << $2.to_f colors << colortype.string_to_type($1) else values << nil colors << colortype.string_to_type(s) end end cm = ColorMap.new(values, colors) cm.above = above cm.below = below cm.rgb = ! hls return cm end |
Instance Method Details
#prepare_data_display(t, data, zmin, zmax) ⇒ Object
handle masking + in and out of range.
I don’t think this function is named properly.
Prepares the ‘data’, ‘colormap’ and ‘value_mask’ arguments to t.create_image based on the given data, and the min and max Z levels
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 155 def prepare_data_display(t, data, zmin, zmax) # We correct zmin and zmax cmap, zmin, zmax = *self.to_colormap(t, zmin, zmax) data = t.create_image_data(data.reverse_rows, 'min_value' => zmin, 'max_value' => zmax) return { 'data' => data, 'colormap' => cmap } end |
#to_colormap(t, zmin, zmax) ⇒ Object
That won’t work when there are things inside/outside
Converts to a Tioga color_map
of the map.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 191 def to_colormap(t, zmin, zmax) # OK. Now, we have correct z values. We just need to scale # them between z_values[0] and z_values.last, to get a [0:1] # interval. zvs = z_values(zmin, zmax) p_values = zvs.dup p_values.sub!(p_values.first) p_values.div!(p_values.last) dict = { 'points' => p_values } if @rgb dict['Rs'] = [] dict['Gs'] = [] dict['Bs'] = [] for col in @colors dict['Rs'] << col[0] dict['Gs'] << col[1] dict['Bs'] << col[2] end else dict['Hs'] = [] dict['Ls'] = [] dict['Ss'] = [] for col in @colors col = t.rgb_to_hls(col) dict['Hs'] << col[0] dict['Ls'] << col[1] dict['Ss'] << col[2] end end return [t.create_colormap(dict), zvs.first, zvs.last] end |
#z_color(z, zmin, zmax) ⇒ Object
For now, the HSV parameter isn’t honored.
Returns a color triplet corresponding to the given z value
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 171 def z_color(z, zmin, zmax) zvs = z_values(zmin, zmax) idx = zvs.where_first_ge(z) if idx && idx > 0 x = (zvs[idx] - z)/(zvs[idx] - zvs[idx-1]) c = Utils::mix_objects(@colors[idx-1],@colors[idx], x) # p [c, idx, z, zmin, zmax] return c elsif idx == 0 return @colors.first else return @colors.last end end |