Class: CTioga2::Graphics::Styles::ColorMap

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/graphics/styles/colormap.rb

Overview

TODO:

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = [], colors = []) ⇒ ColorMap

Returns a new instance of ColorMap.



63
64
65
66
67
68
69
70
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 63

def initialize(values = [], colors = [])
  @values = values.dup
  @colors = colors.dup

  @symmetry_center = nil

  @rgb = true
end

Instance Attribute Details

#aboveObject

TODO:

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

#belowObject

TODO:

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

#colorsObject

Corresponding colors



44
45
46
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 44

def colors
  @colors
end

#rgbObject

Whether the map is interpolated in the RGB (true) or HLS (false) color space. 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

#symmetry_centerObject

Whether or not the map should be considered symmetric around a given value.



61
62
63
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 61

def symmetry_center
  @symmetry_center
end

#valuesObject

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.



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 83

def self.from_text(str)
  str = str.dup
  hls = false
  re = /(natural|hls|wheel):?/i     # Not too bad ?
  if str =~ re
    str.sub!(re,'')
    hls = true
  end

  sym = nil

  re = /:(?:sym|around):(.*)$/i
  if str =~ re
    sym = $1.to_f
    str.sub!(re,'')
  end

  # We first try to see if it could be a color set ?
  colorsettype = Commands::CommandType.get_type('color-set')

  begin 
    set = colorsettype.string_to_type(str)
    cm = ColorMap.new([nil] * set.size, set)
    cm.rgb = ! hls
    cm.symmetry_center = sym
    return cm
  rescue Exception => e
    # This is not a color set
  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
  cm.symmetry_center = sym
  return cm
end

Instance Method Details

#prepare_data_display(t, data, zmin, zmax) ⇒ Object

TODO:

handle masking + in and out of range.

TODO:

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



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 183

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

TODO:

That won’t work when there are things inside/outside

Converts to a Tioga color_map

of the map.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 219

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

TODO:

For now, the HSV parameter isn’t honored.

Returns a color triplet corresponding to the given z value



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ctioga2/graphics/styles/colormap.rb', line 199

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